blob: 83858fb8b9828d93ced95b63a0e67c94fc8e3180 [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
10use strict;
11use warnings;
Kay Sievers19806692005-08-07 20:26:27 +020012use CGI qw(:standard :escapeHTML -nosticky);
Kay Sievers7403d502005-08-07 20:23:49 +020013use CGI::Util qw(unescape);
Kay Sievers161332a2005-08-07 19:49:46 +020014use CGI::Carp qw(fatalsToBrowser);
Kay Sievers40c13812005-11-19 17:41:29 +010015use Encode;
Kay Sieversb87d78d2005-08-07 20:21:04 +020016use Fcntl ':mode';
Junio C Hamano7a13b992006-07-31 19:18:34 -070017use File::Find qw();
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +053018use File::Basename qw(basename);
Kay Sievers10bb9032005-11-23 04:26:40 +010019binmode STDOUT, ':utf8';
Kay Sievers161332a2005-08-07 19:49:46 +020020
Jakub Narebskib1f5f642006-12-28 00:00:52 +010021BEGIN {
Jakub Narebski3be8e722007-04-01 22:22:21 +020022 CGI->compile() if $ENV{'MOD_PERL'};
Jakub Narebskib1f5f642006-12-28 00:00:52 +010023}
24
Dennis Stosberg4a87b432006-06-21 15:07:08 +020025our $cgi = new CGI;
Junio C Hamano06c084d2006-08-02 13:50:20 -070026our $version = "++GIT_VERSION++";
Dennis Stosberg4a87b432006-06-21 15:07:08 +020027our $my_url = $cgi->url();
28our $my_uri = $cgi->url(-absolute => 1);
Kay Sievers44ad2972005-08-07 19:59:24 +020029
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010030# Base URL for relative URLs in gitweb ($logo, $favicon, ...),
31# needed and used only for URLs with nonempty PATH_INFO
32our $base_url = $my_url;
33
34# When the script is used as DirectoryIndex, the URL does not contain the name
35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
36# have to do it ourselves. We make $path_info global because it's also used
37# later on.
38#
39# Another issue with the script being the DirectoryIndex is that the resulting
40# $my_url data is not the full script URL: this is good, because we want
41# generated links to keep implying the script name if it wasn't explicitly
42# indicated in the URL we're handling, but it means that $my_url cannot be used
43# as base URL.
44# Therefore, if we needed to strip PATH_INFO, then we know that we have
45# to build the base URL ourselves:
Alexander Gavrilovdde80d92008-11-06 01:10:07 +030046our $path_info = $ENV{"PATH_INFO"};
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +020047if ($path_info) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +010048 if ($my_url =~ s,\Q$path_info\E$,, &&
49 $my_uri =~ s,\Q$path_info\E$,, &&
50 defined $ENV{'SCRIPT_NAME'}) {
51 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
52 }
Giuseppe Bilottab65910f2008-09-29 15:07:42 +020053}
54
Alp Tokere130dda2006-07-12 23:55:10 +010055# core git executable to use
56# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070057our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020058
Kay Sieversb87d78d2005-08-07 20:21:04 +020059# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020060#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070061our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020062
Luke Luca5e9492007-10-16 20:45:25 -070063# fs traversing limit for getting project list
64# the number is relative to the projectroot
65our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
66
Kay Sieversb87d78d2005-08-07 20:21:04 +020067# target of the home link on top of all pages
Martin Waitz6132b7e2006-08-17 00:28:39 +020068our $home_link = $my_uri || "/";
Kay Sieversb87d78d2005-08-07 20:21:04 +020069
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090070# string of the home link on top of all pages
71our $home_link_str = "++GITWEB_HOME_LINK_STR++";
72
Alp Toker49da1da2006-07-11 21:10:26 +010073# name of your site or organization to appear in page titles
74# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020075our $site_name = "++GITWEB_SITENAME++"
76 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010077
Alan Chandlerb2d34762006-10-03 13:49:03 +010078# filename of html text to include at top of each page
79our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020080# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070081our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010082# filename of html text to include at bottom of each page
83our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020084
Alan Chandlerb2d34762006-10-03 13:49:03 +010085# URI of stylesheets
86our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +020087# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
88our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020089# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -070090our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +020091# URI of GIT favicon, assumed to be image/png type
92our $favicon = "++GITWEB_FAVICON++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +020093
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020094# URI and label (title) of GIT logo link
95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
96#our $logo_label = "git documentation";
97our $logo_url = "http://git.or.cz/";
98our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -070099
Kay Sievers09bd7892005-08-07 20:21:23 +0200100# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -0700101our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +0200102
Michael Hendricks55feb122007-07-04 18:36:48 -0600103# the width (in characters) of the projects list "Description" column
104our $projects_list_description_width = 25;
105
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +0200106# default order of projects list
107# valid values are none, project, descr, owner, and age
108our $default_projects_order = "project";
109
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200110# show repository only if this file exists
111# (only effective if this variable evaluates to true)
112our $export_ok = "++GITWEB_EXPORT_OK++";
113
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300114# show repository only if this subroutine returns true
115# when given the path to the project, for example:
116# sub { return -e "$_[0]/git-daemon-export-ok"; }
117our $export_auth_hook = undef;
118
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200119# only allow viewing of repositories also shown on the overview page
120our $strict_export = "++GITWEB_STRICT_EXPORT++";
121
Jakub Narebski19a87212006-08-15 23:03:17 +0200122# list of git base URLs used for URL to where fetch project from,
123# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +0200124our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +0200125
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200126# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200127our $default_blob_plain_mimetype = 'text/plain';
128our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200129
Petr Baudis2d007372006-06-18 00:01:06 +0200130# file to use for guessing MIME types before trying /etc/mime.types
131# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200132our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200133
Martin Koegler00f429a2007-06-03 17:42:44 +0200134# assume this charset if line contains non-UTF-8 characters;
135# it should be valid encoding (see Encoding::Supported(3pm) for list),
136# for which encoding all byte sequences are valid, for example
137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
138# could be even 'utf-8' for the old behavior)
139our $fallback_encoding = 'latin1';
140
Jakub Narebski69a9b412007-07-20 02:15:09 +0200141# rename detection options for git-diff and git-diff-tree
142# - default is '-M', with the cost proportional to
143# (number of removed files) * (number of new files).
144# - more costly is '-C' (which implies '-M'), with the cost proportional to
145# (number of changed files + number of removed files) * (number of new files)
146# - even more costly is '-C', '--find-copies-harder' with cost
147# (number of files in the original tree) * (number of new files)
148# - one might want to include '-B' option, e.g. '-B', '-M'
149our @diff_opts = ('-M'); # taken from git_commit
150
Matt McCutchen7e1100e2009-02-07 19:00:09 -0500151# Disables features that would allow repository owners to inject script into
152# the gitweb domain.
153our $prevent_xss = 0;
154
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200155# information about snapshot formats that gitweb is capable of serving
156our %known_snapshot_formats = (
157 # name => {
158 # 'display' => display name,
159 # 'type' => mime type,
160 # 'suffix' => filename suffix,
161 # 'format' => --format for git-archive,
162 # 'compressor' => [compressor command and arguments]
163 # (array reference, optional)}
164 #
165 'tgz' => {
166 'display' => 'tar.gz',
167 'type' => 'application/x-gzip',
168 'suffix' => '.tar.gz',
169 'format' => 'tar',
170 'compressor' => ['gzip']},
171
172 'tbz2' => {
173 'display' => 'tar.bz2',
174 'type' => 'application/x-bzip2',
175 'suffix' => '.tar.bz2',
176 'format' => 'tar',
177 'compressor' => ['bzip2']},
178
179 'zip' => {
180 'display' => 'zip',
181 'type' => 'application/x-zip',
182 'suffix' => '.zip',
183 'format' => 'zip'},
184);
185
186# Aliases so we understand old gitweb.snapshot values in repository
187# configuration.
188our %known_snapshot_format_aliases = (
189 'gzip' => 'tgz',
190 'bzip2' => 'tbz2',
191
192 # backward compatibility: legacy gitweb config support
193 'x-gzip' => undef, 'gz' => undef,
194 'x-bzip2' => undef, 'bz2' => undef,
195 'x-zip' => undef, '' => undef,
196);
197
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530198# You define site-wide feature defaults here; override them with
199# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200200our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200201 # feature => {
202 # 'sub' => feature-sub (subroutine),
203 # 'override' => allow-override (boolean),
204 # 'default' => [ default options...] (array reference)}
205 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200206 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200207 # then feature-sub will be called with default options as parameters;
208 # return value of feature-sub indicates if to enable specified feature
209 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200210 # if there is no 'sub' key (no feature-sub), then feature cannot be
211 # overriden
212 #
Giuseppe Bilottaff3c0ff2008-12-02 14:57:28 -0800213 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
214 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
215 # is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530216
Petr Baudis45a3b122006-10-07 15:17:47 +0200217 # Enable the 'blame' blob view, showing the last commit that modified
218 # each line in the file. This can be very CPU-intensive.
219
220 # To enable system wide have in $GITWEB_CONFIG
221 # $feature{'blame'}{'default'} = [1];
222 # To have project specific config enable override in $GITWEB_CONFIG
223 # $feature{'blame'}{'override'} = 1;
224 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200225 'blame' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800226 'sub' => sub { feature_bool('blame', @_) },
Jakub Narebski952c65f2006-08-22 16:52:50 +0200227 'override' => 0,
228 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530229
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200230 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200231 # tree. This can potentially generate high traffic if you have large
232 # project.
233
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200234 # Value is a list of formats defined in %known_snapshot_formats that
235 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200236 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200237 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200238 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100239 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200240 # and in project config, a comma-separated list of formats or "none"
241 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200242 'snapshot' => {
243 'sub' => \&feature_snapshot,
244 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200245 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200246
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000247 # Enable text search, which will list the commits which match author,
248 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200249 # Project specific override is not supported.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000250 'search' => {
251 'override' => 0,
252 'default' => [1]},
253
Petr Baudise7738552007-05-17 04:31:12 +0200254 # Enable grep search, which will list the files in currently selected
255 # tree containing the given string. Enabled by default. This can be
256 # potentially CPU-intensive, of course.
257
258 # To enable system wide have in $GITWEB_CONFIG
259 # $feature{'grep'}{'default'} = [1];
260 # To have project specific config enable override in $GITWEB_CONFIG
261 # $feature{'grep'}{'override'} = 1;
262 # and in project config gitweb.grep = 0|1;
263 'grep' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800264 'sub' => sub { feature_bool('grep', @_) },
Petr Baudise7738552007-05-17 04:31:12 +0200265 'override' => 0,
266 'default' => [1]},
267
Petr Baudis45a3b122006-10-07 15:17:47 +0200268 # Enable the pickaxe search, which will list the commits that modified
269 # a given string in a file. This can be practical and quite faster
270 # alternative to 'blame', but still potentially CPU-intensive.
271
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'pickaxe'}{'default'} = [1];
274 # To have project specific config enable override in $GITWEB_CONFIG
275 # $feature{'pickaxe'}{'override'} = 1;
276 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200277 'pickaxe' => {
Matt Kraaicdad8172008-12-15 22:16:19 -0800278 'sub' => sub { feature_bool('pickaxe', @_) },
Jakub Narebski04f7a942006-09-11 00:29:27 +0200279 'override' => 0,
280 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200281
Petr Baudis45a3b122006-10-07 15:17:47 +0200282 # Make gitweb use an alternative format of the URLs which can be
283 # more readable and natural-looking: project name is embedded
284 # directly in the path and the query string contains other
285 # auxiliary information. All gitweb installations recognize
286 # URL in either format; this configures in which formats gitweb
287 # generates links.
288
289 # To enable system wide have in $GITWEB_CONFIG
290 # $feature{'pathinfo'}{'default'} = [1];
291 # Project specific override is not supported.
292
293 # Note that you will need to change the default location of CSS,
294 # favicon, logo and possibly other files to an absolute URL. Also,
295 # if gitweb.cgi serves as your indexfile, you will need to force
296 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200297 'pathinfo' => {
298 'override' => 0,
299 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200300
301 # Make gitweb consider projects in project root subdirectories
302 # to be forks of existing projects. Given project $projname.git,
303 # projects matching $projname/*.git will not be shown in the main
304 # projects list, instead a '+' mark will be added to $projname
305 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200306 # all the forks. If project list is taken from a file, forks have
307 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200308
309 # To enable system wide have in $GITWEB_CONFIG
310 # $feature{'forks'}{'default'} = [1];
311 # Project specific override is not supported.
312 'forks' => {
313 'override' => 0,
314 'default' => [0]},
Petr Baudisd627f682008-10-02 16:36:52 +0200315
316 # Insert custom links to the action bar of all project pages.
317 # This enables you mainly to link to third-party scripts integrating
318 # into gitweb; e.g. git-browser for graphical history representation
319 # or custom web-based repository administration interface.
320
321 # The 'default' value consists of a list of triplets in the form
322 # (label, link, position) where position is the label after which
Jakub Narebski2b11e052008-10-12 00:02:32 +0200323 # to insert the link and link is a format string where %n expands
Petr Baudisd627f682008-10-02 16:36:52 +0200324 # to the project name, %f to the project path within the filesystem,
325 # %h to the current hash (h gitweb parameter) and %b to the current
Jakub Narebski2b11e052008-10-12 00:02:32 +0200326 # hash base (hb gitweb parameter); %% expands to %.
Petr Baudisd627f682008-10-02 16:36:52 +0200327
328 # To enable system wide have in $GITWEB_CONFIG e.g.
329 # $feature{'actions'}{'default'} = [('graphiclog',
330 # '/git-browser/by-commit.html?r=%n', 'summary')];
331 # Project specific override is not supported.
332 'actions' => {
333 'override' => 0,
334 'default' => []},
Shawn O. Pearce3e3d4ee2008-10-03 07:41:25 -0700335
Petr Baudisaed93de2008-10-02 17:13:02 +0200336 # Allow gitweb scan project content tags described in ctags/
337 # of project repository, and display the popular Web 2.0-ish
338 # "tag cloud" near the project list. Note that this is something
339 # COMPLETELY different from the normal Git tags.
340
341 # gitweb by itself can show existing tags, but it does not handle
342 # tagging itself; you need an external application for that.
343 # For an example script, check Girocco's cgi/tagproj.cgi.
344 # You may want to install the HTML::TagCloud Perl module to get
345 # a pretty tag cloud instead of just a list of tags.
346
347 # To enable system wide have in $GITWEB_CONFIG
348 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
349 # Project specific override is not supported.
350 'ctags' => {
351 'override' => 0,
352 'default' => [0]},
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100353
354 # The maximum number of patches in a patchset generated in patch
355 # view. Set this to 0 or undef to disable patch view, or to a
356 # negative number to remove any limit.
357
358 # To disable system wide have in $GITWEB_CONFIG
359 # $feature{'patches'}{'default'} = [0];
360 # To have project specific config enable override in $GITWEB_CONFIG
361 # $feature{'patches'}{'override'} = 1;
362 # and in project config gitweb.patches = 0|n;
363 # where n is the maximum number of patches allowed in a patchset.
364 'patches' => {
365 'sub' => \&feature_patches,
366 'override' => 0,
367 'default' => [16]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530368);
369
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800370sub gitweb_get_feature {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530371 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200372 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200373 my ($sub, $override, @defaults) = (
374 $feature{$name}{'sub'},
375 $feature{$name}{'override'},
376 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530377 if (!$override) { return @defaults; }
Martin Waitza9455912006-10-03 20:07:43 +0200378 if (!defined $sub) {
379 warn "feature $name is not overrideable";
380 return @defaults;
381 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530382 return $sub->(@defaults);
383}
384
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800385# A wrapper to check if a given feature is enabled.
386# With this, you can say
387#
388# my $bool_feat = gitweb_check_feature('bool_feat');
389# gitweb_check_feature('bool_feat') or somecode;
390#
391# instead of
392#
393# my ($bool_feat) = gitweb_get_feature('bool_feat');
394# (gitweb_get_feature('bool_feat'))[0] or somecode;
395#
396sub gitweb_check_feature {
397 return (gitweb_get_feature(@_))[0];
398}
399
400
Matt Kraaicdad8172008-12-15 22:16:19 -0800401sub feature_bool {
402 my $key = shift;
403 my ($val) = git_get_project_config($key, '--bool');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530404
Marcel M. Carydf5d10a2009-02-18 14:09:41 +0100405 if (!defined $val) {
406 return ($_[0]);
407 } elsif ($val eq 'true') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800408 return (1);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530409 } elsif ($val eq 'false') {
Matt Kraaicdad8172008-12-15 22:16:19 -0800410 return (0);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530411 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530412}
413
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530414sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200415 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530416
417 my ($val) = git_get_project_config('snapshot');
418
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200419 if ($val) {
420 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530421 }
422
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200423 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700424}
425
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100426sub feature_patches {
427 my @val = (git_get_project_config('patches', '--int'));
428
429 if (@val) {
430 return @val;
431 }
432
433 return ($_[0]);
434}
435
Junio C Hamano2172ce42006-10-03 02:30:47 -0700436# checking HEAD file with -e is fragile if the repository was
437# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
438# and then pruned.
439sub check_head_link {
440 my ($dir) = @_;
441 my $headfile = "$dir/HEAD";
442 return ((-e $headfile) ||
443 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
444}
445
446sub check_export_ok {
447 my ($dir) = @_;
448 return (check_head_link($dir) &&
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300449 (!$export_ok || -e "$dir/$export_ok") &&
450 (!$export_auth_hook || $export_auth_hook->($dir)));
Junio C Hamano2172ce42006-10-03 02:30:47 -0700451}
452
Jakub Narebskia7817852007-07-22 23:41:20 +0200453# process alternate names for backward compatibility
454# filter out unsupported (unknown) snapshot formats
455sub filter_snapshot_fmts {
456 my @fmts = @_;
457
458 @fmts = map {
459 exists $known_snapshot_format_aliases{$_} ?
460 $known_snapshot_format_aliases{$_} : $_} @fmts;
461 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
462
463}
464
Junio C Hamano06c084d2006-08-02 13:50:20 -0700465our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Gerrit Pape17a8b252008-03-26 18:11:19 +0000466if (-e $GITWEB_CONFIG) {
467 do $GITWEB_CONFIG;
468} else {
469 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
470 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
471}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400472
473# version of the core git binary
Jakub Narebski66115d32008-06-14 20:37:59 +0200474our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
Jeff Kingc8d138a2006-08-02 15:23:34 -0400475
476$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400477
Jakub Narebski154b4d72006-08-05 12:55:20 +0200478# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200479# input validation and dispatch
Kay Sieversb87d78d2005-08-07 20:21:04 +0200480
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200481# input parameters can be collected from a variety of sources (presently, CGI
482# and PATH_INFO), so we define an %input_params hash that collects them all
483# together during validation: this allows subsequent uses (e.g. href()) to be
484# agnostic of the parameter origin
Kay Sievers6191f8e2005-08-07 20:19:56 +0200485
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300486our %input_params = ();
Martin Waitz5c95fab2006-08-17 00:28:38 +0200487
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200488# input parameters are stored with the long parameter name as key. This will
489# also be used in the href subroutine to convert parameters to their CGI
490# equivalent, and since the href() usage is the most frequent one, we store
491# the name -> CGI key mapping here, instead of the reverse.
492#
493# XXX: Warning: If you touch this, check the search form for updating,
494# too.
Jakub Narebski24d06932006-09-26 01:57:02 +0200495
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300496our @cgi_param_mapping = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200497 project => "p",
498 action => "a",
499 file_name => "f",
500 file_parent => "fp",
501 hash => "h",
502 hash_parent => "hp",
503 hash_base => "hb",
504 hash_parent_base => "hpb",
505 page => "pg",
506 order => "o",
507 searchtext => "s",
508 searchtype => "st",
509 snapshot_format => "sf",
510 extra_options => "opt",
511 search_use_regexp => "sr",
Miklos Vajna868bc062007-07-12 20:39:27 +0200512);
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300513our %cgi_param_mapping = @cgi_param_mapping;
Miklos Vajna868bc062007-07-12 20:39:27 +0200514
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200515# we will also need to know the possible actions, for validation
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300516our %actions = (
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +0200517 "blame" => \&git_blame,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200518 "blobdiff" => \&git_blobdiff,
519 "blobdiff_plain" => \&git_blobdiff_plain,
520 "blob" => \&git_blob,
521 "blob_plain" => \&git_blob_plain,
522 "commitdiff" => \&git_commitdiff,
523 "commitdiff_plain" => \&git_commitdiff_plain,
524 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200525 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200526 "heads" => \&git_heads,
527 "history" => \&git_history,
528 "log" => \&git_log,
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +0100529 "patch" => \&git_patch,
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +0100530 "patches" => \&git_patches,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200531 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100532 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200533 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200534 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200535 "shortlog" => \&git_shortlog,
536 "summary" => \&git_summary,
537 "tag" => \&git_tag,
538 "tags" => \&git_tags,
539 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530540 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100541 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200542 # those below don't need $project
543 "opml" => \&git_opml,
544 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200545 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200546);
547
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200548# finally, we have the hash of allowed extra_options for the commands that
549# allow them
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300550our %allowed_options = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200551 "--no-merges" => [ qw(rss atom log shortlog history) ],
552);
553
554# fill %input_params with the CGI parameters. All values except for 'opt'
555# should be single values, but opt can be an array. We should probably
556# build an array of parameters that can be multi-valued, but since for the time
557# being it's only this one, we just single it out
558while (my ($name, $symbol) = each %cgi_param_mapping) {
559 if ($symbol eq 'opt') {
560 $input_params{$name} = [ $cgi->param($symbol) ];
561 } else {
562 $input_params{$name} = $cgi->param($symbol);
563 }
564}
565
566# now read PATH_INFO and update the parameter list for missing parameters
567sub evaluate_path_info {
568 return if defined $input_params{'project'};
569 return if !$path_info;
570 $path_info =~ s,^/+,,;
571 return if !$path_info;
572
573 # find which part of PATH_INFO is project
574 my $project = $path_info;
575 $project =~ s,/+$,,;
576 while ($project && !check_head_link("$projectroot/$project")) {
577 $project =~ s,/*[^/]*$,,;
578 }
579 return unless $project;
580 $input_params{'project'} = $project;
581
582 # do not change any parameters if an action is given using the query string
583 return if $input_params{'action'};
584 $path_info =~ s,^\Q$project\E/*,,;
585
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200586 # next, check if we have an action
587 my $action = $path_info;
588 $action =~ s,/.*$,,;
589 if (exists $actions{$action}) {
590 $path_info =~ s,^$action/*,,;
591 $input_params{'action'} = $action;
592 }
593
594 # list of actions that want hash_base instead of hash, but can have no
595 # pathname (f) parameter
596 my @wants_base = (
597 'tree',
598 'history',
599 );
600
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200601 # we want to catch
602 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
603 my ($parentrefname, $parentpathname, $refname, $pathname) =
604 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
605
606 # first, analyze the 'current' part
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200607 if (defined $pathname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200608 # we got "branch:filename" or "branch:dir/"
609 # we could use git_get_type(branch:pathname), but:
610 # - it needs $git_dir
611 # - it does a git() call
612 # - the convention of terminating directories with a slash
613 # makes it superfluous
614 # - embedding the action in the PATH_INFO would make it even
615 # more superfluous
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200616 $pathname =~ s,^/+,,;
617 if (!$pathname || substr($pathname, -1) eq "/") {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200618 $input_params{'action'} ||= "tree";
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200619 $pathname =~ s,/$,,;
620 } else {
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200621 # the default action depends on whether we had parent info
622 # or not
623 if ($parentrefname) {
624 $input_params{'action'} ||= "blobdiff_plain";
625 } else {
626 $input_params{'action'} ||= "blob_plain";
627 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200628 }
629 $input_params{'hash_base'} ||= $refname;
630 $input_params{'file_name'} ||= $pathname;
631 } elsif (defined $refname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200632 # we got "branch". In this case we have to choose if we have to
633 # set hash or hash_base.
634 #
635 # Most of the actions without a pathname only want hash to be
636 # set, except for the ones specified in @wants_base that want
637 # hash_base instead. It should also be noted that hand-crafted
638 # links having 'history' as an action and no pathname or hash
639 # set will fail, but that happens regardless of PATH_INFO.
640 $input_params{'action'} ||= "shortlog";
641 if (grep { $_ eq $input_params{'action'} } @wants_base) {
642 $input_params{'hash_base'} ||= $refname;
643 } else {
644 $input_params{'hash'} ||= $refname;
645 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200646 }
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200647
648 # next, handle the 'parent' part, if present
649 if (defined $parentrefname) {
650 # a missing pathspec defaults to the 'current' filename, allowing e.g.
651 # someproject/blobdiff/oldrev..newrev:/filename
652 if ($parentpathname) {
653 $parentpathname =~ s,^/+,,;
654 $parentpathname =~ s,/$,,;
655 $input_params{'file_parent'} ||= $parentpathname;
656 } else {
657 $input_params{'file_parent'} ||= $input_params{'file_name'};
658 }
659 # we assume that hash_parent_base is wanted if a path was specified,
660 # or if the action wants hash_base instead of hash
661 if (defined $input_params{'file_parent'} ||
662 grep { $_ eq $input_params{'action'} } @wants_base) {
663 $input_params{'hash_parent_base'} ||= $parentrefname;
664 } else {
665 $input_params{'hash_parent'} ||= $parentrefname;
666 }
667 }
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100668
669 # for the snapshot action, we allow URLs in the form
670 # $project/snapshot/$hash.ext
671 # where .ext determines the snapshot and gets removed from the
672 # passed $refname to provide the $hash.
673 #
674 # To be able to tell that $refname includes the format extension, we
675 # require the following two conditions to be satisfied:
676 # - the hash input parameter MUST have been set from the $refname part
677 # of the URL (i.e. they must be equal)
678 # - the snapshot format MUST NOT have been defined already (e.g. from
679 # CGI parameter sf)
680 # It's also useless to try any matching unless $refname has a dot,
681 # so we check for that too
682 if (defined $input_params{'action'} &&
683 $input_params{'action'} eq 'snapshot' &&
684 defined $refname && index($refname, '.') != -1 &&
685 $refname eq $input_params{'hash'} &&
686 !defined $input_params{'snapshot_format'}) {
687 # We loop over the known snapshot formats, checking for
688 # extensions. Allowed extensions are both the defined suffix
689 # (which includes the initial dot already) and the snapshot
690 # format key itself, with a prepended dot
691 while (my ($fmt, %opt) = each %known_snapshot_formats) {
692 my $hash = $refname;
693 my $sfx;
694 $hash =~ s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//;
695 next unless $sfx = $1;
696 # a valid suffix was found, so set the snapshot format
697 # and reset the hash parameter
698 $input_params{'snapshot_format'} = $fmt;
699 $input_params{'hash'} = $hash;
700 # we also set the format suffix to the one requested
701 # in the URL: this way a request for e.g. .tgz returns
702 # a .tgz instead of a .tar.gz
703 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
704 last;
705 }
706 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200707}
708evaluate_path_info();
709
710our $action = $input_params{'action'};
711if (defined $action) {
712 if (!validate_action($action)) {
713 die_error(400, "Invalid action parameter");
714 }
715}
716
717# parameters which are pathnames
718our $project = $input_params{'project'};
719if (defined $project) {
720 if (!validate_project($project)) {
721 undef $project;
722 die_error(404, "No such project");
723 }
724}
725
726our $file_name = $input_params{'file_name'};
727if (defined $file_name) {
728 if (!validate_pathname($file_name)) {
729 die_error(400, "Invalid file parameter");
730 }
731}
732
733our $file_parent = $input_params{'file_parent'};
734if (defined $file_parent) {
735 if (!validate_pathname($file_parent)) {
736 die_error(400, "Invalid file parent parameter");
737 }
738}
739
740# parameters which are refnames
741our $hash = $input_params{'hash'};
742if (defined $hash) {
743 if (!validate_refname($hash)) {
744 die_error(400, "Invalid hash parameter");
745 }
746}
747
748our $hash_parent = $input_params{'hash_parent'};
749if (defined $hash_parent) {
750 if (!validate_refname($hash_parent)) {
751 die_error(400, "Invalid hash parent parameter");
752 }
753}
754
755our $hash_base = $input_params{'hash_base'};
756if (defined $hash_base) {
757 if (!validate_refname($hash_base)) {
758 die_error(400, "Invalid hash base parameter");
759 }
760}
761
762our @extra_options = @{$input_params{'extra_options'}};
763# @extra_options is always defined, since it can only be (currently) set from
764# CGI, and $cgi->param() returns the empty array in array context if the param
765# is not set
766foreach my $opt (@extra_options) {
767 if (not exists $allowed_options{$opt}) {
768 die_error(400, "Invalid option parameter");
769 }
770 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
771 die_error(400, "Invalid option parameter for this action");
772 }
773}
774
775our $hash_parent_base = $input_params{'hash_parent_base'};
776if (defined $hash_parent_base) {
777 if (!validate_refname($hash_parent_base)) {
778 die_error(400, "Invalid hash parent base parameter");
779 }
780}
781
782# other parameters
783our $page = $input_params{'page'};
784if (defined $page) {
785 if ($page =~ m/[^0-9]/) {
786 die_error(400, "Invalid page parameter");
787 }
788}
789
790our $searchtype = $input_params{'searchtype'};
791if (defined $searchtype) {
792 if ($searchtype =~ m/[^a-z]/) {
793 die_error(400, "Invalid searchtype parameter");
794 }
795}
796
797our $search_use_regexp = $input_params{'search_use_regexp'};
798
799our $searchtext = $input_params{'searchtext'};
800our $search_regexp;
801if (defined $searchtext) {
802 if (length($searchtext) < 2) {
803 die_error(403, "At least two characters are required for search parameter");
804 }
805 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
806}
807
808# path to the current git repository
809our $git_dir;
810$git_dir = "$projectroot/$project" if $project;
811
Giuseppe Bilotta5e166842008-11-02 10:21:37 +0100812# list of supported snapshot formats
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800813our @snapshot_fmts = gitweb_get_feature('snapshot');
Giuseppe Bilotta5e166842008-11-02 10:21:37 +0100814@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
815
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200816# dispatch
Gerrit Pape7f9778b2007-05-10 07:32:07 +0000817if (!defined $action) {
818 if (defined $hash) {
819 $action = git_get_type($hash);
820 } elsif (defined $hash_base && defined $file_name) {
821 $action = git_get_type("$hash_base:$file_name");
822 } elsif (defined $project) {
823 $action = 'summary';
824 } else {
825 $action = 'project_list';
826 }
Jakub Narebski77a153f2006-08-22 16:59:20 +0200827}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200828if (!defined($actions{$action})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +0200829 die_error(400, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200830}
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200831if ($action !~ m/^(opml|project_list|project_index)$/ &&
832 !$project) {
Lea Wiemann074afaa2008-06-19 22:03:21 +0200833 die_error(400, "Project needed");
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200834}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200835$actions{$action}->();
836exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200837
Jakub Narebski717b8312006-07-31 21:22:15 +0200838## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200839## action links
840
Jakub Narebski35621982008-04-20 22:09:48 +0200841sub href (%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200842 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +0100843 # default is to use -absolute url() i.e. $my_uri
844 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +0200845
Jakub Narebskiafa9b622008-02-14 09:22:30 +0100846 $params{'project'} = $project unless exists $params{'project'};
847
Jakub Narebski1cad2832007-11-01 13:06:27 +0100848 if ($params{-replay}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200849 while (my ($name, $symbol) = each %cgi_param_mapping) {
Jakub Narebski1cad2832007-11-01 13:06:27 +0100850 if (!exists $params{$name}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200851 $params{$name} = $input_params{$name};
Jakub Narebski1cad2832007-11-01 13:06:27 +0100852 }
853 }
854 }
855
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800856 my $use_pathinfo = gitweb_check_feature('pathinfo');
Giuseppe Bilottafb098a92009-01-02 12:34:40 +0100857 if ($use_pathinfo and defined $params{'project'}) {
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200858 # try to put as many parameters as possible in PATH_INFO:
859 # - project name
860 # - action
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200861 # - hash_parent or hash_parent_base:/file_parent
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +0200862 # - hash or hash_base:/filename
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100863 # - the snapshot_format as an appropriate suffix
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200864
865 # When the script is the root DirectoryIndex for the domain,
866 # $href here would be something like http://gitweb.example.com/
867 # Thus, we strip any trailing / from $href, to spare us double
868 # slashes in the final URL
869 $href =~ s,/$,,;
870
871 # Then add the project name, if present
Giuseppe Bilottafb098a92009-01-02 12:34:40 +0100872 $href .= "/".esc_url($params{'project'});
Martin Waitz9e756902006-10-01 23:57:48 +0200873 delete $params{'project'};
874
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100875 # since we destructively absorb parameters, we keep this
876 # boolean that remembers if we're handling a snapshot
877 my $is_snapshot = $params{'action'} eq 'snapshot';
878
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200879 # Summary just uses the project path URL, any other action is
880 # added to the URL
881 if (defined $params{'action'}) {
882 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
Martin Waitz9e756902006-10-01 23:57:48 +0200883 delete $params{'action'};
884 }
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200885
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200886 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
887 # stripping nonexistent or useless pieces
888 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
889 || $params{'hash_parent'} || $params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200890 if (defined $params{'hash_base'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200891 if (defined $params{'hash_parent_base'}) {
892 $href .= esc_url($params{'hash_parent_base'});
893 # skip the file_parent if it's the same as the file_name
894 delete $params{'file_parent'} if $params{'file_parent'} eq $params{'file_name'};
895 if (defined $params{'file_parent'} && $params{'file_parent'} !~ /\.\./) {
896 $href .= ":/".esc_url($params{'file_parent'});
897 delete $params{'file_parent'};
898 }
899 $href .= "..";
900 delete $params{'hash_parent'};
901 delete $params{'hash_parent_base'};
902 } elsif (defined $params{'hash_parent'}) {
903 $href .= esc_url($params{'hash_parent'}). "..";
904 delete $params{'hash_parent'};
905 }
906
907 $href .= esc_url($params{'hash_base'});
908 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +0200909 $href .= ":/".esc_url($params{'file_name'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200910 delete $params{'file_name'};
911 }
912 delete $params{'hash'};
913 delete $params{'hash_base'};
914 } elsif (defined $params{'hash'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200915 $href .= esc_url($params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200916 delete $params{'hash'};
917 }
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100918
919 # If the action was a snapshot, we can absorb the
920 # snapshot_format parameter too
921 if ($is_snapshot) {
922 my $fmt = $params{'snapshot_format'};
923 # snapshot_format should always be defined when href()
924 # is called, but just in case some code forgets, we
925 # fall back to the default
926 $fmt ||= $snapshot_fmts[0];
927 $href .= $known_snapshot_formats{$fmt}{'suffix'};
928 delete $params{'snapshot_format'};
929 }
Martin Waitz9e756902006-10-01 23:57:48 +0200930 }
931
932 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +0200933 my @result = ();
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200934 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
935 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
Jakub Narebski498fe002006-08-22 19:05:25 +0200936 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +0200937 if (ref($params{$name}) eq "ARRAY") {
938 foreach my $par (@{$params{$name}}) {
939 push @result, $symbol . "=" . esc_param($par);
940 }
941 } else {
942 push @result, $symbol . "=" . esc_param($params{$name});
943 }
Jakub Narebski498fe002006-08-22 19:05:25 +0200944 }
945 }
Martin Waitz9e756902006-10-01 23:57:48 +0200946 $href .= "?" . join(';', @result) if scalar @result;
947
948 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +0200949}
950
951
952## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200953## validation, quoting/unquoting and escaping
954
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200955sub validate_action {
956 my $input = shift || return undef;
957 return undef unless exists $actions{$input};
958 return $input;
959}
960
961sub validate_project {
962 my $input = shift || return undef;
963 if (!validate_pathname($input) ||
964 !(-d "$projectroot/$input") ||
Alexander Gavrilovec26f092008-11-06 01:15:56 +0300965 !check_export_ok("$projectroot/$input") ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200966 ($strict_export && !project_in_list($input))) {
967 return undef;
968 } else {
969 return $input;
970 }
971}
972
Jakub Narebski24d06932006-09-26 01:57:02 +0200973sub validate_pathname {
974 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +0200975
Jakub Narebski24d06932006-09-26 01:57:02 +0200976 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
977 # at the beginning, at the end, and between slashes.
978 # also this catches doubled slashes
979 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
980 return undef;
981 }
982 # no null characters
983 if ($input =~ m!\0!) {
984 return undef;
985 }
986 return $input;
987}
988
989sub validate_refname {
990 my $input = shift || return undef;
991
992 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +0200993 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
994 return $input;
995 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200996 # it must be correct pathname
997 $input = validate_pathname($input)
998 or return undef;
999 # restrictions on ref name according to git-check-ref-format
1000 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001001 return undef;
1002 }
1003 return $input;
1004}
1005
Martin Koegler00f429a2007-06-03 17:42:44 +02001006# decode sequences of octets in utf8 into Perl's internal form,
1007# which is utf-8 with utf8 flag set if needed. gitweb writes out
1008# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1009sub to_utf8 {
1010 my $str = shift;
İsmail Dönmeze5d3de52007-12-04 10:55:41 +02001011 if (utf8::valid($str)) {
1012 utf8::decode($str);
1013 return $str;
Martin Koegler00f429a2007-06-03 17:42:44 +02001014 } else {
1015 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1016 }
1017}
1018
Kay Sievers232ff552005-11-24 16:56:55 +01001019# quote unsafe chars, but keep the slash, even when it's not
1020# correct, but quoted slashes look too horrible in bookmarks
1021sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +01001022 my $str = shift;
Petr Baudisa2f3db22006-09-24 00:18:41 +02001023 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +01001024 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +01001025 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +01001026 return $str;
1027}
1028
Jakub Narebskif93bff82006-09-26 01:58:41 +02001029# quote unsafe chars in whole URL, so some charactrs cannot be quoted
1030sub esc_url {
1031 my $str = shift;
1032 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1033 $str =~ s/\+/%2B/g;
1034 $str =~ s/ /\+/g;
1035 return $str;
1036}
1037
Kay Sievers232ff552005-11-24 16:56:55 +01001038# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski6255ef02006-11-01 14:33:21 +01001039sub esc_html ($;%) {
Kay Sievers40c13812005-11-19 17:41:29 +01001040 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +01001041 my %opts = @_;
1042
Martin Koegler00f429a2007-06-03 17:42:44 +02001043 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001044 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +01001045 if ($opts{'-nbsp'}) {
1046 $str =~ s/ /&nbsp;/g;
1047 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -08001048 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +01001049 return $str;
1050}
1051
Jakub Narebski391862e2006-11-25 09:43:59 +01001052# quote control characters and escape filename to HTML
1053sub esc_path {
1054 my $str = shift;
1055 my %opts = @_;
1056
Martin Koegler00f429a2007-06-03 17:42:44 +02001057 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001058 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +01001059 if ($opts{'-nbsp'}) {
1060 $str =~ s/ /&nbsp;/g;
1061 }
1062 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1063 return $str;
1064}
1065
1066# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001067sub quot_cec {
1068 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001069 my %opts = @_;
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001070 my %es = ( # character escape codes, aka escape sequences
Jakub Narebskic84c4832008-02-17 18:48:13 +01001071 "\t" => '\t', # tab (HT)
1072 "\n" => '\n', # line feed (LF)
1073 "\r" => '\r', # carrige return (CR)
1074 "\f" => '\f', # form feed (FF)
1075 "\b" => '\b', # backspace (BS)
1076 "\a" => '\a', # alarm (bell) (BEL)
1077 "\e" => '\e', # escape (ESC)
1078 "\013" => '\v', # vertical tab (VT)
1079 "\000" => '\0', # nul character (NUL)
1080 );
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001081 my $chr = ( (exists $es{$cntrl})
1082 ? $es{$cntrl}
Petr Baudis25dfd172008-10-01 22:11:54 +02001083 : sprintf('\%2x', ord($cntrl)) );
Jakub Narebskic84c4832008-02-17 18:48:13 +01001084 if ($opts{-nohtml}) {
1085 return $chr;
1086 } else {
1087 return "<span class=\"cntrl\">$chr</span>";
1088 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001089}
1090
Jakub Narebski391862e2006-11-25 09:43:59 +01001091# Alternatively use unicode control pictures codepoints,
1092# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001093sub quot_upr {
1094 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001095 my %opts = @_;
1096
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001097 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
Jakub Narebskic84c4832008-02-17 18:48:13 +01001098 if ($opts{-nohtml}) {
1099 return $chr;
1100 } else {
1101 return "<span class=\"cntrl\">$chr</span>";
1102 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001103}
1104
Kay Sievers232ff552005-11-24 16:56:55 +01001105# git may return quoted and escaped filenames
1106sub unquote {
1107 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +01001108
1109 sub unq {
1110 my $seq = shift;
1111 my %es = ( # character escape codes, aka escape sequences
1112 't' => "\t", # tab (HT, TAB)
1113 'n' => "\n", # newline (NL)
1114 'r' => "\r", # return (CR)
1115 'f' => "\f", # form feed (FF)
1116 'b' => "\b", # backspace (BS)
1117 'a' => "\a", # alarm (bell) (BEL)
1118 'e' => "\e", # escape (ESC)
1119 'v' => "\013", # vertical tab (VT)
1120 );
1121
1122 if ($seq =~ m/^[0-7]{1,3}$/) {
1123 # octal char sequence
1124 return chr(oct($seq));
1125 } elsif (exists $es{$seq}) {
1126 # C escape sequence, aka character escape code
Jakub Narebskic84c4832008-02-17 18:48:13 +01001127 return $es{$seq};
Jakub Narebski403d0902006-11-08 11:48:56 +01001128 }
1129 # quoted ordinary character
1130 return $seq;
1131 }
1132
Kay Sievers232ff552005-11-24 16:56:55 +01001133 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +01001134 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +01001135 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +01001136 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +01001137 }
1138 return $str;
1139}
1140
Jakub Narebskif16db172006-08-06 02:08:31 +02001141# escape tabs (convert tabs to spaces)
1142sub untabify {
1143 my $line = shift;
1144
1145 while ((my $pos = index($line, "\t")) != -1) {
1146 if (my $count = (8 - ($pos % 8))) {
1147 my $spaces = ' ' x $count;
1148 $line =~ s/\t/$spaces/;
1149 }
1150 }
1151
1152 return $line;
1153}
1154
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +02001155sub project_in_list {
1156 my $project = shift;
1157 my @list = git_get_projects_list();
1158 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1159}
1160
Jakub Narebski717b8312006-07-31 21:22:15 +02001161## ----------------------------------------------------------------------
1162## HTML aware string manipulation
1163
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001164# Try to chop given string on a word boundary between position
1165# $len and $len+$add_len. If there is no word boundary there,
1166# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1167# (marking chopped part) would be longer than given string.
Jakub Narebski717b8312006-07-31 21:22:15 +02001168sub chop_str {
1169 my $str = shift;
1170 my $len = shift;
1171 my $add_len = shift || 10;
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001172 my $where = shift || 'right'; # 'left' | 'center' | 'right'
Jakub Narebski717b8312006-07-31 21:22:15 +02001173
Anders Waldenborgdee27752008-05-21 13:44:43 +02001174 # Make sure perl knows it is utf8 encoded so we don't
1175 # cut in the middle of a utf8 multibyte char.
1176 $str = to_utf8($str);
1177
Jakub Narebski717b8312006-07-31 21:22:15 +02001178 # allow only $len chars, but don't cut a word if it would fit in $add_len
1179 # 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 +01001180 # remove chopped character entities entirely
1181
1182 # when chopping in the middle, distribute $len into left and right part
1183 # return early if chopping wouldn't make string shorter
1184 if ($where eq 'center') {
1185 return $str if ($len + 5 >= length($str)); # filler is length 5
1186 $len = int($len/2);
1187 } else {
1188 return $str if ($len + 4 >= length($str)); # filler is length 4
Jakub Narebski717b8312006-07-31 21:22:15 +02001189 }
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001190
1191 # regexps: ending and beginning with word part up to $add_len
1192 my $endre = qr/.{$len}\w{0,$add_len}/;
1193 my $begre = qr/\w{0,$add_len}.{$len}/;
1194
1195 if ($where eq 'left') {
1196 $str =~ m/^(.*?)($begre)$/;
1197 my ($lead, $body) = ($1, $2);
1198 if (length($lead) > 4) {
1199 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1200 $lead = " ...";
1201 }
1202 return "$lead$body";
1203
1204 } elsif ($where eq 'center') {
1205 $str =~ m/^($endre)(.*)$/;
1206 my ($left, $str) = ($1, $2);
1207 $str =~ m/^(.*?)($begre)$/;
1208 my ($mid, $right) = ($1, $2);
1209 if (length($mid) > 5) {
1210 $left =~ s/&[^;]*$//;
1211 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1212 $mid = " ... ";
1213 }
1214 return "$left$mid$right";
1215
1216 } else {
1217 $str =~ m/^($endre)(.*)$/;
1218 my $body = $1;
1219 my $tail = $2;
1220 if (length($tail) > 4) {
1221 $body =~ s/&[^;]*$//;
1222 $tail = "... ";
1223 }
1224 return "$body$tail";
1225 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001226}
1227
David Symondsce58ec92007-10-23 11:31:22 +10001228# takes the same arguments as chop_str, but also wraps a <span> around the
1229# result with a title attribute if it does get chopped. Additionally, the
1230# string is HTML-escaped.
1231sub chop_and_escape_str {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001232 my ($str) = @_;
David Symondsce58ec92007-10-23 11:31:22 +10001233
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001234 my $chopped = chop_str(@_);
David Symondsce58ec92007-10-23 11:31:22 +10001235 if ($chopped eq $str) {
1236 return esc_html($chopped);
1237 } else {
Jakub Narebski850b90a2008-02-16 23:07:46 +01001238 $str =~ s/([[:cntrl:]])/?/g;
1239 return $cgi->span({-title=>$str}, esc_html($chopped));
David Symondsce58ec92007-10-23 11:31:22 +10001240 }
1241}
1242
Jakub Narebski717b8312006-07-31 21:22:15 +02001243## ----------------------------------------------------------------------
1244## functions returning short strings
1245
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001246# CSS class for given age value (in seconds)
1247sub age_class {
1248 my $age = shift;
1249
Jakub Narebski785cdea2007-05-13 12:39:22 +02001250 if (!defined $age) {
1251 return "noage";
1252 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001253 return "age0";
1254 } elsif ($age < 60*60*24*2) {
1255 return "age1";
1256 } else {
1257 return "age2";
1258 }
1259}
1260
Jakub Narebski717b8312006-07-31 21:22:15 +02001261# convert age in seconds to "nn units ago" string
1262sub age_string {
1263 my $age = shift;
1264 my $age_str;
1265
1266 if ($age > 60*60*24*365*2) {
1267 $age_str = (int $age/60/60/24/365);
1268 $age_str .= " years ago";
1269 } elsif ($age > 60*60*24*(365/12)*2) {
1270 $age_str = int $age/60/60/24/(365/12);
1271 $age_str .= " months ago";
1272 } elsif ($age > 60*60*24*7*2) {
1273 $age_str = int $age/60/60/24/7;
1274 $age_str .= " weeks ago";
1275 } elsif ($age > 60*60*24*2) {
1276 $age_str = int $age/60/60/24;
1277 $age_str .= " days ago";
1278 } elsif ($age > 60*60*2) {
1279 $age_str = int $age/60/60;
1280 $age_str .= " hours ago";
1281 } elsif ($age > 60*2) {
1282 $age_str = int $age/60;
1283 $age_str .= " min ago";
1284 } elsif ($age > 2) {
1285 $age_str = int $age;
1286 $age_str .= " sec ago";
1287 } else {
1288 $age_str .= " right now";
1289 }
1290 return $age_str;
1291}
1292
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001293use constant {
1294 S_IFINVALID => 0030000,
1295 S_IFGITLINK => 0160000,
1296};
1297
1298# submodule/subproject, a commit object reference
1299sub S_ISGITLINK($) {
1300 my $mode = shift;
1301
1302 return (($mode & S_IFMT) == S_IFGITLINK)
1303}
1304
Jakub Narebski717b8312006-07-31 21:22:15 +02001305# convert file mode in octal to symbolic file mode string
1306sub mode_str {
1307 my $mode = oct shift;
1308
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001309 if (S_ISGITLINK($mode)) {
1310 return 'm---------';
1311 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001312 return 'drwxr-xr-x';
1313 } elsif (S_ISLNK($mode)) {
1314 return 'lrwxrwxrwx';
1315 } elsif (S_ISREG($mode)) {
1316 # git cares only about the executable bit
1317 if ($mode & S_IXUSR) {
1318 return '-rwxr-xr-x';
1319 } else {
1320 return '-rw-r--r--';
1321 };
1322 } else {
1323 return '----------';
1324 }
1325}
1326
1327# convert file mode in octal to file type string
1328sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001329 my $mode = shift;
1330
1331 if ($mode !~ m/^[0-7]+$/) {
1332 return $mode;
1333 } else {
1334 $mode = oct $mode;
1335 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001336
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001337 if (S_ISGITLINK($mode)) {
1338 return "submodule";
1339 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001340 return "directory";
1341 } elsif (S_ISLNK($mode)) {
1342 return "symlink";
1343 } elsif (S_ISREG($mode)) {
1344 return "file";
1345 } else {
1346 return "unknown";
1347 }
1348}
1349
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001350# convert file mode in octal to file type description string
1351sub file_type_long {
1352 my $mode = shift;
1353
1354 if ($mode !~ m/^[0-7]+$/) {
1355 return $mode;
1356 } else {
1357 $mode = oct $mode;
1358 }
1359
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001360 if (S_ISGITLINK($mode)) {
1361 return "submodule";
1362 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001363 return "directory";
1364 } elsif (S_ISLNK($mode)) {
1365 return "symlink";
1366 } elsif (S_ISREG($mode)) {
1367 if ($mode & S_IXUSR) {
1368 return "executable";
1369 } else {
1370 return "file";
1371 };
1372 } else {
1373 return "unknown";
1374 }
1375}
1376
1377
Jakub Narebski717b8312006-07-31 21:22:15 +02001378## ----------------------------------------------------------------------
1379## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05001380## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02001381
Junio C Hamano225932e2006-11-09 00:57:13 -08001382# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02001383sub format_log_line_html {
1384 my $line = shift;
1385
Junio C Hamano225932e2006-11-09 00:57:13 -08001386 $line = esc_html($line, -nbsp=>1);
Jakub Narebskiccb04f92009-02-06 10:12:41 +01001387 if ($line =~ m/\b([0-9a-fA-F]{8,40})\b/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001388 my $hash_text = $1;
Jakub Narebskibfe21912006-12-10 13:25:49 +01001389 my $link =
1390 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1391 -class => "text"}, $hash_text);
1392 $line =~ s/$hash_text/$link/;
Jakub Narebski717b8312006-07-31 21:22:15 +02001393 }
1394 return $line;
1395}
1396
1397# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001398
1399# the destination action is chosen based on object type and current context:
1400# - for annotated tags, we choose the tag view unless it's the current view
1401# already, in which case we go to shortlog view
1402# - for other refs, we keep the current view if we're in history, shortlog or
1403# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02001404sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02001405 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001406 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02001407
1408 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001409 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001410 # this code exploits the fact that non-lightweight tags are the
1411 # only indirect objects, and that they are the only objects for which
1412 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001413 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001414 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001415 # e.g. tags/v2.6.11 or heads/next
1416 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1417 $type = $1;
1418 $name = $2;
1419 } else {
1420 $type = "ref";
1421 $name = $ref;
1422 }
1423
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001424 my $class = $type;
1425 $class .= " indirect" if $indirect;
1426
1427 my $dest_action = "shortlog";
1428
1429 if ($indirect) {
1430 $dest_action = "tag" unless $action eq "tag";
1431 } elsif ($action =~ /^(history|(short)?log)$/) {
1432 $dest_action = $action;
1433 }
1434
1435 my $dest = "";
1436 $dest .= "refs/" unless $ref =~ m!^refs/!;
1437 $dest .= $ref;
1438
1439 my $link = $cgi->a({
1440 -href => href(
1441 action=>$dest_action,
1442 hash=>$dest
1443 )}, $name);
1444
1445 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1446 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001447 }
1448 }
1449
1450 if ($markers) {
1451 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02001452 } else {
1453 return "";
1454 }
1455}
1456
Jakub Narebski17d07442006-08-14 02:08:27 +02001457# format, perhaps shortened and with markers, title line
1458sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001459 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02001460 $extra = '' unless defined($extra);
1461
1462 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +02001463 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02001464 -title => to_utf8($long)},
Jakub Narebski17d07442006-08-14 02:08:27 +02001465 esc_html($short) . $extra);
1466 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02001467 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +02001468 esc_html($long) . $extra);
1469 }
1470}
1471
Jakub Narebski90921742007-06-08 13:27:42 +02001472# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1473sub format_git_diff_header_line {
1474 my $line = shift;
1475 my $diffinfo = shift;
1476 my ($from, $to) = @_;
1477
1478 if ($diffinfo->{'nparents'}) {
1479 # combined diff
1480 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1481 if ($to->{'href'}) {
1482 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1483 esc_path($to->{'file'}));
1484 } else { # file was deleted (no href)
1485 $line .= esc_path($to->{'file'});
1486 }
1487 } else {
1488 # "ordinary" diff
1489 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1490 if ($from->{'href'}) {
1491 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1492 'a/' . esc_path($from->{'file'}));
1493 } else { # file was added (no href)
1494 $line .= 'a/' . esc_path($from->{'file'});
1495 }
1496 $line .= ' ';
1497 if ($to->{'href'}) {
1498 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1499 'b/' . esc_path($to->{'file'}));
1500 } else { # file was deleted
1501 $line .= 'b/' . esc_path($to->{'file'});
1502 }
1503 }
1504
1505 return "<div class=\"diff header\">$line</div>\n";
1506}
1507
1508# format extended diff header line, before patch itself
1509sub format_extended_diff_header_line {
1510 my $line = shift;
1511 my $diffinfo = shift;
1512 my ($from, $to) = @_;
1513
1514 # match <path>
1515 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1516 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1517 esc_path($from->{'file'}));
1518 }
1519 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1520 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1521 esc_path($to->{'file'}));
1522 }
1523 # match single <mode>
1524 if ($line =~ m/\s(\d{6})$/) {
1525 $line .= '<span class="info"> (' .
1526 file_type_long($1) .
1527 ')</span>';
1528 }
1529 # match <hash>
1530 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1531 # can match only for combined diff
1532 $line = 'index ';
1533 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1534 if ($from->{'href'}[$i]) {
1535 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1536 -class=>"hash"},
1537 substr($diffinfo->{'from_id'}[$i],0,7));
1538 } else {
1539 $line .= '0' x 7;
1540 }
1541 # separator
1542 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1543 }
1544 $line .= '..';
1545 if ($to->{'href'}) {
1546 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1547 substr($diffinfo->{'to_id'},0,7));
1548 } else {
1549 $line .= '0' x 7;
1550 }
1551
1552 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1553 # can match only for ordinary diff
1554 my ($from_link, $to_link);
1555 if ($from->{'href'}) {
1556 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1557 substr($diffinfo->{'from_id'},0,7));
1558 } else {
1559 $from_link = '0' x 7;
1560 }
1561 if ($to->{'href'}) {
1562 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1563 substr($diffinfo->{'to_id'},0,7));
1564 } else {
1565 $to_link = '0' x 7;
1566 }
1567 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1568 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1569 }
1570
1571 return $line . "<br/>\n";
1572}
1573
1574# format from-file/to-file diff header
1575sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001576 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02001577 my $line;
1578 my $result = '';
1579
1580 $line = $from_line;
1581 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001582 # no extra formatting for "^--- /dev/null"
1583 if (! $diffinfo->{'nparents'}) {
1584 # ordinary (single parent) diff
1585 if ($line =~ m!^--- "?a/!) {
1586 if ($from->{'href'}) {
1587 $line = '--- a/' .
1588 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1589 esc_path($from->{'file'}));
1590 } else {
1591 $line = '--- a/' .
1592 esc_path($from->{'file'});
1593 }
1594 }
1595 $result .= qq!<div class="diff from_file">$line</div>\n!;
1596
1597 } else {
1598 # combined diff (merge commit)
1599 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1600 if ($from->{'href'}[$i]) {
1601 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001602 $cgi->a({-href=>href(action=>"blobdiff",
1603 hash_parent=>$diffinfo->{'from_id'}[$i],
1604 hash_parent_base=>$parents[$i],
1605 file_parent=>$from->{'file'}[$i],
1606 hash=>$diffinfo->{'to_id'},
1607 hash_base=>$hash,
1608 file_name=>$to->{'file'}),
1609 -class=>"path",
1610 -title=>"diff" . ($i+1)},
1611 $i+1) .
1612 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001613 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1614 esc_path($from->{'file'}[$i]));
1615 } else {
1616 $line = '--- /dev/null';
1617 }
1618 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02001619 }
1620 }
Jakub Narebski90921742007-06-08 13:27:42 +02001621
1622 $line = $to_line;
1623 #assert($line =~ m/^\+\+\+/) if DEBUG;
1624 # no extra formatting for "^+++ /dev/null"
1625 if ($line =~ m!^\+\+\+ "?b/!) {
1626 if ($to->{'href'}) {
1627 $line = '+++ b/' .
1628 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1629 esc_path($to->{'file'}));
1630 } else {
1631 $line = '+++ b/' .
1632 esc_path($to->{'file'});
1633 }
1634 }
1635 $result .= qq!<div class="diff to_file">$line</div>\n!;
1636
1637 return $result;
1638}
1639
Jakub Narebskicd030c32007-06-08 13:33:28 +02001640# create note for patch simplified by combined diff
1641sub format_diff_cc_simplified {
1642 my ($diffinfo, @parents) = @_;
1643 my $result = '';
1644
1645 $result .= "<div class=\"diff header\">" .
1646 "diff --cc ";
1647 if (!is_deleted($diffinfo)) {
1648 $result .= $cgi->a({-href => href(action=>"blob",
1649 hash_base=>$hash,
1650 hash=>$diffinfo->{'to_id'},
1651 file_name=>$diffinfo->{'to_file'}),
1652 -class => "path"},
1653 esc_path($diffinfo->{'to_file'}));
1654 } else {
1655 $result .= esc_path($diffinfo->{'to_file'});
1656 }
1657 $result .= "</div>\n" . # class="diff header"
1658 "<div class=\"diff nodifferences\">" .
1659 "Simple merge" .
1660 "</div>\n"; # class="diff nodifferences"
1661
1662 return $result;
1663}
1664
Jakub Narebski90921742007-06-08 13:27:42 +02001665# format patch (diff) line (not to be used for diff headers)
Jakub Narebskieee08902006-08-24 00:15:14 +02001666sub format_diff_line {
1667 my $line = shift;
Jakub Narebski59e3b142006-11-18 23:35:40 +01001668 my ($from, $to) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001669 my $diff_class = "";
1670
1671 chomp $line;
1672
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001673 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1674 # combined diff
1675 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1676 if ($line =~ m/^\@{3}/) {
1677 $diff_class = " chunk_header";
1678 } elsif ($line =~ m/^\\/) {
1679 $diff_class = " incomplete";
1680 } elsif ($prefix =~ tr/+/+/) {
1681 $diff_class = " add";
1682 } elsif ($prefix =~ tr/-/-/) {
1683 $diff_class = " rem";
1684 }
1685 } else {
1686 # assume ordinary diff
1687 my $char = substr($line, 0, 1);
1688 if ($char eq '+') {
1689 $diff_class = " add";
1690 } elsif ($char eq '-') {
1691 $diff_class = " rem";
1692 } elsif ($char eq '@') {
1693 $diff_class = " chunk_header";
1694 } elsif ($char eq "\\") {
1695 $diff_class = " incomplete";
1696 }
Jakub Narebskieee08902006-08-24 00:15:14 +02001697 }
1698 $line = untabify($line);
Jakub Narebski59e3b142006-11-18 23:35:40 +01001699 if ($from && $to && $line =~ m/^\@{2} /) {
1700 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1701 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1702
1703 $from_lines = 0 unless defined $from_lines;
1704 $to_lines = 0 unless defined $to_lines;
1705
1706 if ($from->{'href'}) {
1707 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1708 -class=>"list"}, $from_text);
1709 }
1710 if ($to->{'href'}) {
1711 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1712 -class=>"list"}, $to_text);
1713 }
1714 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1715 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1716 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001717 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1718 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1719 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1720
1721 @from_text = split(' ', $ranges);
1722 for (my $i = 0; $i < @from_text; ++$i) {
1723 ($from_start[$i], $from_nlines[$i]) =
1724 (split(',', substr($from_text[$i], 1)), 0);
1725 }
1726
1727 $to_text = pop @from_text;
1728 $to_start = pop @from_start;
1729 $to_nlines = pop @from_nlines;
1730
1731 $line = "<span class=\"chunk_info\">$prefix ";
1732 for (my $i = 0; $i < @from_text; ++$i) {
1733 if ($from->{'href'}[$i]) {
1734 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1735 -class=>"list"}, $from_text[$i]);
1736 } else {
1737 $line .= $from_text[$i];
1738 }
1739 $line .= " ";
1740 }
1741 if ($to->{'href'}) {
1742 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1743 -class=>"list"}, $to_text);
1744 } else {
1745 $line .= $to_text;
1746 }
1747 $line .= " $prefix</span>" .
1748 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1749 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebski59e3b142006-11-18 23:35:40 +01001750 }
Jakub Narebski6255ef02006-11-01 14:33:21 +01001751 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001752}
1753
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001754# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1755# linked. Pass the hash of the tree/commit to snapshot.
1756sub format_snapshot_links {
1757 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001758 my $num_fmts = @snapshot_fmts;
1759 if ($num_fmts > 1) {
1760 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02001761 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001762 return "snapshot (" . join(' ', map
1763 $cgi->a({
1764 -href => href(
1765 action=>"snapshot",
1766 hash=>$hash,
1767 snapshot_format=>$_
1768 )
1769 }, $known_snapshot_formats{$_}{'display'})
1770 , @snapshot_fmts) . ")";
1771 } elsif ($num_fmts == 1) {
1772 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02001773 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001774 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02001775 return
1776 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001777 -href => href(
1778 action=>"snapshot",
1779 hash=>$hash,
1780 snapshot_format=>$fmt
1781 ),
1782 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1783 }, "snapshot");
1784 } else { # $num_fmts == 0
1785 return undef;
1786 }
1787}
1788
Jakub Narebski35621982008-04-20 22:09:48 +02001789## ......................................................................
1790## functions returning values to be passed, perhaps after some
1791## transformation, to other functions; e.g. returning arguments to href()
1792
1793# returns hash to be passed to href to generate gitweb URL
1794# in -title key it returns description of link
1795sub get_feed_info {
1796 my $format = shift || 'Atom';
1797 my %res = (action => lc($format));
1798
1799 # feed links are possible only for project views
1800 return unless (defined $project);
1801 # some views should link to OPML, or to generic project feed,
1802 # or don't have specific feed yet (so they should use generic)
1803 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1804
1805 my $branch;
1806 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1807 # from tag links; this also makes possible to detect branch links
1808 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1809 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1810 $branch = $1;
1811 }
1812 # find log type for feed description (title)
1813 my $type = 'log';
1814 if (defined $file_name) {
1815 $type = "history of $file_name";
1816 $type .= "/" if ($action eq 'tree');
1817 $type .= " on '$branch'" if (defined $branch);
1818 } else {
1819 $type = "log of $branch" if (defined $branch);
1820 }
1821
1822 $res{-title} = $type;
1823 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1824 $res{'file_name'} = $file_name;
1825
1826 return %res;
1827}
1828
Jakub Narebski717b8312006-07-31 21:22:15 +02001829## ----------------------------------------------------------------------
1830## git utility subroutines, invoking git commands
1831
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001832# returns path to the core git executable and the --git-dir parameter as list
1833sub git_cmd {
1834 return $GIT, '--git-dir='.$git_dir;
1835}
1836
Lea Wiemann516381d2008-06-17 23:46:35 +02001837# quote the given arguments for passing them to the shell
1838# quote_command("command", "arg 1", "arg with ' and ! characters")
1839# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1840# Try to avoid using this function wherever possible.
1841sub quote_command {
1842 return join(' ',
1843 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001844}
1845
Jakub Narebski717b8312006-07-31 21:22:15 +02001846# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02001847sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +02001848 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001849 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001850 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001851 $git_dir = "$projectroot/$project";
1852 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +02001853 my $head = <$fd>;
1854 close $fd;
1855 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1856 $retval = $1;
1857 }
1858 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001859 if (defined $o_git_dir) {
1860 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001861 }
1862 return $retval;
1863}
1864
1865# get type of given object
1866sub git_get_type {
1867 my $hash = shift;
1868
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001869 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001870 my $type = <$fd>;
1871 close $fd or return;
1872 chomp $type;
1873 return $type;
1874}
1875
Jakub Narebskib2019272007-11-03 00:41:19 +01001876# repository configuration
1877our $config_file = '';
1878our %config;
1879
1880# store multiple values for single key as anonymous array reference
1881# single values stored directly in the hash, not as [ <value> ]
1882sub hash_set_multi {
1883 my ($hash, $key, $value) = @_;
1884
1885 if (!exists $hash->{$key}) {
1886 $hash->{$key} = $value;
1887 } elsif (!ref $hash->{$key}) {
1888 $hash->{$key} = [ $hash->{$key}, $value ];
1889 } else {
1890 push @{$hash->{$key}}, $value;
1891 }
1892}
1893
1894# return hash of git project configuration
1895# optionally limited to some section, e.g. 'gitweb'
1896sub git_parse_project_config {
1897 my $section_regexp = shift;
1898 my %config;
1899
1900 local $/ = "\0";
1901
1902 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1903 or return;
1904
1905 while (my $keyval = <$fh>) {
1906 chomp $keyval;
1907 my ($key, $value) = split(/\n/, $keyval, 2);
1908
1909 hash_set_multi(\%config, $key, $value)
1910 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1911 }
1912 close $fh;
1913
1914 return %config;
1915}
1916
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01001917# convert config value to boolean: 'true' or 'false'
Jakub Narebskib2019272007-11-03 00:41:19 +01001918# no value, number > 0, 'true' and 'yes' values are true
1919# rest of values are treated as false (never as error)
1920sub config_to_bool {
1921 my $val = shift;
1922
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01001923 return 1 if !defined $val; # section.key
1924
Jakub Narebskib2019272007-11-03 00:41:19 +01001925 # strip leading and trailing whitespace
1926 $val =~ s/^\s+//;
1927 $val =~ s/\s+$//;
1928
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01001929 return (($val =~ /^\d+$/ && $val) || # section.key = 1
Jakub Narebskib2019272007-11-03 00:41:19 +01001930 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1931}
1932
1933# convert config value to simple decimal number
1934# an optional value suffix of 'k', 'm', or 'g' will cause the value
1935# to be multiplied by 1024, 1048576, or 1073741824
1936sub config_to_int {
1937 my $val = shift;
1938
1939 # strip leading and trailing whitespace
1940 $val =~ s/^\s+//;
1941 $val =~ s/\s+$//;
1942
1943 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1944 $unit = lc($unit);
1945 # unknown unit is treated as 1
1946 return $num * ($unit eq 'g' ? 1073741824 :
1947 $unit eq 'm' ? 1048576 :
1948 $unit eq 'k' ? 1024 : 1);
1949 }
1950 return $val;
1951}
1952
1953# convert config value to array reference, if needed
1954sub config_to_multi {
1955 my $val = shift;
1956
Jakub Narebskid76a5852007-12-20 10:48:09 +01001957 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01001958}
1959
Jakub Narebski717b8312006-07-31 21:22:15 +02001960sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301961 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001962
Jakub Narebskib2019272007-11-03 00:41:19 +01001963 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02001964 return unless ($key);
1965 $key =~ s/^gitweb\.//;
1966 return if ($key =~ m/\W/);
1967
Jakub Narebskib2019272007-11-03 00:41:19 +01001968 # type sanity check
1969 if (defined $type) {
1970 $type =~ s/^--//;
1971 $type = undef
1972 unless ($type eq 'bool' || $type eq 'int');
1973 }
1974
1975 # get config
1976 if (!defined $config_file ||
1977 $config_file ne "$git_dir/config") {
1978 %config = git_parse_project_config('gitweb');
1979 $config_file = "$git_dir/config";
1980 }
1981
Marcel M. Carydf5d10a2009-02-18 14:09:41 +01001982 # check if config variable (key) exists
1983 return unless exists $config{"gitweb.$key"};
1984
Jakub Narebskib2019272007-11-03 00:41:19 +01001985 # ensure given type
1986 if (!defined $type) {
1987 return $config{"gitweb.$key"};
1988 } elsif ($type eq 'bool') {
1989 # backward compatibility: 'git config --bool' returns true/false
1990 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1991 } elsif ($type eq 'int') {
1992 return config_to_int($config{"gitweb.$key"});
1993 }
1994 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02001995}
1996
Jakub Narebski717b8312006-07-31 21:22:15 +02001997# get hash of given path at given ref
1998sub git_get_hash_by_path {
1999 my $base = shift;
2000 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002001 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02002002
Jakub Narebski4b02f482006-09-26 01:54:24 +02002003 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02002004
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002005 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02002006 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02002007 my $line = <$fd>;
2008 close $fd or return undef;
2009
Jakub Narebski198a2a82007-05-12 21:16:34 +02002010 if (!defined $line) {
2011 # there is no tree or hash given by $path at $base
2012 return undef;
2013 }
2014
Jakub Narebski717b8312006-07-31 21:22:15 +02002015 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002016 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02002017 if (defined $type && $type ne $2) {
2018 # type doesn't match
2019 return undef;
2020 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002021 return $3;
2022}
2023
Jakub Narebskied224de2007-05-07 01:10:04 +02002024# get path of entry with given hash at given tree-ish (ref)
2025# used to get 'from' filename for combined diff (merge commit) for renames
2026sub git_get_path_by_hash {
2027 my $base = shift || return;
2028 my $hash = shift || return;
2029
2030 local $/ = "\0";
2031
2032 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2033 or return undef;
2034 while (my $line = <$fd>) {
2035 chomp $line;
2036
2037 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2038 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2039 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2040 close $fd;
2041 return $1;
2042 }
2043 }
2044 close $fd;
2045 return undef;
2046}
2047
Jakub Narebski717b8312006-07-31 21:22:15 +02002048## ......................................................................
2049## git utility functions, directly accessing git repository
2050
Jakub Narebski847e01f2006-08-14 02:05:47 +02002051sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +02002052 my $path = shift;
2053
Jakub Narebski0e121a22007-11-03 00:41:20 +01002054 $git_dir = "$projectroot/$path";
Bruno Ribasc1dcf7e2008-01-30 03:37:56 -02002055 open my $fd, "$git_dir/description"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002056 or return git_get_project_config('description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002057 my $descr = <$fd>;
2058 close $fd;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002059 if (defined $descr) {
2060 chomp $descr;
2061 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002062 return $descr;
2063}
2064
Petr Baudisaed93de2008-10-02 17:13:02 +02002065sub git_get_project_ctags {
2066 my $path = shift;
2067 my $ctags = {};
2068
2069 $git_dir = "$projectroot/$path";
Junio C Hamanoeee01842008-10-14 21:27:12 -07002070 unless (opendir D, "$git_dir/ctags") {
2071 return $ctags;
2072 }
2073 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir(D)) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002074 open CT, $_ or next;
2075 my $val = <CT>;
2076 chomp $val;
2077 close CT;
2078 my $ctag = $_; $ctag =~ s#.*/##;
2079 $ctags->{$ctag} = $val;
2080 }
Junio C Hamanoeee01842008-10-14 21:27:12 -07002081 closedir D;
Petr Baudisaed93de2008-10-02 17:13:02 +02002082 $ctags;
2083}
2084
2085sub git_populate_project_tagcloud {
2086 my $ctags = shift;
2087
2088 # First, merge different-cased tags; tags vote on casing
2089 my %ctags_lc;
2090 foreach (keys %$ctags) {
2091 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2092 if (not $ctags_lc{lc $_}->{topcount}
2093 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2094 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2095 $ctags_lc{lc $_}->{topname} = $_;
2096 }
2097 }
2098
2099 my $cloud;
2100 if (eval { require HTML::TagCloud; 1; }) {
2101 $cloud = HTML::TagCloud->new;
2102 foreach (sort keys %ctags_lc) {
2103 # Pad the title with spaces so that the cloud looks
2104 # less crammed.
2105 my $title = $ctags_lc{$_}->{topname};
2106 $title =~ s/ /&nbsp;/g;
2107 $title =~ s/^/&nbsp;/g;
2108 $title =~ s/$/&nbsp;/g;
2109 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2110 }
2111 } else {
2112 $cloud = \%ctags_lc;
2113 }
2114 $cloud;
2115}
2116
2117sub git_show_project_tagcloud {
2118 my ($cloud, $count) = @_;
2119 print STDERR ref($cloud)."..\n";
2120 if (ref $cloud eq 'HTML::TagCloud') {
2121 return $cloud->html_and_css($count);
2122 } else {
2123 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2124 return '<p align="center">' . join (', ', map {
2125 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2126 } splice(@tags, 0, $count)) . '</p>';
2127 }
2128}
2129
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002130sub git_get_project_url_list {
2131 my $path = shift;
2132
Jakub Narebski0e121a22007-11-03 00:41:20 +01002133 $git_dir = "$projectroot/$path";
Bruno Ribas201945e2008-02-06 15:15:12 -02002134 open my $fd, "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002135 or return wantarray ?
2136 @{ config_to_multi(git_get_project_config('url')) } :
2137 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002138 my @git_project_url_list = map { chomp; $_ } <$fd>;
2139 close $fd;
2140
2141 return wantarray ? @git_project_url_list : \@git_project_url_list;
2142}
2143
Jakub Narebski847e01f2006-08-14 02:05:47 +02002144sub git_get_projects_list {
Petr Baudise30496d2006-10-24 05:33:17 +02002145 my ($filter) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002146 my @list;
2147
Petr Baudise30496d2006-10-24 05:33:17 +02002148 $filter ||= '';
2149 $filter =~ s/\.git$//;
2150
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08002151 my $check_forks = gitweb_check_feature('forks');
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002152
Jakub Narebski717b8312006-07-31 21:22:15 +02002153 if (-d $projects_list) {
2154 # search in directory
Petr Baudise30496d2006-10-24 05:33:17 +02002155 my $dir = $projects_list . ($filter ? "/$filter" : '');
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05302156 # remove the trailing "/"
2157 $dir =~ s!/+$!!;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002158 my $pfxlen = length("$dir");
Luke Luca5e9492007-10-16 20:45:25 -07002159 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002160
2161 File::Find::find({
2162 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07002163 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002164 dangling_symlinks => 0, # ignore dangling symlinks, silently
2165 wanted => sub {
2166 # skip project-list toplevel, if we get it.
2167 return if (m!^[/.]$!);
2168 # only directories can be git repositories
2169 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07002170 # don't traverse too deep (Find is super slow on os x)
2171 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2172 $File::Find::prune = 1;
2173 return;
2174 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002175
2176 my $subdir = substr($File::Find::name, $pfxlen + 1);
2177 # we check related file in $projectroot
Devin Doucettefb3bb3d2008-12-27 02:39:31 -07002178 my $path = ($filter ? "$filter/" : '') . $subdir;
2179 if (check_export_ok("$projectroot/$path")) {
2180 push @list, { path => $path };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002181 $File::Find::prune = 1;
2182 }
2183 },
2184 }, "$dir");
2185
Jakub Narebski717b8312006-07-31 21:22:15 +02002186 } elsif (-f $projects_list) {
2187 # read from file(url-encoded):
2188 # 'git%2Fgit.git Linus+Torvalds'
2189 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2190 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002191 my %paths;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +02002192 open my ($fd), $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002193 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02002194 while (my $line = <$fd>) {
2195 chomp $line;
2196 my ($path, $owner) = split ' ', $line;
2197 $path = unescape($path);
2198 $owner = unescape($owner);
2199 if (!defined $path) {
2200 next;
2201 }
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002202 if ($filter ne '') {
2203 # looking for forks;
2204 my $pfx = substr($path, 0, length($filter));
2205 if ($pfx ne $filter) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002206 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002207 }
2208 my $sfx = substr($path, length($filter));
2209 if ($sfx !~ /^\/.*\.git$/) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002210 next PROJECT;
2211 }
2212 } elsif ($check_forks) {
2213 PATH:
2214 foreach my $filter (keys %paths) {
2215 # looking for forks;
2216 my $pfx = substr($path, 0, length($filter));
2217 if ($pfx ne $filter) {
2218 next PATH;
2219 }
2220 my $sfx = substr($path, length($filter));
2221 if ($sfx !~ /^\/.*\.git$/) {
2222 next PATH;
2223 }
2224 # is a fork, don't include it in
2225 # the list
2226 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002227 }
2228 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07002229 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002230 my $pr = {
2231 path => $path,
Martin Koegler00f429a2007-06-03 17:42:44 +02002232 owner => to_utf8($owner),
Jakub Narebski717b8312006-07-31 21:22:15 +02002233 };
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002234 push @list, $pr;
2235 (my $forks_path = $path) =~ s/\.git$//;
2236 $paths{$forks_path}++;
Jakub Narebski717b8312006-07-31 21:22:15 +02002237 }
2238 }
2239 close $fd;
2240 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002241 return @list;
2242}
2243
Junio C Hamano47852452007-07-03 22:10:42 -07002244our $gitweb_project_owner = undef;
2245sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002246
Junio C Hamano47852452007-07-03 22:10:42 -07002247 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002248
Junio C Hamano47852452007-07-03 22:10:42 -07002249 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002250 # read from file (url-encoded):
2251 # 'git%2Fgit.git Linus+Torvalds'
2252 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2253 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2254 if (-f $projects_list) {
2255 open (my $fd , $projects_list);
2256 while (my $line = <$fd>) {
2257 chomp $line;
2258 my ($pr, $ow) = split ' ', $line;
2259 $pr = unescape($pr);
2260 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07002261 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002262 }
2263 close $fd;
2264 }
Junio C Hamano47852452007-07-03 22:10:42 -07002265}
2266
2267sub git_get_project_owner {
2268 my $project = shift;
2269 my $owner;
2270
2271 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02002272 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07002273
2274 if (!defined $gitweb_project_owner) {
2275 git_get_project_list_from_file();
2276 }
2277
2278 if (exists $gitweb_project_owner->{$project}) {
2279 $owner = $gitweb_project_owner->{$project};
2280 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02002281 if (!defined $owner){
2282 $owner = git_get_project_config('owner');
2283 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002284 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02002285 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002286 }
2287
2288 return $owner;
2289}
2290
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002291sub git_get_last_activity {
2292 my ($path) = @_;
2293 my $fd;
2294
2295 $git_dir = "$projectroot/$path";
2296 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00002297 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002298 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00002299 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002300 'refs/heads') or return;
2301 my $most_recent = <$fd>;
2302 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02002303 if (defined $most_recent &&
2304 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002305 my $timestamp = $1;
2306 my $age = time - $timestamp;
2307 return ($age, age_string($age));
2308 }
Matt McCutchenc9563952007-06-28 18:15:22 -04002309 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002310}
2311
Jakub Narebski847e01f2006-08-14 02:05:47 +02002312sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02002313 my $type = shift || "";
2314 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01002315 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2316 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2317 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2318 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02002319 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002320
Jakub Narebski717b8312006-07-31 21:22:15 +02002321 while (my $line = <$fd>) {
2322 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002323 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002324 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002325 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02002326 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002327 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02002328 }
2329 }
2330 }
2331 close $fd or return;
2332 return \%refs;
2333}
2334
Jakub Narebski56a322f2006-08-24 19:41:23 +02002335sub git_get_rev_name_tags {
2336 my $hash = shift || return undef;
2337
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002338 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02002339 or return;
2340 my $name_rev = <$fd>;
2341 close $fd;
2342
2343 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2344 return $1;
2345 } else {
2346 # catches also '$hash undefined' output
2347 return undef;
2348 }
2349}
2350
Jakub Narebski717b8312006-07-31 21:22:15 +02002351## ----------------------------------------------------------------------
2352## parse to hash functions
2353
Jakub Narebski847e01f2006-08-14 02:05:47 +02002354sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02002355 my $epoch = shift;
2356 my $tz = shift || "-0000";
2357
2358 my %date;
2359 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2360 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2361 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2362 $date{'hour'} = $hour;
2363 $date{'minute'} = $min;
2364 $date{'mday'} = $mday;
2365 $date{'day'} = $days[$wday];
2366 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002367 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2368 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02002369 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2370 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002371 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01002372 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02002373
2374 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2375 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2376 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2377 $date{'hour_local'} = $hour;
2378 $date{'minute_local'} = $min;
2379 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002380 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2381 1900+$year, $mon+1, $mday,
2382 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02002383 return %date;
2384}
2385
Jakub Narebski847e01f2006-08-14 02:05:47 +02002386sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02002387 my $tag_id = shift;
2388 my %tag;
2389 my @comment;
2390
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002391 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002392 $tag{'id'} = $tag_id;
2393 while (my $line = <$fd>) {
2394 chomp $line;
2395 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2396 $tag{'object'} = $1;
2397 } elsif ($line =~ m/^type (.+)$/) {
2398 $tag{'type'} = $1;
2399 } elsif ($line =~ m/^tag (.+)$/) {
2400 $tag{'name'} = $1;
2401 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2402 $tag{'author'} = $1;
2403 $tag{'epoch'} = $2;
2404 $tag{'tz'} = $3;
2405 } elsif ($line =~ m/--BEGIN/) {
2406 push @comment, $line;
2407 last;
2408 } elsif ($line eq "") {
2409 last;
2410 }
2411 }
2412 push @comment, <$fd>;
2413 $tag{'comment'} = \@comment;
2414 close $fd or return;
2415 if (!defined $tag{'name'}) {
2416 return
2417 };
2418 return %tag
2419}
2420
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002421sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002422 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002423 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02002424 my %co;
2425
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002426 pop @commit_lines; # Remove '\0'
2427
Jakub Narebski198a2a82007-05-12 21:16:34 +02002428 if (! @commit_lines) {
2429 return;
2430 }
2431
Jakub Narebski717b8312006-07-31 21:22:15 +02002432 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02002433 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002434 return;
2435 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002436 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02002437 while (my $line = shift @commit_lines) {
2438 last if $line eq "\n";
2439 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2440 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002441 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00002442 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02002443 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2444 $co{'author'} = $1;
2445 $co{'author_epoch'} = $2;
2446 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01002447 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2448 $co{'author_name'} = $1;
2449 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02002450 } else {
2451 $co{'author_name'} = $co{'author'};
2452 }
2453 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2454 $co{'committer'} = $1;
2455 $co{'committer_epoch'} = $2;
2456 $co{'committer_tz'} = $3;
2457 $co{'committer_name'} = $co{'committer'};
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01002458 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2459 $co{'committer_name'} = $1;
2460 $co{'committer_email'} = $2;
2461 } else {
2462 $co{'committer_name'} = $co{'committer'};
2463 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002464 }
2465 }
2466 if (!defined $co{'tree'}) {
2467 return;
2468 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00002469 $co{'parents'} = \@parents;
2470 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02002471
2472 foreach my $title (@commit_lines) {
2473 $title =~ s/^ //;
2474 if ($title ne "") {
2475 $co{'title'} = chop_str($title, 80, 5);
2476 # remove leading stuff of merges to make the interesting part visible
2477 if (length($title) > 50) {
2478 $title =~ s/^Automatic //;
2479 $title =~ s/^merge (of|with) /Merge ... /i;
2480 if (length($title) > 50) {
2481 $title =~ s/(http|rsync):\/\///;
2482 }
2483 if (length($title) > 50) {
2484 $title =~ s/(master|www|rsync)\.//;
2485 }
2486 if (length($title) > 50) {
2487 $title =~ s/kernel.org:?//;
2488 }
2489 if (length($title) > 50) {
2490 $title =~ s/\/pub\/scm//;
2491 }
2492 }
2493 $co{'title_short'} = chop_str($title, 50, 5);
2494 last;
2495 }
2496 }
Joey Hess53c39672008-09-05 14:26:29 -04002497 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02002498 $co{'title'} = $co{'title_short'} = '(no commit message)';
2499 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002500 # remove added spaces
2501 foreach my $line (@commit_lines) {
2502 $line =~ s/^ //;
2503 }
2504 $co{'comment'} = \@commit_lines;
2505
2506 my $age = time - $co{'committer_epoch'};
2507 $co{'age'} = $age;
2508 $co{'age_string'} = age_string($age);
2509 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2510 if ($age > 60*60*24*7*2) {
2511 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2512 $co{'age_string_age'} = $co{'age_string'};
2513 } else {
2514 $co{'age_string_date'} = $co{'age_string'};
2515 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2516 }
2517 return %co;
2518}
2519
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002520sub parse_commit {
2521 my ($commit_id) = @_;
2522 my %co;
2523
2524 local $/ = "\0";
2525
2526 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002527 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002528 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002529 "--max-count=1",
2530 $commit_id,
2531 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02002532 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002533 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002534 close $fd;
2535
2536 return %co;
2537}
2538
2539sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01002540 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002541 my @cos;
2542
2543 $maxcount ||= 1;
2544 $skip ||= 0;
2545
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002546 local $/ = "\0";
2547
2548 open my $fd, "-|", git_cmd(), "rev-list",
2549 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01002550 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002551 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00002552 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02002553 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002554 $commit_id,
2555 "--",
2556 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02002557 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002558 while (my $line = <$fd>) {
2559 my %co = parse_commit_text($line);
2560 push @cos, \%co;
2561 }
2562 close $fd;
2563
2564 return wantarray ? @cos : \@cos;
2565}
2566
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002567# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02002568sub parse_difftree_raw_line {
2569 my $line = shift;
2570 my %res;
2571
2572 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2573 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2574 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2575 $res{'from_mode'} = $1;
2576 $res{'to_mode'} = $2;
2577 $res{'from_id'} = $3;
2578 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01002579 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02002580 $res{'similarity'} = $6;
2581 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002582 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002583 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01002584 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002585 }
2586 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02002587 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2588 # combined diff (for merge commit)
2589 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2590 $res{'nparents'} = length($1);
2591 $res{'from_mode'} = [ split(' ', $2) ];
2592 $res{'to_mode'} = pop @{$res{'from_mode'}};
2593 $res{'from_id'} = [ split(' ', $3) ];
2594 $res{'to_id'} = pop @{$res{'from_id'}};
2595 $res{'status'} = [ split('', $4) ];
2596 $res{'to_file'} = unquote($5);
2597 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002598 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02002599 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2600 $res{'commit'} = $1;
2601 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002602
2603 return wantarray ? %res : \%res;
2604}
2605
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002606# wrapper: return parsed line of git-diff-tree "raw" output
2607# (the argument might be raw line, or parsed info)
2608sub parsed_difftree_line {
2609 my $line_or_ref = shift;
2610
2611 if (ref($line_or_ref) eq "HASH") {
2612 # pre-parsed (or generated by hand)
2613 return $line_or_ref;
2614 } else {
2615 return parse_difftree_raw_line($line_or_ref);
2616 }
2617}
2618
Jakub Narebskicb849b42006-08-31 00:32:15 +02002619# parse line of git-ls-tree output
2620sub parse_ls_tree_line ($;%) {
2621 my $line = shift;
2622 my %opts = @_;
2623 my %res;
2624
2625 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002626 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002627
2628 $res{'mode'} = $1;
2629 $res{'type'} = $2;
2630 $res{'hash'} = $3;
2631 if ($opts{'-z'}) {
2632 $res{'name'} = $4;
2633 } else {
2634 $res{'name'} = unquote($4);
2635 }
2636
2637 return wantarray ? %res : \%res;
2638}
2639
Jakub Narebski90921742007-06-08 13:27:42 +02002640# generates _two_ hashes, references to which are passed as 2 and 3 argument
2641sub parse_from_to_diffinfo {
2642 my ($diffinfo, $from, $to, @parents) = @_;
2643
2644 if ($diffinfo->{'nparents'}) {
2645 # combined diff
2646 $from->{'file'} = [];
2647 $from->{'href'} = [];
2648 fill_from_file_info($diffinfo, @parents)
2649 unless exists $diffinfo->{'from_file'};
2650 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01002651 $from->{'file'}[$i] =
2652 defined $diffinfo->{'from_file'}[$i] ?
2653 $diffinfo->{'from_file'}[$i] :
2654 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002655 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2656 $from->{'href'}[$i] = href(action=>"blob",
2657 hash_base=>$parents[$i],
2658 hash=>$diffinfo->{'from_id'}[$i],
2659 file_name=>$from->{'file'}[$i]);
2660 } else {
2661 $from->{'href'}[$i] = undef;
2662 }
2663 }
2664 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002665 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01002666 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002667 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2668 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2669 hash=>$diffinfo->{'from_id'},
2670 file_name=>$from->{'file'});
2671 } else {
2672 delete $from->{'href'};
2673 }
2674 }
2675
Jakub Narebski9d301452007-11-01 12:38:08 +01002676 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002677 if (!is_deleted($diffinfo)) { # file exists in result
2678 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2679 hash=>$diffinfo->{'to_id'},
2680 file_name=>$to->{'file'});
2681 } else {
2682 delete $to->{'href'};
2683 }
2684}
2685
Jakub Narebski717b8312006-07-31 21:22:15 +02002686## ......................................................................
2687## parse to array of hashes functions
2688
Jakub Narebskicd146402006-11-02 20:23:11 +01002689sub git_get_heads_list {
2690 my $limit = shift;
2691 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002692
Jakub Narebskicd146402006-11-02 20:23:11 +01002693 open my $fd, '-|', git_cmd(), 'for-each-ref',
2694 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2695 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2696 'refs/heads'
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002697 or return;
2698 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01002699 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02002700
Jakub Narebskicd146402006-11-02 20:23:11 +01002701 chomp $line;
2702 my ($refinfo, $committerinfo) = split(/\0/, $line);
2703 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2704 my ($committer, $epoch, $tz) =
2705 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01002706 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01002707 $name =~ s!^refs/heads/!!;
2708
2709 $ref_item{'name'} = $name;
2710 $ref_item{'id'} = $hash;
2711 $ref_item{'title'} = $title || '(no commit message)';
2712 $ref_item{'epoch'} = $epoch;
2713 if ($epoch) {
2714 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2715 } else {
2716 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02002717 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002718
2719 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002720 }
2721 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07002722
Jakub Narebskicd146402006-11-02 20:23:11 +01002723 return wantarray ? @headslist : \@headslist;
2724}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002725
Jakub Narebskicd146402006-11-02 20:23:11 +01002726sub git_get_tags_list {
2727 my $limit = shift;
2728 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002729
Jakub Narebskicd146402006-11-02 20:23:11 +01002730 open my $fd, '-|', git_cmd(), 'for-each-ref',
2731 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2732 '--format=%(objectname) %(objecttype) %(refname) '.
2733 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2734 'refs/tags'
2735 or return;
2736 while (my $line = <$fd>) {
2737 my %ref_item;
2738
2739 chomp $line;
2740 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2741 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2742 my ($creator, $epoch, $tz) =
2743 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01002744 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01002745 $name =~ s!^refs/tags/!!;
2746
2747 $ref_item{'type'} = $type;
2748 $ref_item{'id'} = $id;
2749 $ref_item{'name'} = $name;
2750 if ($type eq "tag") {
2751 $ref_item{'subject'} = $title;
2752 $ref_item{'reftype'} = $reftype;
2753 $ref_item{'refid'} = $refid;
2754 } else {
2755 $ref_item{'reftype'} = $type;
2756 $ref_item{'refid'} = $id;
2757 }
2758
2759 if ($type eq "tag" || $type eq "commit") {
2760 $ref_item{'epoch'} = $epoch;
2761 if ($epoch) {
2762 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2763 } else {
2764 $ref_item{'age'} = "unknown";
2765 }
2766 }
2767
2768 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02002769 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002770 close $fd;
2771
2772 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002773}
2774
2775## ----------------------------------------------------------------------
2776## filesystem-related functions
2777
2778sub get_file_owner {
2779 my $path = shift;
2780
2781 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2782 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2783 if (!defined $gcos) {
2784 return undef;
2785 }
2786 my $owner = $gcos;
2787 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02002788 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02002789}
2790
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01002791# assume that file exists
2792sub insert_file {
2793 my $filename = shift;
2794
2795 open my $fd, '<', $filename;
Jakub Narebski45868642008-12-08 14:13:21 +01002796 print map { to_utf8($_) } <$fd>;
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01002797 close $fd;
2798}
2799
Jakub Narebski717b8312006-07-31 21:22:15 +02002800## ......................................................................
2801## mimetype related functions
2802
2803sub mimetype_guess_file {
2804 my $filename = shift;
2805 my $mimemap = shift;
2806 -r $mimemap or return undef;
2807
2808 my %mimemap;
2809 open(MIME, $mimemap) or return undef;
2810 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02002811 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02002812 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07002813 if (defined $exts) {
2814 my @exts = split(/\s+/, $exts);
2815 foreach my $ext (@exts) {
2816 $mimemap{$ext} = $mime;
2817 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002818 }
2819 }
2820 close(MIME);
2821
Jakub Narebski80593192006-09-19 13:57:03 +02002822 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02002823 return $mimemap{$1};
2824}
2825
2826sub mimetype_guess {
2827 my $filename = shift;
2828 my $mime;
2829 $filename =~ /\./ or return undef;
2830
2831 if ($mimetypes_file) {
2832 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02002833 if ($file !~ m!^/!) { # if it is relative path
2834 # it is relative to project
2835 $file = "$projectroot/$project/$file";
2836 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002837 $mime = mimetype_guess_file($filename, $file);
2838 }
2839 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2840 return $mime;
2841}
2842
Jakub Narebski847e01f2006-08-14 02:05:47 +02002843sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02002844 my $fd = shift;
2845 my $filename = shift;
2846
2847 if ($filename) {
2848 my $mime = mimetype_guess($filename);
2849 $mime and return $mime;
2850 }
2851
2852 # just in case
2853 return $default_blob_plain_mimetype unless $fd;
2854
2855 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02002856 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02002857 } elsif (! $filename) {
2858 return 'application/octet-stream';
2859 } elsif ($filename =~ m/\.png$/i) {
2860 return 'image/png';
2861 } elsif ($filename =~ m/\.gif$/i) {
2862 return 'image/gif';
2863 } elsif ($filename =~ m/\.jpe?g$/i) {
2864 return 'image/jpeg';
2865 } else {
2866 return 'application/octet-stream';
2867 }
2868}
2869
Jakub Narebski7f718e82008-06-03 16:47:10 +02002870sub blob_contenttype {
2871 my ($fd, $file_name, $type) = @_;
2872
2873 $type ||= blob_mimetype($fd, $file_name);
2874 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2875 $type .= "; charset=$default_text_plain_charset";
2876 }
2877
2878 return $type;
2879}
2880
Jakub Narebski717b8312006-07-31 21:22:15 +02002881## ======================================================================
2882## functions printing HTML: header, footer, error page
2883
Kay Sievers12a88f22005-08-07 20:02:47 +02002884sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02002885 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02002886 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002887
Petr Baudis8be28902006-10-24 05:18:39 +02002888 my $title = "$site_name";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002889 if (defined $project) {
Martin Koegler00f429a2007-06-03 17:42:44 +02002890 $title .= " - " . to_utf8($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002891 if (defined $action) {
2892 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002893 if (defined $file_name) {
Jakub Narebski403d0902006-11-08 11:48:56 +01002894 $title .= " - " . esc_path($file_name);
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002895 if ($action eq "tree" && $file_name !~ m|/$|) {
2896 $title .= "/";
2897 }
2898 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02002899 }
2900 }
Alp Tokerf6801d62006-07-11 11:19:34 +01002901 my $content_type;
2902 # require explicit support from the UA if we are to send the page as
2903 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2904 # we have to do this because MSIE sometimes globs '*/*', pretending to
2905 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02002906 if (defined $cgi->http('HTTP_ACCEPT') &&
2907 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2908 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01002909 $content_type = 'application/xhtml+xml';
2910 } else {
2911 $content_type = 'text/html';
2912 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002913 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2914 -status=> $status, -expires => $expires);
Jakub Narebski45c9a752006-12-27 23:59:51 +01002915 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02002916 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02002917<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02002918<!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 +02002919<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02002920<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02002921<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02002922<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01002923<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01002924<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02002925<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02002926<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02002927EOF
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01002928 # the stylesheet, favicon etc urls won't work correctly with path_info
2929 # unless we set the appropriate base URL
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01002930 if ($ENV{'PATH_INFO'}) {
Giuseppe Bilotta81d3fe92009-02-15 10:18:36 +01002931 print "<base href=\"".esc_url($base_url)."\" />\n";
Giuseppe Bilottac3254ae2009-01-31 02:31:50 +01002932 }
Giuseppe Bilotta41a4d162009-01-31 02:31:52 +01002933 # print out each stylesheet that exist, providing backwards capability
2934 # for those people who defined $stylesheet in a config file
Alan Chandlerb2d34762006-10-03 13:49:03 +01002935 if (defined $stylesheet) {
Alan Chandlerb2d34762006-10-03 13:49:03 +01002936 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2937 } else {
2938 foreach my $stylesheet (@stylesheets) {
2939 next unless $stylesheet;
2940 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2941 }
2942 }
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002943 if (defined $project) {
Jakub Narebski35621982008-04-20 22:09:48 +02002944 my %href_params = get_feed_info();
2945 if (!exists $href_params{'-title'}) {
2946 $href_params{'-title'} = 'log';
2947 }
2948
2949 foreach my $format qw(RSS Atom) {
2950 my $type = lc($format);
2951 my %link_attr = (
2952 '-rel' => 'alternate',
2953 '-title' => "$project - $href_params{'-title'} - $format feed",
2954 '-type' => "application/$type+xml"
2955 );
2956
2957 $href_params{'action'} = $type;
2958 $link_attr{'-href'} = href(%href_params);
2959 print "<link ".
2960 "rel=\"$link_attr{'-rel'}\" ".
2961 "title=\"$link_attr{'-title'}\" ".
2962 "href=\"$link_attr{'-href'}\" ".
2963 "type=\"$link_attr{'-type'}\" ".
2964 "/>\n";
2965
2966 $href_params{'extra_options'} = '--no-merges';
2967 $link_attr{'-href'} = href(%href_params);
2968 $link_attr{'-title'} .= ' (no merges)';
2969 print "<link ".
2970 "rel=\"$link_attr{'-rel'}\" ".
2971 "title=\"$link_attr{'-title'}\" ".
2972 "href=\"$link_attr{'-href'}\" ".
2973 "type=\"$link_attr{'-type'}\" ".
2974 "/>\n";
2975 }
2976
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002977 } else {
2978 printf('<link rel="alternate" title="%s projects list" '.
Jakub Narebski35621982008-04-20 22:09:48 +02002979 'href="%s" type="text/plain; charset=utf-8" />'."\n",
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002980 $site_name, href(project=>undef, action=>"project_index"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002981 printf('<link rel="alternate" title="%s projects feeds" '.
Jakub Narebski35621982008-04-20 22:09:48 +02002982 'href="%s" type="text/x-opml" />'."\n",
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002983 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002984 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002985 if (defined $favicon) {
Jakub Narebski35621982008-04-20 22:09:48 +02002986 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002987 }
Jakub Narebski10161352006-08-05 13:18:58 +02002988
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002989 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01002990 "<body>\n";
2991
2992 if (-f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01002993 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01002994 }
2995
2996 print "<div class=\"page_header\">\n" .
Jakub Narebski9a7a62f2006-10-06 12:31:05 +02002997 $cgi->a({-href => esc_url($logo_url),
2998 -title => $logo_label},
2999 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
Jakub Narebskif93bff82006-09-26 01:58:41 +02003000 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02003001 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003002 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02003003 if (defined $action) {
3004 print " / $action";
3005 }
Kay Sievers19806692005-08-07 20:26:27 +02003006 print "\n";
Robert Fitzsimons6be93512006-12-23 03:35:16 +00003007 }
Petr Baudisd77b5672007-05-17 04:24:19 +02003008 print "</div>\n";
3009
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003010 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02003011 if (defined $project && $have_search) {
Kay Sievers19806692005-08-07 20:26:27 +02003012 if (!defined $searchtext) {
3013 $searchtext = "";
3014 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02003015 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03003016 if (defined $hash_base) {
3017 $search_hash = $hash_base;
3018 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02003019 $search_hash = $hash;
3020 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02003021 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02003022 }
Matt McCutchen40375a82007-06-28 14:57:07 -04003023 my $action = $my_uri;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003024 my $use_pathinfo = gitweb_check_feature('pathinfo');
Matt McCutchen40375a82007-06-28 14:57:07 -04003025 if ($use_pathinfo) {
martin f. krafft85d17a12008-04-20 23:23:38 +02003026 $action .= "/".esc_url($project);
Matt McCutchen40375a82007-06-28 14:57:07 -04003027 }
Matt McCutchen40375a82007-06-28 14:57:07 -04003028 print $cgi->startform(-method => "get", -action => $action) .
Kay Sieversc994d622005-08-07 20:27:18 +02003029 "<div class=\"search\">\n" .
Jakub Narebskif70dda22008-06-02 11:54:41 +02003030 (!$use_pathinfo &&
3031 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3032 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3033 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
Petr Baudis88ad7292006-10-24 05:15:46 +02003034 $cgi->popup_menu(-name => 'st', -default => 'commit',
Petr Baudise7738552007-05-17 04:31:12 +02003035 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Petr Baudis88ad7292006-10-24 05:15:46 +02003036 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3037 " search:\n",
Kay Sieversc994d622005-08-07 20:27:18 +02003038 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
Petr Baudis0e559912008-02-26 13:22:08 +01003039 "<span title=\"Extended regular expression\">" .
3040 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3041 -checked => $search_use_regexp) .
3042 "</span>" .
Kay Sieversc994d622005-08-07 20:27:18 +02003043 "</div>" .
3044 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003045 }
Kay Sievers161332a2005-08-07 19:49:46 +02003046}
3047
Kay Sievers12a88f22005-08-07 20:02:47 +02003048sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02003049 my $feed_class = 'rss_logo';
3050
Kay Sievers6191f8e2005-08-07 20:19:56 +02003051 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02003052 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003053 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02003054 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01003055 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02003056 }
Jakub Narebski35621982008-04-20 22:09:48 +02003057
3058 my %href_params = get_feed_info();
3059 if (!%href_params) {
3060 $feed_class .= ' generic';
3061 }
3062 $href_params{'-title'} ||= 'log';
3063
3064 foreach my $format qw(RSS Atom) {
3065 $href_params{'action'} = lc($format);
3066 print $cgi->a({-href => href(%href_params),
3067 -title => "$href_params{'-title'} $format feed",
3068 -class => $feed_class}, $format)."\n";
3069 }
3070
Kay Sieversc994d622005-08-07 20:27:18 +02003071 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02003072 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski35621982008-04-20 22:09:48 +02003073 -class => $feed_class}, "OPML") . " ";
Jakub Narebski9d0734a2006-09-15 11:11:33 +02003074 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
Jakub Narebski35621982008-04-20 22:09:48 +02003075 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02003076 }
Jakub Narebski35621982008-04-20 22:09:48 +02003077 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01003078
3079 if (-f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003080 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01003081 }
3082
3083 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02003084 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02003085}
3086
Lea Wiemann074afaa2008-06-19 22:03:21 +02003087# die_error(<http_status_code>, <error_message>)
3088# Example: die_error(404, 'Hash not found')
3089# By convention, use the following status codes (as defined in RFC 2616):
3090# 400: Invalid or missing CGI parameters, or
3091# requested object exists but has wrong type.
3092# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3093# this server or project.
3094# 404: Requested object/revision/project doesn't exist.
3095# 500: The server isn't configured properly, or
3096# an internal error occurred (e.g. failed assertions caused by bugs), or
3097# an unknown error occurred (e.g. the git binary died unexpectedly).
Kay Sievers061cc7c2005-08-07 20:15:57 +02003098sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02003099 my $status = shift || 500;
3100 my $error = shift || "Internal server error";
Kay Sievers664f4cc2005-08-07 20:17:19 +02003101
Lea Wiemann074afaa2008-06-19 22:03:21 +02003102 my %http_responses = (400 => '400 Bad Request',
3103 403 => '403 Forbidden',
3104 404 => '404 Not Found',
3105 500 => '500 Internal Server Error');
3106 git_header_html($http_responses{$status});
Jakub Narebski59b9f612006-08-22 23:42:53 +02003107 print <<EOF;
3108<div class="page_body">
3109<br /><br />
3110$status - $error
3111<br />
3112</div>
3113EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02003114 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02003115 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02003116}
3117
Jakub Narebski717b8312006-07-31 21:22:15 +02003118## ----------------------------------------------------------------------
3119## functions printing or outputting HTML: navigation
3120
Jakub Narebski847e01f2006-08-14 02:05:47 +02003121sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003122 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3123 $extra = '' if !defined $extra; # pager or formats
3124
3125 my @navs = qw(summary shortlog log commit commitdiff tree);
3126 if ($suppress) {
3127 @navs = grep { $_ ne $suppress } @navs;
3128 }
3129
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003130 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003131 if (defined $head) {
3132 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02003133 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003134 }
3135 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3136 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02003137 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003138 }
3139 }
3140 }
Petr Baudisd627f682008-10-02 16:36:52 +02003141
Jakub Narebski3be8e722007-04-01 22:22:21 +02003142 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3143 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003144
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08003145 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02003146 my %repl = (
3147 '%' => '%',
3148 'n' => $project, # project name
3149 'f' => $git_dir, # project path within filesystem
3150 'h' => $treehead || '', # current hash ('h' parameter)
3151 'b' => $treebase || '', # hash base ('hb' parameter)
3152 );
Petr Baudisd627f682008-10-02 16:36:52 +02003153 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02003154 my ($label, $link, $pos) = splice(@actions,0,3);
3155 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02003156 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3157 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02003158 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02003159 $arg{$label}{'_href'} = $link;
3160 }
3161
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003162 print "<div class=\"page_nav\">\n" .
3163 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003164 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02003165 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003166 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02003167 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003168 "</div>\n";
3169}
3170
Jakub Narebski847e01f2006-08-14 02:05:47 +02003171sub format_paging_nav {
Lea Wiemann1f684dc2008-05-28 01:25:42 +02003172 my ($action, $hash, $head, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02003173 my $paging_nav;
3174
3175
3176 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003177 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003178 } else {
3179 $paging_nav .= "HEAD";
3180 }
3181
3182 if ($page > 0) {
3183 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01003184 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02003185 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003186 } else {
3187 $paging_nav .= " &sdot; prev";
3188 }
3189
Lea Wiemann1f684dc2008-05-28 01:25:42 +02003190 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02003191 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01003192 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02003193 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003194 } else {
3195 $paging_nav .= " &sdot; next";
3196 }
3197
3198 return $paging_nav;
3199}
3200
Jakub Narebski717b8312006-07-31 21:22:15 +02003201## ......................................................................
3202## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02003203
Jakub Narebski847e01f2006-08-14 02:05:47 +02003204sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02003205 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003206 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02003207
Jakub Narebski3be8e722007-04-01 22:22:21 +02003208 $args{'action'} = $action;
3209 $args{'hash'} = $hash if $hash;
3210 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02003211
3212 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003213 $cgi->a({-href => href(%args), -class => "title"},
3214 $title ? $title : $action) .
3215 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02003216}
3217
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003218#sub git_print_authorship (\%) {
3219sub git_print_authorship {
3220 my $co = shift;
3221
Jakub Narebskia44465c2006-08-28 23:17:31 +02003222 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003223 print "<div class=\"author_date\">" .
3224 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02003225 " [$ad{'rfc2822'}";
3226 if ($ad{'hour_local'} < 6) {
3227 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3228 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3229 } else {
3230 printf(" (%02d:%02d %s)",
3231 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3232 }
3233 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003234}
3235
Jakub Narebski717b8312006-07-31 21:22:15 +02003236sub git_print_page_path {
3237 my $name = shift;
3238 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003239 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01003240
Jakub Narebski4df118e2006-10-21 17:53:55 +02003241
3242 print "<div class=\"page_path\">";
3243 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02003244 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02003245 print " / ";
3246 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02003247 my @dirname = split '/', $name;
3248 my $basename = pop @dirname;
3249 my $fullname = '';
3250
Jakub Narebski762c7202006-09-04 18:17:58 +02003251 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02003252 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02003253 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3254 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003255 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02003256 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02003257 }
3258 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02003259 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02003260 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003261 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02003262 } elsif (defined $type && $type eq 'tree') {
3263 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3264 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003265 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02003266 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003267 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01003268 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003269 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02003270 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02003271 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003272}
3273
Jakub Narebskib7f92532006-08-28 14:48:10 +02003274# sub git_print_log (\@;%) {
3275sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02003276 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02003277 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02003278
Jakub Narebskib7f92532006-08-28 14:48:10 +02003279 if ($opts{'-remove_title'}) {
3280 # remove title, i.e. first line of log
3281 shift @$log;
3282 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02003283 # remove leading empty lines
3284 while (defined $log->[0] && $log->[0] eq "") {
3285 shift @$log;
3286 }
3287
3288 # print log
3289 my $signoff = 0;
3290 my $empty = 0;
3291 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02003292 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3293 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02003294 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02003295 if (! $opts{'-remove_signoff'}) {
3296 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3297 next;
3298 } else {
3299 # remove signoff lines
3300 next;
3301 }
3302 } else {
3303 $signoff = 0;
3304 }
3305
Jakub Narebskid16d0932006-08-17 11:21:23 +02003306 # print only one empty line
3307 # do not print empty line after signoff
3308 if ($line eq "") {
3309 next if ($empty || $signoff);
3310 $empty = 1;
3311 } else {
3312 $empty = 0;
3313 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02003314
3315 print format_log_line_html($line) . "<br/>\n";
3316 }
3317
3318 if ($opts{'-final_empty_line'}) {
3319 # end with single empty line
3320 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02003321 }
3322}
3323
Jakub Narebskie33fba42006-12-10 13:25:46 +01003324# return link target (what link points to)
3325sub git_get_link_target {
3326 my $hash = shift;
3327 my $link_target;
3328
3329 # read link
3330 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3331 or return;
3332 {
3333 local $/;
3334 $link_target = <$fd>;
3335 }
3336 close $fd
3337 or return;
3338
3339 return $link_target;
3340}
3341
Jakub Narebski3bf9d572006-12-10 13:25:48 +01003342# given link target, and the directory (basedir) the link is in,
3343# return target of link relative to top directory (top tree);
3344# return undef if it is not possible (including absolute links).
3345sub normalize_link_target {
3346 my ($link_target, $basedir, $hash_base) = @_;
3347
3348 # we can normalize symlink target only if $hash_base is provided
3349 return unless $hash_base;
3350
3351 # absolute symlinks (beginning with '/') cannot be normalized
3352 return if (substr($link_target, 0, 1) eq '/');
3353
3354 # normalize link target to path from top (root) tree (dir)
3355 my $path;
3356 if ($basedir) {
3357 $path = $basedir . '/' . $link_target;
3358 } else {
3359 # we are in top (root) tree (dir)
3360 $path = $link_target;
3361 }
3362
3363 # remove //, /./, and /../
3364 my @path_parts;
3365 foreach my $part (split('/', $path)) {
3366 # discard '.' and ''
3367 next if (!$part || $part eq '.');
3368 # handle '..'
3369 if ($part eq '..') {
3370 if (@path_parts) {
3371 pop @path_parts;
3372 } else {
3373 # link leads outside repository (outside top dir)
3374 return;
3375 }
3376 } else {
3377 push @path_parts, $part;
3378 }
3379 }
3380 $path = join('/', @path_parts);
3381
3382 return $path;
3383}
Jakub Narebskie33fba42006-12-10 13:25:46 +01003384
Jakub Narebskifa702002006-08-31 00:35:07 +02003385# print tree entry (row of git_tree), but without encompassing <tr> element
3386sub git_print_tree_entry {
3387 my ($t, $basedir, $hash_base, $have_blame) = @_;
3388
3389 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01003390 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02003391
Luben Tuikov4de741b2006-09-25 22:38:16 -07003392 # The format of a table row is: mode list link. Where mode is
3393 # the mode of the entry, list is the name of the entry, an href,
3394 # and link is the action links of the entry.
3395
Jakub Narebskifa702002006-08-31 00:35:07 +02003396 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3397 if ($t->{'type'} eq "blob") {
3398 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07003399 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003400 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01003401 -class => "list"}, esc_path($t->{'name'}));
3402 if (S_ISLNK(oct $t->{'mode'})) {
3403 my $link_target = git_get_link_target($t->{'hash'});
3404 if ($link_target) {
Jakub Narebski3bf9d572006-12-10 13:25:48 +01003405 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3406 if (defined $norm_target) {
3407 print " -> " .
3408 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3409 file_name=>$norm_target),
3410 -title => $norm_target}, esc_path($link_target));
3411 } else {
3412 print " -> " . esc_path($link_target);
3413 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01003414 }
3415 }
3416 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07003417 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02003418 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003419 file_name=>"$basedir$t->{'name'}", %base_key)},
3420 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02003421 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02003422 print " | " .
3423 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003424 file_name=>"$basedir$t->{'name'}", %base_key)},
3425 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02003426 }
3427 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02003428 print " | " .
3429 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02003430 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3431 "history");
3432 }
3433 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07003434 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003435 file_name=>"$basedir$t->{'name'}")},
3436 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07003437 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02003438
3439 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07003440 print "<td class=\"list\">";
3441 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskifa702002006-08-31 00:35:07 +02003442 file_name=>"$basedir$t->{'name'}", %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01003443 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07003444 print "</td>\n";
3445 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02003446 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003447 file_name=>"$basedir$t->{'name'}", %base_key)},
3448 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02003449 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02003450 print " | " .
3451 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02003452 file_name=>"$basedir$t->{'name'}")},
3453 "history");
3454 }
3455 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02003456 } else {
3457 # unknown object: we can only present history for it
3458 # (this includes 'commit' object, i.e. submodule support)
3459 print "<td class=\"list\">" .
3460 esc_path($t->{'name'}) .
3461 "</td>\n";
3462 print "<td class=\"link\">";
3463 if (defined $hash_base) {
3464 print $cgi->a({-href => href(action=>"history",
3465 hash_base=>$hash_base,
3466 file_name=>"$basedir$t->{'name'}")},
3467 "history");
3468 }
3469 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02003470 }
3471}
3472
Jakub Narebski717b8312006-07-31 21:22:15 +02003473## ......................................................................
3474## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02003475
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003476# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003477sub fill_from_file_info {
3478 my ($diff, @parents) = @_;
3479
3480 $diff->{'from_file'} = [ ];
3481 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3482 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3483 if ($diff->{'status'}[$i] eq 'R' ||
3484 $diff->{'status'}[$i] eq 'C') {
3485 $diff->{'from_file'}[$i] =
3486 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3487 }
3488 }
3489
3490 return $diff;
3491}
3492
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003493# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02003494sub is_deleted {
3495 my $diffinfo = shift;
3496
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003497 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02003498}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003499
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003500# does patch correspond to [previous] difftree raw line
3501# $diffinfo - hashref of parsed raw diff format
3502# $patchinfo - hashref of parsed patch diff format
3503# (the same keys as in $diffinfo)
3504sub is_patch_split {
3505 my ($diffinfo, $patchinfo) = @_;
3506
3507 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01003508 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003509}
3510
3511
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003512sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02003513 my ($difftree, $hash, @parents) = @_;
3514 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003515 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003516 print "<div class=\"list_head\">\n";
3517 if ($#{$difftree} > 10) {
3518 print(($#{$difftree} + 1) . " files changed:\n");
3519 }
3520 print "</div>\n";
3521
Jakub Narebskied224de2007-05-07 01:10:04 +02003522 print "<table class=\"" .
3523 (@parents > 1 ? "combined " : "") .
3524 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02003525
3526 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02003527 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02003528 if ($has_header) {
3529 # table header
3530 print "<thead><tr>\n" .
3531 "<th></th><th></th>\n"; # filename, patchN link
3532 for (my $i = 0; $i < @parents; $i++) {
3533 my $par = $parents[$i];
3534 print "<th>" .
3535 $cgi->a({-href => href(action=>"commitdiff",
3536 hash=>$hash, hash_parent=>$par),
3537 -title => 'commitdiff to parent number ' .
3538 ($i+1) . ': ' . substr($par,0,7)},
3539 $i+1) .
3540 "&nbsp;</th>\n";
3541 }
3542 print "</tr></thead>\n<tbody>\n";
3543 }
3544
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003545 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02003546 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003547 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003548 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003549
3550 if ($alternate) {
3551 print "<tr class=\"dark\">\n";
3552 } else {
3553 print "<tr class=\"light\">\n";
3554 }
3555 $alternate ^= 1;
3556
Jakub Narebski493e01d2007-05-07 01:10:06 +02003557 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02003558
Jakub Narebski493e01d2007-05-07 01:10:06 +02003559 fill_from_file_info($diff, @parents)
3560 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003561
Jakub Narebski90921742007-06-08 13:27:42 +02003562 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003563 # file exists in the result (child) commit
3564 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02003565 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3566 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003567 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02003568 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02003569 "</td>\n";
3570 } else {
3571 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02003572 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02003573 "</td>\n";
3574 }
3575
3576 if ($action eq 'commitdiff') {
3577 # link to patch
3578 $patchno++;
3579 print "<td class=\"link\">" .
3580 $cgi->a({-href => "#patch$patchno"}, "patch") .
3581 " | " .
3582 "</td>\n";
3583 }
3584
3585 my $has_history = 0;
3586 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02003587 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003588 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02003589 my $from_hash = $diff->{'from_id'}[$i];
3590 my $from_path = $diff->{'from_file'}[$i];
3591 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02003592
3593 $has_history ||= ($status ne 'A');
3594 $not_deleted ||= ($status ne 'D');
3595
Jakub Narebskied224de2007-05-07 01:10:04 +02003596 if ($status eq 'A') {
3597 print "<td class=\"link\" align=\"right\"> | </td>\n";
3598 } elsif ($status eq 'D') {
3599 print "<td class=\"link\">" .
3600 $cgi->a({-href => href(action=>"blob",
3601 hash_base=>$hash,
3602 hash=>$from_hash,
3603 file_name=>$from_path)},
3604 "blob" . ($i+1)) .
3605 " | </td>\n";
3606 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02003607 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003608 print "<td class=\"link nochange\">";
3609 } else {
3610 print "<td class=\"link\">";
3611 }
3612 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003613 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003614 hash_parent=>$from_hash,
3615 hash_base=>$hash,
3616 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003617 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003618 file_parent=>$from_path)},
3619 "diff" . ($i+1)) .
3620 " | </td>\n";
3621 }
3622 }
3623
3624 print "<td class=\"link\">";
3625 if ($not_deleted) {
3626 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003627 hash=>$diff->{'to_id'},
3628 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003629 hash_base=>$hash)},
3630 "blob");
3631 print " | " if ($has_history);
3632 }
3633 if ($has_history) {
3634 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003635 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003636 hash_base=>$hash)},
3637 "history");
3638 }
3639 print "</td>\n";
3640
3641 print "</tr>\n";
3642 next; # instead of 'else' clause, to avoid extra indent
3643 }
3644 # else ordinary diff
3645
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003646 my ($to_mode_oct, $to_mode_str, $to_file_type);
3647 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02003648 if ($diff->{'to_mode'} ne ('0' x 6)) {
3649 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003650 if (S_ISREG($to_mode_oct)) { # only for regular file
3651 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003652 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003653 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003654 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003655 if ($diff->{'from_mode'} ne ('0' x 6)) {
3656 $from_mode_oct = oct $diff->{'from_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003657 if (S_ISREG($to_mode_oct)) { # only for regular file
3658 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3659 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003660 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003661 }
3662
Jakub Narebski493e01d2007-05-07 01:10:06 +02003663 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003664 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3665 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3666 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003667 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003668 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3669 hash_base=>$hash, file_name=>$diff->{'file'}),
3670 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003671 print "</td>\n";
3672 print "<td>$mode_chng</td>\n";
3673 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003674 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003675 # link to patch
3676 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003677 print $cgi->a({-href => "#patch$patchno"}, "patch");
Jakub Narebski897d1d22006-11-19 22:51:39 +01003678 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003679 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003680 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3681 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01003682 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02003683 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003684
Jakub Narebski493e01d2007-05-07 01:10:06 +02003685 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003686 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003687 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003688 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3689 hash_base=>$parent, file_name=>$diff->{'file'}),
3690 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003691 print "</td>\n";
3692 print "<td>$mode_chng</td>\n";
3693 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003694 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003695 # link to patch
3696 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003697 print $cgi->a({-href => "#patch$patchno"}, "patch");
3698 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003699 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003700 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3701 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003702 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003703 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003704 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003705 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003706 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003707 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02003708 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003709 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003710 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07003711 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003712
Jakub Narebski493e01d2007-05-07 01:10:06 +02003713 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003714 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003715 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003716 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01003717 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003718 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003719 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003720 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3721 if ($from_mode_str && $to_mode_str) {
3722 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3723 } elsif ($to_mode_str) {
3724 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003725 }
3726 }
3727 $mode_chnge .= "]</span>\n";
3728 }
3729 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003730 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3731 hash_base=>$hash, file_name=>$diff->{'file'}),
3732 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003733 print "</td>\n";
3734 print "<td>$mode_chnge</td>\n";
3735 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003736 if ($action eq 'commitdiff') {
3737 # link to patch
3738 $patchno++;
3739 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3740 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003741 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003742 # "commit" view and modified file (not onlu mode changed)
3743 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003744 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003745 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003746 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003747 "diff") .
3748 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003749 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003750 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3751 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003752 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003753 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003754 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003755 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003756 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003757 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07003758 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003759 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003760 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003761 print "</td>\n";
3762
Jakub Narebski493e01d2007-05-07 01:10:06 +02003763 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003764 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02003765 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003766 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003767 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003768 # mode also for directories, so we cannot use $to_mode_str
3769 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003770 }
3771 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003772 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003773 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3774 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003775 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3776 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003777 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3778 -class => "list"}, esc_path($diff->{'from_file'})) .
3779 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07003780 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003781 if ($action eq 'commitdiff') {
3782 # link to patch
3783 $patchno++;
3784 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3785 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003786 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003787 # "commit" view and modified file (not only pure rename or copy)
3788 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003789 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003790 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003791 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003792 "diff") .
3793 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003794 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003795 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3796 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003797 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003798 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003799 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003800 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003801 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003802 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01003803 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003804 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003805 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003806 print "</td>\n";
3807
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003808 } # we should not encounter Unmerged (U) or Unknown (X) status
3809 print "</tr>\n";
3810 }
Jakub Narebski47598d72007-06-08 13:24:56 +02003811 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003812 print "</table>\n";
3813}
3814
Jakub Narebskieee08902006-08-24 00:15:14 +02003815sub git_patchset_body {
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003816 my ($fd, $difftree, $hash, @hash_parents) = @_;
3817 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02003818
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003819 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02003820 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07003821 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003822 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02003823 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003824 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003825 my (%from, %to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003826
3827 print "<div class=\"patchset\">\n";
3828
Jakub Narebski6d55f052006-11-18 23:35:39 +01003829 # skip to first patch
3830 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02003831 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02003832
Jakub Narebski6d55f052006-11-18 23:35:39 +01003833 last if ($patch_line =~ m/^diff /);
3834 }
3835
3836 PATCH:
3837 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01003838
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003839 # parse "git diff" header line
3840 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3841 # $1 is from_name, which we do not use
3842 $to_name = unquote($2);
3843 $to_name =~ s!^b/!!;
3844 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3845 # $1 is 'cc' or 'combined', which we do not use
3846 $to_name = unquote($2);
3847 } else {
3848 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003849 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003850
3851 # check if current patch belong to current raw line
3852 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003853 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02003854 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01003855 print "<div class=\"patch cont\">\n";
3856 } else {
3857 # advance raw git-diff output if needed
3858 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02003859
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003860 # read and prepare patch information
3861 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3862
Jakub Narebskicd030c32007-06-08 13:33:28 +02003863 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003864 # find which patch (using pathname of result) we are at now;
3865 if ($is_combined) {
3866 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003867 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3868 format_diff_cc_simplified($diffinfo, @hash_parents) .
3869 "</div>\n"; # class="patch"
3870
3871 $patch_idx++;
3872 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003873
3874 last if $patch_idx > $#$difftree;
3875 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003876 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003877 }
Jakub Narebski711fa742007-09-08 21:49:11 +02003878
Jakub Narebski90921742007-06-08 13:27:42 +02003879 # modifies %from, %to hashes
3880 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02003881
Jakub Narebski6d55f052006-11-18 23:35:39 +01003882 # this is first patch for raw difftree line with $patch_idx index
3883 # we index @$difftree array from 0, but number patches from 1
3884 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003885 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003886
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003887 # git diff header
3888 #assert($patch_line =~ m/^diff /) if DEBUG;
3889 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3890 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003891 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02003892 print format_git_diff_header_line($patch_line, $diffinfo,
3893 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003894
Jakub Narebski6d55f052006-11-18 23:35:39 +01003895 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003896 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01003897 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003898 while ($patch_line = <$fd>) {
3899 chomp $patch_line;
3900
3901 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3902
Jakub Narebski90921742007-06-08 13:27:42 +02003903 print format_extended_diff_header_line($patch_line, $diffinfo,
3904 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003905 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003906 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02003907
Jakub Narebski6d55f052006-11-18 23:35:39 +01003908 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01003909 if (! $patch_line) {
3910 print "</div>\n"; # class="patch"
3911 last PATCH;
3912 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01003913 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003914 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003915
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003916 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003917 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003918 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02003919 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003920
Jakub Narebski90921742007-06-08 13:27:42 +02003921 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02003922 $diffinfo, \%from, \%to,
3923 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003924
3925 # the patch itself
3926 LINE:
3927 while ($patch_line = <$fd>) {
3928 chomp $patch_line;
3929
3930 next PATCH if ($patch_line =~ m/^diff /);
3931
Jakub Narebski59e3b142006-11-18 23:35:40 +01003932 print format_diff_line($patch_line, \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003933 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003934
Jakub Narebski6d55f052006-11-18 23:35:39 +01003935 } continue {
3936 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02003937 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02003938
Jakub Narebskicd030c32007-06-08 13:33:28 +02003939 # for compact combined (--cc) format, with chunk and patch simpliciaction
3940 # patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003941 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02003942 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003943 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003944 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003945 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003946
3947 # generate anchor for "patch" links in difftree / whatchanged part
3948 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3949 format_diff_cc_simplified($diffinfo, @hash_parents) .
3950 "</div>\n"; # class="patch"
3951
3952 $patch_number++;
3953 }
3954
Jakub Narebskid26c4262007-05-17 00:05:55 +02003955 if ($patch_number == 0) {
3956 if (@hash_parents > 1) {
3957 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3958 } else {
3959 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3960 }
3961 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003962
3963 print "</div>\n"; # class="patchset"
3964}
3965
3966# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3967
Jakub Narebski69913412008-06-10 19:21:01 +02003968# fills project list info (age, description, owner, forks) for each
3969# project in the list, removing invalid projects from returned list
3970# NOTE: modifies $projlist, but does not remove entries from it
3971sub fill_project_list_info {
3972 my ($projlist, $check_forks) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02003973 my @projects;
Jakub Narebski69913412008-06-10 19:21:01 +02003974
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003975 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02003976 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02003977 foreach my $pr (@$projlist) {
Jakub Narebski69913412008-06-10 19:21:01 +02003978 my (@activity) = git_get_last_activity($pr->{'path'});
3979 unless (@activity) {
3980 next PROJECT;
Petr Baudise30496d2006-10-24 05:33:17 +02003981 }
Jakub Narebski69913412008-06-10 19:21:01 +02003982 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02003983 if (!defined $pr->{'descr'}) {
3984 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02003985 $descr = to_utf8($descr);
3986 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06003987 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02003988 }
3989 if (!defined $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003990 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02003991 }
3992 if ($check_forks) {
3993 my $pname = $pr->{'path'};
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003994 if (($pname =~ s/\.git$//) &&
3995 ($pname !~ /\/$/) &&
3996 (-d "$projectroot/$pname")) {
3997 $pr->{'forks'} = "-d $projectroot/$pname";
Jakub Narebski69913412008-06-10 19:21:01 +02003998 } else {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003999 $pr->{'forks'} = 0;
4000 }
Petr Baudise30496d2006-10-24 05:33:17 +02004001 }
Petr Baudisaed93de2008-10-02 17:13:02 +02004002 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02004003 push @projects, $pr;
4004 }
4005
Jakub Narebski69913412008-06-10 19:21:01 +02004006 return @projects;
4007}
4008
Petr Baudis6b28da62008-09-25 18:48:37 +02004009# print 'sort by' <th> element, generating 'sort by $name' replay link
4010# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02004011sub print_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02004012 my ($name, $order, $header) = @_;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02004013 $header ||= ucfirst($name);
4014
4015 if ($order eq $name) {
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02004016 print "<th>$header</th>\n";
4017 } else {
4018 print "<th>" .
4019 $cgi->a({-href => href(-replay=>1, order=>$name),
4020 -class => "header"}, $header) .
4021 "</th>\n";
4022 }
4023}
4024
Jakub Narebski69913412008-06-10 19:21:01 +02004025sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02004026 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02004027 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4028
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004029 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski69913412008-06-10 19:21:01 +02004030 my @projects = fill_project_list_info($projlist, $check_forks);
4031
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02004032 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02004033 $from = 0 unless defined $from;
4034 $to = $#projects if (!defined $to || $#projects < $to);
4035
Petr Baudis6b28da62008-09-25 18:48:37 +02004036 my %order_info = (
4037 project => { key => 'path', type => 'str' },
4038 descr => { key => 'descr_long', type => 'str' },
4039 owner => { key => 'owner', type => 'str' },
4040 age => { key => 'age', type => 'num' }
4041 );
4042 my $oi = $order_info{$order};
4043 if ($oi->{'type'} eq 'str') {
4044 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4045 } else {
4046 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4047 }
4048
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004049 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02004050 if ($show_ctags) {
4051 my %ctags;
4052 foreach my $p (@projects) {
4053 foreach my $ct (keys %{$p->{'ctags'}}) {
4054 $ctags{$ct} += $p->{'ctags'}->{$ct};
4055 }
4056 }
4057 my $cloud = git_populate_project_tagcloud(\%ctags);
4058 print git_show_project_tagcloud($cloud, 64);
4059 }
4060
Petr Baudise30496d2006-10-24 05:33:17 +02004061 print "<table class=\"project_list\">\n";
4062 unless ($no_header) {
4063 print "<tr>\n";
4064 if ($check_forks) {
4065 print "<th></th>\n";
4066 }
Petr Baudis6b28da62008-09-25 18:48:37 +02004067 print_sort_th('project', $order, 'Project');
4068 print_sort_th('descr', $order, 'Description');
4069 print_sort_th('owner', $order, 'Owner');
4070 print_sort_th('age', $order, 'Last Change');
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02004071 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02004072 "</tr>\n";
4073 }
4074 my $alternate = 1;
Petr Baudisaed93de2008-10-02 17:13:02 +02004075 my $tagfilter = $cgi->param('by_tag');
Petr Baudise30496d2006-10-24 05:33:17 +02004076 for (my $i = $from; $i <= $to; $i++) {
4077 my $pr = $projects[$i];
Petr Baudis42326112008-10-02 17:17:01 +02004078
Petr Baudisaed93de2008-10-02 17:13:02 +02004079 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
Petr Baudis0d1d1542008-10-03 09:29:45 +02004080 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4081 and not $pr->{'descr_long'} =~ /$searchtext/;
4082 # Weed out forks or non-matching entries of search
Petr Baudis42326112008-10-02 17:17:01 +02004083 if ($check_forks) {
4084 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4085 $forkbase="^$forkbase" if $forkbase;
Petr Baudis0d1d1542008-10-03 09:29:45 +02004086 next if not $searchtext and not $tagfilter and $show_ctags
4087 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
Petr Baudis42326112008-10-02 17:17:01 +02004088 }
4089
Petr Baudise30496d2006-10-24 05:33:17 +02004090 if ($alternate) {
4091 print "<tr class=\"dark\">\n";
4092 } else {
4093 print "<tr class=\"light\">\n";
4094 }
4095 $alternate ^= 1;
4096 if ($check_forks) {
4097 print "<td>";
4098 if ($pr->{'forks'}) {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08004099 print "<!-- $pr->{'forks'} -->\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004100 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4101 }
4102 print "</td>\n";
4103 }
4104 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4105 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01004106 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4107 -class => "list", -title => $pr->{'descr_long'}},
4108 esc_html($pr->{'descr'})) . "</td>\n" .
David Symondsd3cd2492007-10-23 11:31:23 +10004109 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004110 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
Jakub Narebski785cdea2007-05-13 12:39:22 +02004111 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
Petr Baudise30496d2006-10-24 05:33:17 +02004112 "<td class=\"link\">" .
4113 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
Alexandre Julliardfaa1bbf2006-11-15 21:37:50 +01004114 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
Petr Baudise30496d2006-10-24 05:33:17 +02004115 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4116 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4117 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4118 "</td>\n" .
4119 "</tr>\n";
4120 }
4121 if (defined $extra) {
4122 print "<tr>\n";
4123 if ($check_forks) {
4124 print "<td></td>\n";
4125 }
4126 print "<td colspan=\"5\">$extra</td>\n" .
4127 "</tr>\n";
4128 }
4129 print "</table>\n";
4130}
4131
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004132sub git_shortlog_body {
4133 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004134 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304135
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004136 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004137 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004138
Jakub Narebski591ebf62007-11-19 14:16:11 +01004139 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004140 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004141 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004142 my %co = %{$commitlist->[$i]};
4143 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02004144 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004145 if ($alternate) {
4146 print "<tr class=\"dark\">\n";
4147 } else {
4148 print "<tr class=\"light\">\n";
4149 }
4150 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10004151 my $author = chop_and_escape_str($co{'author_name'}, 10);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004152 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4153 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004154 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004155 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004156 print format_subject_html($co{'title'}, $co{'title_short'},
4157 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004158 print "</td>\n" .
4159 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02004160 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02004161 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02004162 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004163 my $snapshot_links = format_snapshot_links($commit);
4164 if (defined $snapshot_links) {
4165 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02004166 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304167 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004168 "</tr>\n";
4169 }
4170 if (defined $extra) {
4171 print "<tr>\n" .
4172 "<td colspan=\"4\">$extra</td>\n" .
4173 "</tr>\n";
4174 }
4175 print "</table>\n";
4176}
4177
Jakub Narebski581860e2006-08-14 02:09:08 +02004178sub git_history_body {
4179 # Warning: assumes constant type (blob or tree) during history
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004180 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02004181
4182 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004183 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02004184
Jakub Narebski591ebf62007-11-19 14:16:11 +01004185 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004186 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02004187 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004188 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02004189 if (!%co) {
4190 next;
4191 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004192 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02004193
4194 my $ref = format_ref_marker($refs, $commit);
4195
4196 if ($alternate) {
4197 print "<tr class=\"dark\">\n";
4198 } else {
4199 print "<tr class=\"light\">\n";
4200 }
4201 $alternate ^= 1;
David Symondse076a0e2007-10-22 10:28:03 +10004202 # shortlog uses chop_str($co{'author_name'}, 10)
David Symondsce58ec92007-10-23 11:31:22 +10004203 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
Jakub Narebski581860e2006-08-14 02:09:08 +02004204 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004205 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski581860e2006-08-14 02:09:08 +02004206 "<td>";
4207 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02004208 print format_subject_html($co{'title'}, $co{'title_short'},
4209 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02004210 print "</td>\n" .
4211 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07004212 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4213 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02004214
4215 if ($ftype eq 'blob') {
4216 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4217 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4218 if (defined $blob_current && defined $blob_parent &&
4219 $blob_current ne $blob_parent) {
4220 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02004221 $cgi->a({-href => href(action=>"blobdiff",
4222 hash=>$blob_current, hash_parent=>$blob_parent,
4223 hash_base=>$hash_base, hash_parent_base=>$commit,
4224 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02004225 "diff to current");
4226 }
4227 }
4228 print "</td>\n" .
4229 "</tr>\n";
4230 }
4231 if (defined $extra) {
4232 print "<tr>\n" .
4233 "<td colspan=\"4\">$extra</td>\n" .
4234 "</tr>\n";
4235 }
4236 print "</table>\n";
4237}
4238
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004239sub git_tags_body {
4240 # uses global variable $project
4241 my ($taglist, $from, $to, $extra) = @_;
4242 $from = 0 unless defined $from;
4243 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4244
Jakub Narebski591ebf62007-11-19 14:16:11 +01004245 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004246 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004247 for (my $i = $from; $i <= $to; $i++) {
4248 my $entry = $taglist->[$i];
4249 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01004250 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004251 my $comment_short;
4252 if (defined $comment) {
4253 $comment_short = chop_str($comment, 30, 5);
4254 }
4255 if ($alternate) {
4256 print "<tr class=\"dark\">\n";
4257 } else {
4258 print "<tr class=\"light\">\n";
4259 }
4260 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01004261 if (defined $tag{'age'}) {
4262 print "<td><i>$tag{'age'}</i></td>\n";
4263 } else {
4264 print "<td></td>\n";
4265 }
4266 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004267 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02004268 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004269 "</td>\n" .
4270 "<td>";
4271 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004272 print format_subject_html($comment, $comment_short,
4273 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004274 }
4275 print "</td>\n" .
4276 "<td class=\"selflink\">";
4277 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004278 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004279 } else {
4280 print "&nbsp;";
4281 }
4282 print "</td>\n" .
4283 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004284 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004285 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01004286 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4287 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004288 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004289 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004290 }
4291 print "</td>\n" .
4292 "</tr>";
4293 }
4294 if (defined $extra) {
4295 print "<tr>\n" .
4296 "<td colspan=\"5\">$extra</td>\n" .
4297 "</tr>\n";
4298 }
4299 print "</table>\n";
4300}
4301
4302sub git_heads_body {
4303 # uses global variable $project
Jakub Narebski120ddde2006-09-19 14:33:22 +02004304 my ($headlist, $head, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004305 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02004306 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004307
Jakub Narebski591ebf62007-11-19 14:16:11 +01004308 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004309 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004310 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02004311 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01004312 my %ref = %$entry;
4313 my $curr = $ref{'id'} eq $head;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004314 if ($alternate) {
4315 print "<tr class=\"dark\">\n";
4316 } else {
4317 print "<tr class=\"light\">\n";
4318 }
4319 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01004320 print "<td><i>$ref{'age'}</i></td>\n" .
4321 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01004322 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01004323 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004324 "</td>\n" .
4325 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01004326 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4327 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4328 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004329 "</td>\n" .
4330 "</tr>";
4331 }
4332 if (defined $extra) {
4333 print "<tr>\n" .
4334 "<td colspan=\"3\">$extra</td>\n" .
4335 "</tr>\n";
4336 }
4337 print "</table>\n";
4338}
4339
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004340sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004341 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004342 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004343 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004344
Jakub Narebski591ebf62007-11-19 14:16:11 +01004345 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004346 my $alternate = 1;
4347 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004348 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004349 if (!%co) {
4350 next;
4351 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004352 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004353 if ($alternate) {
4354 print "<tr class=\"dark\">\n";
4355 } else {
4356 print "<tr class=\"light\">\n";
4357 }
4358 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10004359 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004360 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004361 "<td><i>" . $author . "</i></td>\n" .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004362 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004363 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4364 -class => "list subject"},
4365 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004366 my $comment = $co{'comment'};
4367 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01004368 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004369 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01004370 $match = chop_str($match, 70, 5, 'center');
4371 my $contextlen = int((80 - length($match))/2);
4372 $contextlen = 30 if ($contextlen > 30);
4373 $lead = chop_str($lead, $contextlen, 10, 'left');
4374 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004375
4376 $lead = esc_html($lead);
4377 $match = esc_html($match);
4378 $trail = esc_html($trail);
4379
4380 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004381 }
4382 }
4383 print "</td>\n" .
4384 "<td class=\"link\">" .
4385 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4386 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08004387 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4388 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004389 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4390 print "</td>\n" .
4391 "</tr>\n";
4392 }
4393 if (defined $extra) {
4394 print "<tr>\n" .
4395 "<td colspan=\"3\">$extra</td>\n" .
4396 "</tr>\n";
4397 }
4398 print "</table>\n";
4399}
4400
Jakub Narebski717b8312006-07-31 21:22:15 +02004401## ======================================================================
4402## ======================================================================
4403## actions
4404
Jakub Narebski717b8312006-07-31 21:22:15 +02004405sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02004406 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02004407 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004408 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02004409 }
4410
Jakub Narebski847e01f2006-08-14 02:05:47 +02004411 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02004412 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004413 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02004414 }
Jakub Narebski6326b602006-08-01 02:59:12 +02004415
Jakub Narebski717b8312006-07-31 21:22:15 +02004416 git_header_html();
4417 if (-f $home_text) {
4418 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004419 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02004420 print "</div>\n";
4421 }
Petr Baudis0d1d1542008-10-03 09:29:45 +02004422 print $cgi->startform(-method => "get") .
4423 "<p class=\"projsearch\">Search:\n" .
4424 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4425 "</p>" .
4426 $cgi->end_form() . "\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004427 git_project_list_body(\@list, $order);
4428 git_footer_html();
4429}
4430
4431sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02004432 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02004433 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004434 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02004435 }
Petr Baudise30496d2006-10-24 05:33:17 +02004436
4437 my @list = git_get_projects_list($project);
4438 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004439 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02004440 }
Petr Baudise30496d2006-10-24 05:33:17 +02004441
4442 git_header_html();
4443 git_print_page_nav('','');
4444 git_print_header_div('summary', "$project forks");
4445 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02004446 git_footer_html();
4447}
4448
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004449sub git_project_index {
Petr Baudise30496d2006-10-24 05:33:17 +02004450 my @projects = git_get_projects_list($project);
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004451
4452 print $cgi->header(
4453 -type => 'text/plain',
4454 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004455 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004456
4457 foreach my $pr (@projects) {
4458 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02004459 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004460 }
4461
4462 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4463 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4464 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4465 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4466 $path =~ s/ /\+/g;
4467 $owner =~ s/ /\+/g;
4468
4469 print "$path $owner\n";
4470 }
4471}
4472
Kay Sieversede5e102005-08-07 20:23:12 +02004473sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004474 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00004475 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02004476 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00004477 my $head = $co{'id'};
Kay Sieversede5e102005-08-07 20:23:12 +02004478
Jakub Narebski1e0cf032006-08-14 02:10:06 +02004479 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004480
Jakub Narebskicd146402006-11-02 20:23:11 +01004481 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004482 # These get_*_list functions return one more to allow us to see if
4483 # there are more ...
4484 my @taglist = git_get_tags_list(16);
4485 my @headlist = git_get_heads_list(16);
Petr Baudise30496d2006-10-24 05:33:17 +02004486 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004487 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08004488
4489 if ($check_forks) {
Petr Baudise30496d2006-10-24 05:33:17 +02004490 @forklist = git_get_projects_list($project);
4491 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02004492
Kay Sieversede5e102005-08-07 20:23:12 +02004493 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004494 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004495
Kay Sievers19806692005-08-07 20:26:27 +02004496 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01004497 print "<table class=\"projects_list\">\n" .
Petr Baudisa4761422008-10-02 16:25:05 +02004498 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4499 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02004500 if (defined $cd{'rfc2822'}) {
Petr Baudisa4761422008-10-02 16:25:05 +02004501 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02004502 }
4503
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02004504 # use per project git URL list in $projectroot/$project/cloneurl
4505 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02004506 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02004507 my @url_list = git_get_project_url_list($project);
4508 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4509 foreach my $git_url (@url_list) {
4510 next unless $git_url;
Petr Baudisa4761422008-10-02 16:25:05 +02004511 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02004512 $url_tag = "";
4513 }
Petr Baudisaed93de2008-10-02 17:13:02 +02004514
4515 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004516 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02004517 if ($show_ctags) {
4518 my $ctags = git_get_project_ctags($project);
4519 my $cloud = git_populate_project_tagcloud($ctags);
4520 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4521 print "</td>\n<td>" unless %$ctags;
4522 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4523 print "</td>\n<td>" if %$ctags;
4524 print git_show_project_tagcloud($cloud, 48);
4525 print "</td></tr>";
4526 }
4527
Jakub Narebski19a87212006-08-15 23:03:17 +02004528 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004529
Matt McCutchen7e1100e2009-02-07 19:00:09 -05004530 # If XSS prevention is on, we don't include README.html.
4531 # TODO: Allow a readme in some safe format.
4532 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004533 print "<div class=\"title\">readme</div>\n" .
4534 "<div class=\"readme\">\n";
4535 insert_file("$projectroot/$project/README.html");
4536 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02004537 }
4538
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004539 # we need to request one more than 16 (0..15) to check if
4540 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02004541 my @commitlist = $head ? parse_commits($head, 17) : ();
4542 if (@commitlist) {
4543 git_print_header_div('shortlog');
4544 git_shortlog_body(\@commitlist, 0, 15, $refs,
4545 $#commitlist <= 15 ? undef :
4546 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4547 }
Kay Sieversede5e102005-08-07 20:23:12 +02004548
Jakub Narebski120ddde2006-09-19 14:33:22 +02004549 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004550 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02004551 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004552 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004553 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02004554 }
Kay Sievers0db37972005-08-07 20:24:35 +02004555
Jakub Narebski120ddde2006-09-19 14:33:22 +02004556 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004557 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02004558 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004559 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004560 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02004561 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004562
Petr Baudise30496d2006-10-24 05:33:17 +02004563 if (@forklist) {
4564 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02004565 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00004566 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02004567 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02004568 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02004569 }
4570
Kay Sieversede5e102005-08-07 20:23:12 +02004571 git_footer_html();
4572}
4573
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004574sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004575 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004576 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004577 git_print_page_nav('','', $head,undef,$head);
4578 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02004579
4580 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004581 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02004582 }
4583
Jakub Narebski847e01f2006-08-14 02:05:47 +02004584 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004585 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01004586 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02004587 "<tr>\n" .
4588 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004589 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4590 $tag{'object'}) . "</td>\n" .
4591 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4592 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02004593 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004594 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004595 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01004596 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004597 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4598 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4599 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004600 }
4601 print "</table>\n\n" .
4602 "</div>\n";
4603 print "<div class=\"page_body\">";
4604 my $comment = $tag{'comment'};
4605 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08004606 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01004607 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004608 }
4609 print "</div>\n";
4610 git_footer_html();
4611}
4612
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +02004613sub git_blame {
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004614 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004615 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004616 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02004617
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004618 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02004619 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004620 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004621 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004622 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02004623 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004624 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004625 if (!defined $hash) {
4626 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004627 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004628 } else {
4629 $ftype = git_get_type($hash);
4630 if ($ftype !~ "blob") {
4631 die_error(400, "Object is not a blob");
4632 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004633 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004634
4635 # run git-blame --porcelain
4636 open my $fd, "-|", git_cmd(), "blame", '-p',
4637 $hash_base, '--', $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02004638 or die_error(500, "Open git-blame failed");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004639
4640 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004641 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004642 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01004643 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02004644 "blob") .
4645 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01004646 $cgi->a({-href => href(action=>"history", -replay=>1)},
4647 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02004648 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004649 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004650 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004651 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4652 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004653 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004654
4655 # page body
4656 my @rev_color = qw(light2 dark2);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07004657 my $num_colors = scalar(@rev_color);
4658 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004659 my %metainfo = ();
4660
Jakub Narebski59b9f612006-08-22 23:42:53 +02004661 print <<HTML;
4662<div class="page_body">
4663<table class="blame">
4664<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4665HTML
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004666 LINE:
4667 while (my $line = <$fd>) {
4668 chomp $line;
4669 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
4670 # no <lines in group> for subsequent lines in group of lines
Luben Tuikovd15c55a2006-10-11 00:30:05 -07004671 my ($full_rev, $orig_lineno, $lineno, $group_size) =
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004672 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004673 if (!exists $metainfo{$full_rev}) {
4674 $metainfo{$full_rev} = {};
4675 }
4676 my $meta = $metainfo{$full_rev};
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004677 my $data;
4678 while ($data = <$fd>) {
4679 chomp $data;
4680 last if ($data =~ s/^\t//); # contents of line
4681 if ($data =~ /^(\S+) (.*)$/) {
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004682 $meta->{$1} = $2;
4683 }
4684 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004685 my $short_rev = substr($full_rev, 0, 8);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004686 my $author = $meta->{'author'};
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004687 my %date =
4688 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004689 my $date = $date{'iso-tz'};
4690 if ($group_size) {
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004691 $current_color = ($current_color + 1) % $num_colors;
Luben Tuikovcc1bf972006-07-23 13:37:53 -07004692 }
Jakub Narebski4a24bfc2008-12-09 23:46:16 +01004693 print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004694 if ($group_size) {
4695 print "<td class=\"sha1\"";
Luben Tuikov5ad08282006-10-30 12:37:54 -08004696 print " title=\"". esc_html($author) . ", $date\"";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004697 print " rowspan=\"$group_size\"" if ($group_size > 1);
4698 print ">";
4699 print $cgi->a({-href => href(action=>"commit",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004700 hash=>$full_rev,
4701 file_name=>$file_name)},
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004702 esc_html($short_rev));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004703 print "</td>\n";
Luben Tuikov9dc5f8c2006-10-04 00:12:17 -07004704 }
Jakub Narebski39c19ce2008-12-11 01:33:29 +01004705 my $parent_commit;
4706 if (!exists $meta->{'parent'}) {
4707 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4708 or die_error(500, "Open git-rev-parse failed");
4709 $parent_commit = <$dd>;
4710 close $dd;
4711 chomp($parent_commit);
4712 $meta->{'parent'} = $parent_commit;
4713 } else {
4714 $parent_commit = $meta->{'parent'};
4715 }
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004716 my $blamed = href(action => 'blame',
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004717 file_name => $meta->{'filename'},
4718 hash_base => $parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004719 print "<td class=\"linenr\">";
4720 print $cgi->a({ -href => "$blamed#l$orig_lineno",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004721 -class => "linenr" },
4722 esc_html($lineno));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004723 print "</td>";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004724 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4725 print "</tr>\n";
4726 }
4727 print "</table>\n";
4728 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004729 close $fd
4730 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004731
4732 # page footer
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004733 git_footer_html();
4734}
4735
Kay Sieversede5e102005-08-07 20:23:12 +02004736sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004737 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004738 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004739 git_print_page_nav('','', $head,undef,$head);
4740 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004741
Jakub Narebskicd146402006-11-02 20:23:11 +01004742 my @tagslist = git_get_tags_list();
4743 if (@tagslist) {
4744 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02004745 }
Kay Sieversede5e102005-08-07 20:23:12 +02004746 git_footer_html();
4747}
4748
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02004749sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004750 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004751 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004752 git_print_page_nav('','', $head,undef,$head);
4753 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004754
Jakub Narebskicd146402006-11-02 20:23:11 +01004755 my @headslist = git_get_heads_list();
4756 if (@headslist) {
4757 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02004758 }
Kay Sievers0db37972005-08-07 20:24:35 +02004759 git_footer_html();
4760}
4761
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004762sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02004763 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004764 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004765
Luben Tuikovcff07712006-07-23 13:28:55 -07004766 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004767 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004768 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004769 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004770 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004771 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004772 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004773 }
Martin Waitz800764c2006-09-16 23:09:02 +02004774 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4775 # blobs defined by non-textual hash id's can be cached
4776 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004777 }
Martin Waitz800764c2006-09-16 23:09:02 +02004778
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004779 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004780 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004781
Jakub Narebski7f718e82008-06-03 16:47:10 +02004782 # content-type (can include charset)
4783 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004784
Jakub Narebski7f718e82008-06-03 16:47:10 +02004785 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004786 my $save_as = "$hash";
4787 if (defined $file_name) {
4788 $save_as = $file_name;
4789 } elsif ($type =~ m/^text\//) {
4790 $save_as .= '.txt';
4791 }
4792
Matt McCutchen7e1100e2009-02-07 19:00:09 -05004793 # With XSS prevention on, blobs of all types except a few known safe
4794 # ones are served with "Content-Disposition: attachment" to make sure
4795 # they don't run in our security domain. For certain image types,
4796 # blob view writes an <img> tag referring to blob_plain view, and we
4797 # want to be sure not to break that by serving the image as an
4798 # attachment (though Firefox 3 doesn't seem to care).
4799 my $sandbox = $prevent_xss &&
4800 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
4801
Jakub Narebskif2e73302006-08-26 19:14:25 +02004802 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02004803 -type => $type,
4804 -expires => $expires,
Matt McCutchen7e1100e2009-02-07 19:00:09 -05004805 -content_disposition =>
4806 ($sandbox ? 'attachment' : 'inline')
4807 . '; filename="' . $save_as . '"');
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004808 undef $/;
4809 binmode STDOUT, ':raw';
4810 print <$fd>;
4811 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4812 $/ = "\n";
4813 close $fd;
4814}
4815
Kay Sievers09bd7892005-08-07 20:21:23 +02004816sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02004817 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004818
Luben Tuikovcff07712006-07-23 13:28:55 -07004819 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004820 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004821 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004822 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004823 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004824 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004825 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004826 }
Martin Waitz800764c2006-09-16 23:09:02 +02004827 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4828 # blobs defined by non-textual hash id's can be cached
4829 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004830 }
Martin Waitz800764c2006-09-16 23:09:02 +02004831
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004832 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004833 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004834 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004835 my $mimetype = blob_mimetype($fd, $file_name);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004836 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004837 close $fd;
4838 return git_blob_plain($mimetype);
4839 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004840 # we can have blame only for text/* mimetype
4841 $have_blame &&= ($mimetype =~ m!^text/!);
4842
Jakub Narebskif2e73302006-08-26 19:14:25 +02004843 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004844 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02004845 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02004846 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02004847 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004848 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004849 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02004850 "blame") .
4851 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02004852 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004853 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004854 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004855 "history") .
4856 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01004857 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004858 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004859 " | " .
4860 $cgi->a({-href => href(action=>"blob",
4861 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004862 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02004863 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004864 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004865 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4866 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02004867 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004868 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4869 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004870 } else {
4871 print "<div class=\"page_nav\">\n" .
4872 "<br/><br/></div>\n" .
4873 "<div class=\"title\">$hash</div>\n";
4874 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004875 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02004876 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004877 if ($mimetype =~ m!^image/!) {
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004878 print qq!<img type="$mimetype"!;
4879 if ($file_name) {
4880 print qq! alt="$file_name" title="$file_name"!;
4881 }
4882 print qq! src="! .
4883 href(action=>"blob_plain", hash=>$hash,
4884 hash_base=>$hash_base, file_name=>$file_name) .
4885 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004886 } else {
4887 my $nr;
4888 while (my $line = <$fd>) {
4889 chomp $line;
4890 $nr++;
4891 $line = untabify($line);
4892 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4893 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4894 }
Kay Sievers161332a2005-08-07 19:49:46 +02004895 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02004896 close $fd
4897 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02004898 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004899 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004900}
4901
4902sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004903 if (!defined $hash_base) {
4904 $hash_base = "HEAD";
4905 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02004906 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02004907 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004908 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4909 } else {
4910 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02004911 }
Kay Sieverse925f382005-08-07 20:23:35 +02004912 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02004913 die_error(404, "No such tree") unless defined($hash);
Kay Sievers232ff552005-11-24 16:56:55 +01004914 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004915 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004916 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02004917 my @entries = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02004918 close $fd or die_error(404, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01004919 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02004920
Jakub Narebski847e01f2006-08-14 02:05:47 +02004921 my $refs = git_get_references();
4922 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02004923 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02004924 my $basedir = '';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004925 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02004926 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02004927 my @views_nav = ();
4928 if (defined $file_name) {
4929 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01004930 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004931 "history"),
4932 $cgi->a({-href => href(action=>"tree",
4933 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004934 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02004935 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004936 my $snapshot_links = format_snapshot_links($hash);
4937 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02004938 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004939 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02004940 }
4941 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02004942 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02004943 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02004944 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02004945 print "<div class=\"page_nav\">\n";
4946 print "<br/><br/></div>\n";
4947 print "<div class=\"title\">$hash</div>\n";
4948 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004949 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02004950 $basedir = $file_name;
4951 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4952 $basedir .= '/';
4953 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02004954 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004955 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02004956 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01004957 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004958 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02004959 # '..' (top directory) link if possible
4960 if (defined $hash_base &&
4961 defined $file_name && $file_name =~ m![^/]+$!) {
4962 if ($alternate) {
4963 print "<tr class=\"dark\">\n";
4964 } else {
4965 print "<tr class=\"light\">\n";
4966 }
4967 $alternate ^= 1;
4968
4969 my $up = $file_name;
4970 $up =~ s!/?[^/]+$!!;
4971 undef $up unless $up;
4972 # based on git_print_tree_entry
4973 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4974 print '<td class="list">';
4975 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4976 file_name=>$up)},
4977 "..");
4978 print "</td>\n";
4979 print "<td class=\"link\"></td>\n";
4980
4981 print "</tr>\n";
4982 }
Kay Sievers161332a2005-08-07 19:49:46 +02004983 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02004984 my %t = parse_ls_tree_line($line, -z => 1);
4985
Kay Sieversbddec012005-08-07 20:25:42 +02004986 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02004987 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004988 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02004989 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004990 }
4991 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02004992
Jakub Narebski300454f2006-10-21 17:53:09 +02004993 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02004994
Kay Sievers42f7eb92005-08-07 20:21:46 +02004995 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02004996 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02004997 print "</table>\n" .
4998 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004999 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02005000}
5001
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305002sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02005003 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01005004 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005005 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02005006 }
5007 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01005008 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02005009 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005010 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02005011 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005012 die_error(400, "Unknown snapshot format");
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01005013 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005014 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305015 }
5016
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305017 if (!defined $hash) {
5018 $hash = git_get_head_hash($project);
5019 }
5020
Mark Levedahl072570e2007-05-20 11:46:46 -04005021 my $name = $project;
Matthias Lederhofer9a7d9412007-06-07 11:27:08 +02005022 $name =~ s,([^/])/*\.git$,$1,;
5023 $name = basename($name);
5024 my $filename = to_utf8($name);
Mark Levedahl072570e2007-05-20 11:46:46 -04005025 $name =~ s/\047/\047\\\047\047/g;
Mark Levedahl072570e2007-05-20 11:46:46 -04005026 my $cmd;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005027 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
Lea Wiemann516381d2008-06-17 23:46:35 +02005028 $cmd = quote_command(
5029 git_cmd(), 'archive',
5030 "--format=$known_snapshot_formats{$format}{'format'}",
5031 "--prefix=$name/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005032 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02005033 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04005034 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305035
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02005036 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005037 -type => $known_snapshot_formats{$format}{'type'},
Luben Tuikova2a3bf72006-09-28 16:51:43 -07005038 -content_disposition => 'inline; filename="' . "$filename" . '"',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02005039 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305040
Mark Levedahl072570e2007-05-20 11:46:46 -04005041 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005042 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305043 binmode STDOUT, ':raw';
5044 print <$fd>;
5045 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5046 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305047}
5048
Kay Sievers09bd7892005-08-07 20:21:23 +02005049sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005050 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02005051 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02005052 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02005053 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005054 if (!defined $page) {
5055 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02005056 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005057 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02005058
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005059 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02005060
Lea Wiemann1f684dc2008-05-28 01:25:42 +02005061 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005062
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01005063 my ($patch_max) = gitweb_get_feature('patches');
5064 if ($patch_max) {
5065 if ($patch_max < 0 || @commitlist <= $patch_max) {
5066 $paging_nav .= " &sdot; " .
5067 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5068 "patches");
5069 }
5070 }
5071
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005072 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02005073 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005074
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005075 if (!@commitlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005076 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02005077
Jakub Narebski847e01f2006-08-14 02:05:47 +02005078 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02005079 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02005080 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005081 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5082 for (my $i = 0; $i <= $to; $i++) {
5083 my %co = %{$commitlist[$i]};
Kay Sieversb87d78d2005-08-07 20:21:04 +02005084 next if !%co;
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005085 my $commit = $co{'id'};
5086 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski847e01f2006-08-14 02:05:47 +02005087 my %ad = parse_date($co{'author_epoch'});
5088 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02005089 "<span class=\"age\">$co{'age_string'}</span>" .
5090 esc_html($co{'title'}) . $ref,
5091 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02005092 print "<div class=\"title_text\">\n" .
5093 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005094 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005095 " | " .
5096 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Petr Baudis6ef4cb22006-09-22 03:19:48 +02005097 " | " .
Petr Baudisd7267202006-09-22 16:56:43 -07005098 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
Kay Sieverseb282402005-08-07 20:21:34 +02005099 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02005100 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01005101 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02005102 "</div>\n";
5103
5104 print "<div class=\"log_body\">\n";
Jakub Narebskif2069412006-10-24 13:52:46 +02005105 git_print_log($co{'comment'}, -final_empty_line=> 1);
Kay Sievers09bd7892005-08-07 20:21:23 +02005106 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02005107 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005108 if ($#commitlist >= 100) {
5109 print "<div class=\"page_nav\">\n";
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005110 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005111 -accesskey => "n", -title => "Alt-n"}, "next");
5112 print "</div>\n";
5113 }
Kay Sievers034df392005-08-07 20:20:07 +02005114 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02005115}
5116
5117sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01005118 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02005119 my %co = parse_commit($hash)
5120 or die_error(404, "Unknown commit object");
Jakub Narebski847e01f2006-08-14 02:05:47 +02005121 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5122 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02005123
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005124 my $parent = $co{'parent'};
5125 my $parents = $co{'parents'}; # listref
5126
5127 # we need to prepare $formats_nav before any parameter munging
5128 my $formats_nav;
5129 if (!defined $parent) {
5130 # --root commitdiff
5131 $formats_nav .= '(initial)';
5132 } elsif (@$parents == 1) {
5133 # single parent commit
5134 $formats_nav .=
5135 '(parent: ' .
5136 $cgi->a({-href => href(action=>"commit",
5137 hash=>$parent)},
5138 esc_html(substr($parent, 0, 7))) .
5139 ')';
5140 } else {
5141 # merge commit
5142 $formats_nav .=
5143 '(merge: ' .
5144 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01005145 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005146 hash=>$_)},
5147 esc_html(substr($_, 0, 7)));
5148 } @$parents ) .
5149 ')';
5150 }
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01005151 if (gitweb_check_feature('patches')) {
5152 $formats_nav .= " | " .
5153 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5154 "patch");
5155 }
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005156
Kay Sieversd8a20ba2005-08-07 20:28:53 +02005157 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07005158 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02005159 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01005160 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02005161 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5162 @diff_opts,
5163 (@$parents <= 1 ? $parent : '-c'),
5164 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005165 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02005166 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02005167 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02005168
5169 # non-textual hash id's can be cached
5170 my $expires;
5171 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5172 $expires = "+1d";
5173 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005174 my $refs = git_get_references();
5175 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305176
Jakub Narebski594e2122006-07-31 02:21:52 +02005177 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02005178 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02005179 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005180 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07005181
Kay Sieversb87d78d2005-08-07 20:21:04 +02005182 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005183 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02005184 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005185 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02005186 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02005187 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01005188 "<table class=\"object_header\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01005189 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02005190 "<tr>" .
5191 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02005192 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005193 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5194 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02005195 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005196 printf(" (%02d:%02d %s)",
5197 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02005198 }
Kay Sieversbddec012005-08-07 20:25:42 +02005199 print "</td>" .
5200 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01005201 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005202 print "<tr><td></td><td> $cd{'rfc2822'}" .
5203 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5204 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00005205 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02005206 print "<tr>" .
5207 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00005208 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005209 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5210 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02005211 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005212 "<td class=\"link\">" .
5213 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5214 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005215 my $snapshot_links = format_snapshot_links($hash);
5216 if (defined $snapshot_links) {
5217 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305218 }
5219 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02005220 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01005221
Kay Sievers3e029292005-08-07 20:05:15 +02005222 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02005223 print "<tr>" .
5224 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005225 "<td class=\"sha1\">" .
5226 $cgi->a({-href => href(action=>"commit", hash=>$par),
5227 class => "list"}, $par) .
5228 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02005229 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005230 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005231 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02005232 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02005233 "</td>" .
5234 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02005235 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02005236 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02005237 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02005238
Kay Sieversfbb592a2005-08-07 20:12:11 +02005239 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02005240 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02005241 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005242
Jakub Narebski208ecb22007-05-07 01:10:08 +02005243 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005244
Kay Sievers12a88f22005-08-07 20:02:47 +02005245 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02005246}
5247
Jakub Narebskica946012006-12-10 13:25:47 +01005248sub git_object {
5249 # object is defined by:
5250 # - hash or hash_base alone
5251 # - hash_base and file_name
5252 my $type;
5253
5254 # - hash or hash_base alone
5255 if ($hash || ($hash_base && !defined $file_name)) {
5256 my $object_id = $hash || $hash_base;
5257
Lea Wiemann516381d2008-06-17 23:46:35 +02005258 open my $fd, "-|", quote_command(
5259 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02005260 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005261 $type = <$fd>;
5262 chomp $type;
5263 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005264 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005265
5266 # - hash_base and file_name
5267 } elsif ($hash_base && defined $file_name) {
5268 $file_name =~ s,/+$,,;
5269
5270 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02005271 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005272
5273 # here errors should not hapen
5274 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005275 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01005276 my $line = <$fd>;
5277 close $fd;
5278
5279 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5280 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005281 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005282 }
5283 $type = $2;
5284 $hash = $3;
5285 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005286 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01005287 }
5288
5289 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5290 hash=>$hash, hash_base=>$hash_base,
5291 file_name=>$file_name),
5292 -status => '302 Found');
5293}
5294
Kay Sievers09bd7892005-08-07 20:21:23 +02005295sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005296 my $format = shift || 'html';
5297
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005298 my $fd;
5299 my @difftree;
5300 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005301 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005302
5303 # preparing $fd and %diffinfo for git_patchset_body
5304 # new style URI
5305 if (defined $hash_base && defined $hash_parent_base) {
5306 if (defined $file_name) {
5307 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005308 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5309 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02005310 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005311 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005312 @difftree = map { chomp; $_ } <$fd>;
5313 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005314 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005315 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02005316 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005317
Jakub Narebski0aea3372006-08-27 23:45:26 +02005318 } elsif (defined $hash &&
5319 $hash =~ /[0-9a-fA-F]{40}/) {
5320 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005321
5322 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005323 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5324 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005325 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005326 @difftree =
5327 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5328 # $hash == to_id
5329 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5330 map { chomp; $_ } <$fd>;
5331 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005332 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005333 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02005334 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005335
5336 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005337 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005338 }
5339
5340 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005341 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005342 }
5343
5344 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01005345 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5346 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005347
5348 $hash_parent ||= $diffinfo{'from_id'};
5349 $hash ||= $diffinfo{'to_id'};
5350
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005351 # non-textual hash id's can be cached
5352 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5353 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5354 $expires = '+1d';
5355 }
5356
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005357 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005358 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02005359 '-p', ($format eq 'html' ? "--full-index" : ()),
5360 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02005361 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005362 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005363 }
5364
Junio C Hamanob54dc9f2008-12-16 19:42:02 -08005365 # old/legacy style URI -- not generated anymore since 1.4.3.
5366 if (!%diffinfo) {
5367 die_error('404 Not Found', "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005368 }
5369
5370 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005371 if ($format eq 'html') {
5372 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01005373 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02005374 "raw");
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005375 git_header_html(undef, $expires);
5376 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5377 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5378 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5379 } else {
5380 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5381 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5382 }
5383 if (defined $file_name) {
5384 git_print_page_path($file_name, "blob", $hash_base);
5385 } else {
5386 print "<div class=\"page_path\"></div>\n";
5387 }
5388
5389 } elsif ($format eq 'plain') {
5390 print $cgi->header(
5391 -type => 'text/plain',
5392 -charset => 'utf-8',
5393 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07005394 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005395
5396 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5397
Kay Sievers09bd7892005-08-07 20:21:23 +02005398 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005399 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005400 }
5401
5402 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005403 if ($format eq 'html') {
5404 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005405
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005406 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5407 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005408
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005409 print "</div>\n"; # class="page_body"
5410 git_footer_html();
5411
5412 } else {
5413 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01005414 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5415 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005416
5417 print $line;
5418
5419 last if $line =~ m!^\+\+\+!;
5420 }
5421 local $/ = undef;
5422 print <$fd>;
5423 close $fd;
5424 }
Kay Sievers09bd7892005-08-07 20:21:23 +02005425}
5426
Kay Sievers19806692005-08-07 20:26:27 +02005427sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005428 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02005429}
5430
Kay Sievers09bd7892005-08-07 20:21:23 +02005431sub git_commitdiff {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01005432 my %params = @_;
5433 my $format = $params{-format} || 'html';
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005434
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01005435 my ($patch_max) = gitweb_get_feature('patches');
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005436 if ($format eq 'patch') {
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005437 die_error(403, "Patch view not allowed") unless $patch_max;
5438 }
5439
Jakub Narebski9954f772006-11-18 23:35:41 +01005440 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02005441 my %co = parse_commit($hash)
5442 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02005443
Jakub Narebskicd030c32007-06-08 13:33:28 +02005444 # choose format for commitdiff for merge
5445 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5446 $hash_parent = '--cc';
5447 }
5448 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02005449 my $formats_nav;
5450 if ($format eq 'html') {
5451 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01005452 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02005453 "raw");
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01005454 if ($patch_max) {
5455 $formats_nav .= " | " .
5456 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5457 "patch");
5458 }
Jakub Narebski151602d2006-10-23 00:37:56 +02005459
Jakub Narebskicd030c32007-06-08 13:33:28 +02005460 if (defined $hash_parent &&
5461 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02005462 # commitdiff with two commits given
5463 my $hash_parent_short = $hash_parent;
5464 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5465 $hash_parent_short = substr($hash_parent, 0, 7);
5466 }
5467 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02005468 ' (from';
5469 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5470 if ($co{'parents'}[$i] eq $hash_parent) {
5471 $formats_nav .= ' parent ' . ($i+1);
5472 last;
5473 }
5474 }
5475 $formats_nav .= ': ' .
Jakub Narebski151602d2006-10-23 00:37:56 +02005476 $cgi->a({-href => href(action=>"commitdiff",
5477 hash=>$hash_parent)},
5478 esc_html($hash_parent_short)) .
5479 ')';
5480 } elsif (!$co{'parent'}) {
5481 # --root commitdiff
5482 $formats_nav .= ' (initial)';
5483 } elsif (scalar @{$co{'parents'}} == 1) {
5484 # single parent commit
5485 $formats_nav .=
5486 ' (parent: ' .
5487 $cgi->a({-href => href(action=>"commitdiff",
5488 hash=>$co{'parent'})},
5489 esc_html(substr($co{'parent'}, 0, 7))) .
5490 ')';
5491 } else {
5492 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02005493 if ($hash_parent eq '--cc') {
5494 $formats_nav .= ' | ' .
5495 $cgi->a({-href => href(action=>"commitdiff",
5496 hash=>$hash, hash_parent=>'-c')},
5497 'combined');
5498 } else { # $hash_parent eq '-c'
5499 $formats_nav .= ' | ' .
5500 $cgi->a({-href => href(action=>"commitdiff",
5501 hash=>$hash, hash_parent=>'--cc')},
5502 'compact');
5503 }
Jakub Narebski151602d2006-10-23 00:37:56 +02005504 $formats_nav .=
5505 ' (merge: ' .
5506 join(' ', map {
5507 $cgi->a({-href => href(action=>"commitdiff",
5508 hash=>$_)},
5509 esc_html(substr($_, 0, 7)));
5510 } @{$co{'parents'}} ) .
5511 ')';
5512 }
5513 }
5514
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005515 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005516 if (!defined $hash_parent_param) {
5517 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005518 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02005519 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02005520 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005521
5522 # read commitdiff
5523 my $fd;
5524 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02005525 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005526 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005527 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005528 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005529 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02005530
Jakub Narebski04408c32006-11-18 23:35:38 +01005531 while (my $line = <$fd>) {
5532 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005533 # empty line ends raw part of diff-tree output
5534 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02005535 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02005536 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005537
Jakub Narebskieee08902006-08-24 00:15:14 +02005538 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005539 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005540 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005541 or die_error(500, "Open git-diff-tree failed");
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005542 } elsif ($format eq 'patch') {
5543 # For commit ranges, we limit the output to the number of
5544 # patches specified in the 'patches' feature.
5545 # For single commits, we limit the output to a single patch,
5546 # diverging from the git-format-patch default.
5547 my @commit_spec = ();
5548 if ($hash_parent) {
5549 if ($patch_max > 0) {
5550 push @commit_spec, "-$patch_max";
5551 }
5552 push @commit_spec, '-n', "$hash_parent..$hash";
5553 } else {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01005554 if ($params{-single}) {
5555 push @commit_spec, '-1';
5556 } else {
5557 if ($patch_max > 0) {
5558 push @commit_spec, "-$patch_max";
5559 }
5560 push @commit_spec, "-n";
5561 }
5562 push @commit_spec, '--root', $hash;
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005563 }
5564 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
5565 '--stdout', @commit_spec
5566 or die_error(500, "Open git-format-patch failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02005567 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005568 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02005569 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02005570
Kay Sievers11044292005-10-19 03:18:45 +02005571 # non-textual hash id's can be cached
5572 my $expires;
5573 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5574 $expires = "+1d";
5575 }
Kay Sievers09bd7892005-08-07 20:21:23 +02005576
Jakub Narebskieee08902006-08-24 00:15:14 +02005577 # write commit message
5578 if ($format eq 'html') {
5579 my $refs = git_get_references();
5580 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02005581
Jakub Narebskieee08902006-08-24 00:15:14 +02005582 git_header_html(undef, $expires);
5583 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5584 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02005585 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02005586 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02005587 if (@{$co{'comment'}} > 1) {
5588 print "<div class=\"log\">\n";
5589 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5590 print "</div>\n"; # class="log"
5591 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02005592
Jakub Narebskieee08902006-08-24 00:15:14 +02005593 } elsif ($format eq 'plain') {
5594 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005595 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02005596 my $filename = basename($project) . "-$hash.patch";
5597
5598 print $cgi->header(
5599 -type => 'text/plain',
5600 -charset => 'utf-8',
5601 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07005602 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02005603 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09005604 print "From: " . to_utf8($co{'author'}) . "\n";
5605 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5606 print "Subject: " . to_utf8($co{'title'}) . "\n";
5607
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005608 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02005609 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005610
Jakub Narebskieee08902006-08-24 00:15:14 +02005611 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09005612 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02005613 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005614 print "---\n\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005615 } elsif ($format eq 'patch') {
5616 my $filename = basename($project) . "-$hash.patch";
5617
5618 print $cgi->header(
5619 -type => 'text/plain',
5620 -charset => 'utf-8',
5621 -expires => $expires,
5622 -content_disposition => 'inline; filename="' . "$filename" . '"');
Kay Sievers19806692005-08-07 20:26:27 +02005623 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005624
5625 # write patch
5626 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005627 my $use_parents = !defined $hash_parent ||
5628 $hash_parent eq '-c' || $hash_parent eq '--cc';
5629 git_difftree_body(\@difftree, $hash,
5630 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02005631 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02005632
Jakub Narebskicd030c32007-06-08 13:33:28 +02005633 git_patchset_body($fd, \@difftree, $hash,
5634 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02005635 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02005636 print "</div>\n"; # class="page_body"
5637 git_footer_html();
5638
5639 } elsif ($format eq 'plain') {
5640 local $/ = undef;
5641 print <$fd>;
5642 close $fd
5643 or print "Reading git-diff-tree failed\n";
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005644 } elsif ($format eq 'patch') {
5645 local $/ = undef;
5646 print <$fd>;
5647 close $fd
5648 or print "Reading git-format-patch failed\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02005649 }
5650}
5651
5652sub git_commitdiff_plain {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01005653 git_commitdiff(-format => 'plain');
Kay Sievers19806692005-08-07 20:26:27 +02005654}
5655
Giuseppe Bilotta9872cd62008-12-18 08:13:16 +01005656# format-patch-style patches
5657sub git_patch {
Giuseppe Bilottaa3411f82008-12-18 08:13:18 +01005658 git_commitdiff(-format => 'patch', -single=> 1);
5659}
5660
5661sub git_patches {
Giuseppe Bilotta20209852008-12-18 08:13:17 +01005662 git_commitdiff(-format => 'patch');
Kay Sievers09bd7892005-08-07 20:21:23 +02005663}
5664
5665sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07005666 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005667 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02005668 }
Jakub Narebski8be68352006-09-11 00:36:04 +02005669 if (!defined $page) {
5670 $page = 0;
5671 }
Luben Tuikov63433102006-07-23 13:31:15 -07005672 my $ftype;
Lea Wiemann074afaa2008-06-19 22:03:21 +02005673 my %co = parse_commit($hash_base)
5674 or die_error(404, "Unknown commit object");
Jakub Narebski8be68352006-09-11 00:36:04 +02005675
Jakub Narebski847e01f2006-08-14 02:05:47 +02005676 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02005677 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5678
Jakub Narebski5634cf22008-04-13 14:12:15 +02005679 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
Lea Wiemann074afaa2008-06-19 22:03:21 +02005680 $file_name, "--full-history")
5681 or die_error(404, "No such file or directory on given branch");
Jakub Narebski5634cf22008-04-13 14:12:15 +02005682
Luben Tuikov93d5f062006-07-23 13:30:08 -07005683 if (!defined $hash && defined $file_name) {
Jakub Narebski5634cf22008-04-13 14:12:15 +02005684 # some commits could have deleted file in question,
5685 # and not have it in tree, but one of them has to have it
5686 for (my $i = 0; $i <= @commitlist; $i++) {
5687 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5688 last if defined $hash;
5689 }
Luben Tuikov93d5f062006-07-23 13:30:08 -07005690 }
Luben Tuikovcff07712006-07-23 13:28:55 -07005691 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07005692 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07005693 }
Jakub Narebski5634cf22008-04-13 14:12:15 +02005694 if (!defined $ftype) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005695 die_error(500, "Unknown type of object");
Jakub Narebski5634cf22008-04-13 14:12:15 +02005696 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005697
Jakub Narebski8be68352006-09-11 00:36:04 +02005698 my $paging_nav = '';
5699 if ($page > 0) {
5700 $paging_nav .=
5701 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5702 file_name=>$file_name)},
5703 "first");
5704 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005705 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski8be68352006-09-11 00:36:04 +02005706 -accesskey => "p", -title => "Alt-p"}, "prev");
5707 } else {
5708 $paging_nav .= "first";
5709 $paging_nav .= " &sdot; prev";
5710 }
Jakub Narebski8be68352006-09-11 00:36:04 +02005711 my $next_link = '';
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005712 if ($#commitlist >= 100) {
Jakub Narebski8be68352006-09-11 00:36:04 +02005713 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005714 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005715 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005716 $paging_nav .= " &sdot; $next_link";
5717 } else {
5718 $paging_nav .= " &sdot; next";
Jakub Narebski8be68352006-09-11 00:36:04 +02005719 }
Jakub Narebski581860e2006-08-14 02:09:08 +02005720
Jakub Narebski8be68352006-09-11 00:36:04 +02005721 git_header_html();
5722 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5723 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5724 git_print_page_path($file_name, $ftype, $hash_base);
5725
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005726 git_history_body(\@commitlist, 0, 99,
Jakub Narebski8be68352006-09-11 00:36:04 +02005727 $refs, $hash_base, $ftype, $next_link);
5728
Kay Sieversd51e9022005-08-07 20:16:07 +02005729 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02005730}
Kay Sievers19806692005-08-07 20:26:27 +02005731
5732sub git_search {
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005733 gitweb_check_feature('search') or die_error(403, "Search is disabled");
Kay Sievers19806692005-08-07 20:26:27 +02005734 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005735 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02005736 }
5737 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005738 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005739 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005740 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02005741 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005742 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02005743 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005744 if (!defined $page) {
5745 $page = 0;
5746 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02005747
Petr Baudis88ad7292006-10-24 05:15:46 +02005748 $searchtype ||= 'commit';
5749 if ($searchtype eq 'pickaxe') {
Jakub Narebski04f7a942006-09-11 00:29:27 +02005750 # pickaxe may take all resources of your box and run for several minutes
5751 # with every query - so decide by yourself how public you make this feature
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005752 gitweb_check_feature('pickaxe')
Lea Wiemann074afaa2008-06-19 22:03:21 +02005753 or die_error(403, "Pickaxe is disabled");
Kay Sieversc994d622005-08-07 20:27:18 +02005754 }
Petr Baudise7738552007-05-17 04:31:12 +02005755 if ($searchtype eq 'grep') {
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005756 gitweb_check_feature('grep')
Lea Wiemann074afaa2008-06-19 22:03:21 +02005757 or die_error(403, "Grep is disabled");
Petr Baudise7738552007-05-17 04:31:12 +02005758 }
Petr Baudis88ad7292006-10-24 05:15:46 +02005759
Kay Sievers19806692005-08-07 20:26:27 +02005760 git_header_html();
Kay Sievers19806692005-08-07 20:26:27 +02005761
Petr Baudis88ad7292006-10-24 05:15:46 +02005762 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
Robert Fitzsimons8e574fb2006-12-23 03:35:14 +00005763 my $greptype;
5764 if ($searchtype eq 'commit') {
5765 $greptype = "--grep=";
5766 } elsif ($searchtype eq 'author') {
5767 $greptype = "--author=";
5768 } elsif ($searchtype eq 'committer') {
5769 $greptype = "--committer=";
5770 }
Jakub Narebski0270cd02008-02-26 13:22:07 +01005771 $greptype .= $searchtext;
5772 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
Petr Baudis0e559912008-02-26 13:22:08 +01005773 $greptype, '--regexp-ignore-case',
5774 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005775
5776 my $paging_nav = '';
5777 if ($page > 0) {
5778 $paging_nav .=
5779 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebski0270cd02008-02-26 13:22:07 +01005780 searchtext=>$searchtext,
5781 searchtype=>$searchtype)},
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005782 "first");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005783 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005784 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005785 -accesskey => "p", -title => "Alt-p"}, "prev");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005786 } else {
5787 $paging_nav .= "first";
5788 $paging_nav .= " &sdot; prev";
5789 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005790 my $next_link = '';
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005791 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005792 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005793 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005794 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005795 $paging_nav .= " &sdot; $next_link";
5796 } else {
5797 $paging_nav .= " &sdot; next";
5798 }
5799
5800 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005801 }
5802
5803 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5804 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005805 git_search_grep_body(\@commitlist, 0, 99, $next_link);
Kay Sieversc994d622005-08-07 20:27:18 +02005806 }
5807
Petr Baudis88ad7292006-10-24 05:15:46 +02005808 if ($searchtype eq 'pickaxe') {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005809 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5810 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5811
Jakub Narebski591ebf62007-11-19 14:16:11 +01005812 print "<table class=\"pickaxe search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005813 my $alternate = 1;
Kay Sieversc994d622005-08-07 20:27:18 +02005814 $/ = "\n";
Jakub Narebskic582aba2008-03-05 09:31:55 +01005815 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5816 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5817 ($search_use_regexp ? '--pickaxe-regex' : ());
Kay Sieversc994d622005-08-07 20:27:18 +02005818 undef %co;
5819 my @files;
5820 while (my $line = <$fd>) {
Jakub Narebskic582aba2008-03-05 09:31:55 +01005821 chomp $line;
5822 next unless $line;
5823
5824 my %set = parse_difftree_raw_line($line);
5825 if (defined $set{'commit'}) {
5826 # finish previous commit
Kay Sieversc994d622005-08-07 20:27:18 +02005827 if (%co) {
Kay Sieversc994d622005-08-07 20:27:18 +02005828 print "</td>\n" .
5829 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02005830 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005831 " | " .
5832 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02005833 print "</td>\n" .
5834 "</tr>\n";
5835 }
Jakub Narebskic582aba2008-03-05 09:31:55 +01005836
5837 if ($alternate) {
5838 print "<tr class=\"dark\">\n";
5839 } else {
5840 print "<tr class=\"light\">\n";
5841 }
5842 $alternate ^= 1;
5843 %co = parse_commit($set{'commit'});
5844 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5845 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5846 "<td><i>$author</i></td>\n" .
5847 "<td>" .
5848 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5849 -class => "list subject"},
5850 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5851 } elsif (defined $set{'to_id'}) {
5852 next if ($set{'to_id'} =~ m/^0{40}$/);
5853
5854 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5855 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5856 -class => "list"},
5857 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5858 "<br/>\n";
Kay Sieversc994d622005-08-07 20:27:18 +02005859 }
5860 }
5861 close $fd;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005862
Jakub Narebskic582aba2008-03-05 09:31:55 +01005863 # finish last commit (warning: repetition!)
5864 if (%co) {
5865 print "</td>\n" .
5866 "<td class=\"link\">" .
5867 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5868 " | " .
5869 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5870 print "</td>\n" .
5871 "</tr>\n";
5872 }
5873
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005874 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02005875 }
Petr Baudise7738552007-05-17 04:31:12 +02005876
5877 if ($searchtype eq 'grep') {
5878 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5879 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5880
Jakub Narebski591ebf62007-11-19 14:16:11 +01005881 print "<table class=\"grep_search\">\n";
Petr Baudise7738552007-05-17 04:31:12 +02005882 my $alternate = 1;
5883 my $matches = 0;
5884 $/ = "\n";
Petr Baudis0e559912008-02-26 13:22:08 +01005885 open my $fd, "-|", git_cmd(), 'grep', '-n',
5886 $search_use_regexp ? ('-E', '-i') : '-F',
5887 $searchtext, $co{'tree'};
Petr Baudise7738552007-05-17 04:31:12 +02005888 my $lastfile = '';
5889 while (my $line = <$fd>) {
5890 chomp $line;
5891 my ($file, $lno, $ltext, $binary);
5892 last if ($matches++ > 1000);
5893 if ($line =~ /^Binary file (.+) matches$/) {
5894 $file = $1;
5895 $binary = 1;
5896 } else {
5897 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5898 }
5899 if ($file ne $lastfile) {
5900 $lastfile and print "</td></tr>\n";
5901 if ($alternate++) {
5902 print "<tr class=\"dark\">\n";
5903 } else {
5904 print "<tr class=\"light\">\n";
5905 }
5906 print "<td class=\"list\">".
5907 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5908 file_name=>"$file"),
5909 -class => "list"}, esc_path($file));
5910 print "</td><td>\n";
5911 $lastfile = $file;
5912 }
5913 if ($binary) {
5914 print "<div class=\"binary\">Binary file</div>\n";
5915 } else {
5916 $ltext = untabify($ltext);
Petr Baudis0e559912008-02-26 13:22:08 +01005917 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
Petr Baudise7738552007-05-17 04:31:12 +02005918 $ltext = esc_html($1, -nbsp=>1);
5919 $ltext .= '<span class="match">';
5920 $ltext .= esc_html($2, -nbsp=>1);
5921 $ltext .= '</span>';
5922 $ltext .= esc_html($3, -nbsp=>1);
5923 } else {
5924 $ltext = esc_html($ltext, -nbsp=>1);
5925 }
5926 print "<div class=\"pre\">" .
5927 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5928 file_name=>"$file").'#l'.$lno,
5929 -class => "linenr"}, sprintf('%4i', $lno))
5930 . ' ' . $ltext . "</div>\n";
5931 }
5932 }
5933 if ($lastfile) {
5934 print "</td></tr>\n";
5935 if ($matches > 1000) {
5936 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5937 }
5938 } else {
5939 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5940 }
5941 close $fd;
5942
5943 print "</table>\n";
5944 }
Kay Sievers19806692005-08-07 20:26:27 +02005945 git_footer_html();
5946}
5947
Petr Baudis88ad7292006-10-24 05:15:46 +02005948sub git_search_help {
5949 git_header_html();
5950 git_print_page_nav('','', $hash,$hash,$hash);
5951 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01005952<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5953regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5954the pattern entered is recognized as the POSIX extended
5955<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5956insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02005957<dl>
5958<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005959<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005960EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005961 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02005962 if ($have_grep) {
5963 print <<EOT;
5964<dt><b>grep</b></dt>
5965<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01005966 a different one) are searched for the given pattern. On large trees, this search can take
5967a while and put some strain on the server, so please use it with some consideration. Note that
5968due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5969case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005970EOT
5971 }
5972 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02005973<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005974<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 +02005975<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005976<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 +02005977EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005978 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02005979 if ($have_pickaxe) {
5980 print <<EOT;
5981<dt><b>pickaxe</b></dt>
5982<dd>All commits that caused the string to appear or disappear from any file (changes that
5983added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01005984takes a lot of strain on the server, so please use it wisely. Note that since you may be
5985interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02005986EOT
5987 }
5988 print "</dl>\n";
5989 git_footer_html();
5990}
5991
Kay Sievers19806692005-08-07 20:26:27 +02005992sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005993 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005994 if (!defined $hash) {
5995 $hash = $head;
5996 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005997 if (!defined $page) {
5998 $page = 0;
5999 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02006000 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02006001
Giuseppe Bilottaec3e97b2008-08-08 16:12:11 +02006002 my $commit_hash = $hash;
6003 if (defined $hash_parent) {
6004 $commit_hash = "$hash_parent..$hash";
6005 }
6006 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02006007
Lea Wiemann1f684dc2008-05-28 01:25:42 +02006008 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006009 my $next_link = '';
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00006010 if ($#commitlist >= 100) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006011 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01006012 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00006013 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006014 }
Giuseppe Bilotta75bf2cb2008-12-18 08:13:19 +01006015 my $patch_max = gitweb_check_feature('patches');
6016 if ($patch_max) {
6017 if ($patch_max < 0 || @commitlist <= $patch_max) {
6018 $paging_nav .= " &sdot; " .
6019 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6020 "patches");
6021 }
6022 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006023
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006024 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02006025 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
6026 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02006027
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00006028 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02006029
Kay Sievers19806692005-08-07 20:26:27 +02006030 git_footer_html();
6031}
Jakub Narebski717b8312006-07-31 21:22:15 +02006032
6033## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006034## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02006035
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006036sub git_feed {
6037 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08006038 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006039
6040 # Atom: http://www.atomenabled.org/developers/syndication/
6041 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6042 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02006043 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006044 }
6045
6046 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6047 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01006048 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006049
6050 my %latest_commit;
6051 my %latest_date;
6052 my $content_type = "application/$format+xml";
6053 if (defined $cgi->http('HTTP_ACCEPT') &&
6054 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6055 # browser (feed reader) prefers text/xml
6056 $content_type = 'text/xml';
6057 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00006058 if (defined($commitlist[0])) {
6059 %latest_commit = %{$commitlist[0]};
Giuseppe Bilottacd956c72009-01-26 12:50:16 +01006060 my $latest_epoch = $latest_commit{'committer_epoch'};
6061 %latest_date = parse_date($latest_epoch);
6062 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6063 if (defined $if_modified) {
6064 my $since;
6065 if (eval { require HTTP::Date; 1; }) {
6066 $since = HTTP::Date::str2time($if_modified);
6067 } elsif (eval { require Time::ParseDate; 1; }) {
6068 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6069 }
6070 if (defined $since && $latest_epoch <= $since) {
6071 print $cgi->header(
6072 -type => $content_type,
6073 -charset => 'utf-8',
6074 -last_modified => $latest_date{'rfc2822'},
6075 -status => '304 Not Modified');
6076 return;
6077 }
6078 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006079 print $cgi->header(
6080 -type => $content_type,
6081 -charset => 'utf-8',
6082 -last_modified => $latest_date{'rfc2822'});
6083 } else {
6084 print $cgi->header(
6085 -type => $content_type,
6086 -charset => 'utf-8');
6087 }
6088
6089 # Optimization: skip generating the body if client asks only
6090 # for Last-Modified date.
6091 return if ($cgi->request_method() eq 'HEAD');
6092
6093 # header variables
6094 my $title = "$site_name - $project/$action";
6095 my $feed_type = 'log';
6096 if (defined $hash) {
6097 $title .= " - '$hash'";
6098 $feed_type = 'branch log';
6099 if (defined $file_name) {
6100 $title .= " :: $file_name";
6101 $feed_type = 'history';
6102 }
6103 } elsif (defined $file_name) {
6104 $title .= " - $file_name";
6105 $feed_type = 'history';
6106 }
6107 $title .= " $feed_type";
6108 my $descr = git_get_project_description($project);
6109 if (defined $descr) {
6110 $descr = esc_html($descr);
6111 } else {
6112 $descr = "$project " .
6113 ($format eq 'rss' ? 'RSS' : 'Atom') .
6114 " feed";
6115 }
6116 my $owner = git_get_project_owner($project);
6117 $owner = esc_html($owner);
6118
6119 #header
6120 my $alt_url;
6121 if (defined $file_name) {
6122 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6123 } elsif (defined $hash) {
6124 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6125 } else {
6126 $alt_url = href(-full=>1, action=>"summary");
6127 }
6128 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6129 if ($format eq 'rss') {
6130 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02006131<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6132<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02006133XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006134 print "<title>$title</title>\n" .
6135 "<link>$alt_url</link>\n" .
6136 "<description>$descr</description>\n" .
Giuseppe Bilotta3ac109a2009-01-26 12:50:13 +01006137 "<language>en</language>\n" .
6138 # project owner is responsible for 'editorial' content
6139 "<managingEditor>$owner</managingEditor>\n";
Giuseppe Bilotta1ba68ce2009-01-26 12:50:11 +01006140 if (defined $logo || defined $favicon) {
6141 # prefer the logo to the favicon, since RSS
6142 # doesn't allow both
6143 my $img = esc_url($logo || $favicon);
6144 print "<image>\n" .
6145 "<url>$img</url>\n" .
6146 "<title>$title</title>\n" .
6147 "<link>$alt_url</link>\n" .
6148 "</image>\n";
6149 }
Giuseppe Bilotta0cf31282009-01-26 12:50:14 +01006150 if (%latest_date) {
6151 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6152 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6153 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01006154 print "<generator>gitweb v.$version/$git_version</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006155 } elsif ($format eq 'atom') {
6156 print <<XML;
6157<feed xmlns="http://www.w3.org/2005/Atom">
6158XML
6159 print "<title>$title</title>\n" .
6160 "<subtitle>$descr</subtitle>\n" .
6161 '<link rel="alternate" type="text/html" href="' .
6162 $alt_url . '" />' . "\n" .
6163 '<link rel="self" type="' . $content_type . '" href="' .
6164 $cgi->self_url() . '" />' . "\n" .
6165 "<id>" . href(-full=>1) . "</id>\n" .
6166 # use project owner for feed author
6167 "<author><name>$owner</name></author>\n";
6168 if (defined $favicon) {
6169 print "<icon>" . esc_url($favicon) . "</icon>\n";
6170 }
6171 if (defined $logo_url) {
6172 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01006173 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006174 }
6175 if (! %latest_date) {
6176 # dummy date to keep the feed valid until commits trickle in:
6177 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6178 } else {
6179 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6180 }
Giuseppe Bilottaad59a7a2009-01-26 12:50:12 +01006181 print "<generator version='$version/$git_version'>gitweb</generator>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006182 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006183
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006184 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00006185 for (my $i = 0; $i <= $#commitlist; $i++) {
6186 my %co = %{$commitlist[$i]};
6187 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02006188 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01006189 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006190 last;
6191 }
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01006192 my %cd = parse_date($co{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006193
6194 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00006195 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02006196 $co{'parent'} || "--root",
6197 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02006198 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02006199 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02006200 close $fd
6201 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006202
6203 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01006204 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006205 if ($format eq 'rss') {
6206 print "<item>\n" .
6207 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6208 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6209 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6210 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6211 "<link>$co_url</link>\n" .
6212 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6213 "<content:encoded>" .
6214 "<![CDATA[\n";
6215 } elsif ($format eq 'atom') {
6216 print "<entry>\n" .
6217 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6218 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01006219 "<author>\n" .
6220 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6221 if ($co{'author_email'}) {
6222 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6223 }
6224 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006225 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01006226 "<contributor>\n" .
6227 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6228 if ($co{'committer_email'}) {
6229 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6230 }
6231 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006232 "<published>$cd{'iso-8601'}</published>\n" .
6233 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6234 "<id>$co_url</id>\n" .
6235 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6236 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6237 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006238 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006239 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006240 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006241 $line = esc_html($line);
6242 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006243 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006244 print "</pre><ul>\n";
6245 foreach my $difftree_line (@difftree) {
6246 my %difftree = parse_difftree_raw_line($difftree_line);
6247 next if !$difftree{'from_id'};
6248
6249 my $file = $difftree{'file'} || $difftree{'to_file'};
6250
6251 print "<li>" .
6252 "[" .
6253 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6254 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6255 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6256 file_name=>$file, file_parent=>$difftree{'from_file'}),
6257 -title => "diff"}, 'D');
6258 if ($have_blame) {
6259 print $cgi->a({-href => href(-full=>1, action=>"blame",
6260 file_name=>$file, hash_base=>$commit),
6261 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02006262 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006263 # if this is not a feed of a file history
6264 if (!defined $file_name || $file_name ne $file) {
6265 print $cgi->a({-href => href(-full=>1, action=>"history",
6266 file_name=>$file, hash=>$commit),
6267 -title => "history"}, 'H');
6268 }
6269 $file = esc_path($file);
6270 print "] ".
6271 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006272 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006273 if ($format eq 'rss') {
6274 print "</ul>]]>\n" .
6275 "</content:encoded>\n" .
6276 "</item>\n";
6277 } elsif ($format eq 'atom') {
6278 print "</ul>\n</div>\n" .
6279 "</content>\n" .
6280 "</entry>\n";
6281 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006282 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006283
6284 # end of feed
6285 if ($format eq 'rss') {
6286 print "</channel>\n</rss>\n";
6287 } elsif ($format eq 'atom') {
6288 print "</feed>\n";
6289 }
6290}
6291
6292sub git_rss {
6293 git_feed('rss');
6294}
6295
6296sub git_atom {
6297 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02006298}
6299
6300sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006301 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02006302
Giuseppe Bilottaae357852009-01-02 13:49:30 +01006303 print $cgi->header(
6304 -type => 'text/xml',
6305 -charset => 'utf-8',
6306 -content_disposition => 'inline; filename="opml.xml"');
6307
Jakub Narebski59b9f612006-08-22 23:42:53 +02006308 print <<XML;
6309<?xml version="1.0" encoding="utf-8"?>
6310<opml version="1.0">
6311<head>
Petr Baudis8be28902006-10-24 05:18:39 +02006312 <title>$site_name OPML Export</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02006313</head>
6314<body>
6315<outline text="git RSS feeds">
6316XML
Jakub Narebski717b8312006-07-31 21:22:15 +02006317
6318 foreach my $pr (@list) {
6319 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006320 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02006321 if (!defined $head) {
6322 next;
6323 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006324 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02006325 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02006326 if (!%co) {
6327 next;
6328 }
6329
6330 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
Giuseppe Bilottadf63fbb2009-01-02 13:15:28 +01006331 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6332 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02006333 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6334 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02006335 print <<XML;
6336</outline>
6337</body>
6338</opml>
6339XML
Jakub Narebski717b8312006-07-31 21:22:15 +02006340}