blob: ccbf5d47452d9ea14e44d20a1586097741a2451e [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 Bilottab65910f2008-09-29 15:07:42 +020030# if we're called with PATH_INFO, we have to strip that
31# from the URL to find our real URL
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +020032# we make $path_info global because it's also used later on
Alexander Gavrilovdde80d92008-11-06 01:10:07 +030033our $path_info = $ENV{"PATH_INFO"};
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +020034if ($path_info) {
Giuseppe Bilottab65910f2008-09-29 15:07:42 +020035 $my_url =~ s,\Q$path_info\E$,,;
36 $my_uri =~ s,\Q$path_info\E$,,;
37}
38
Alp Tokere130dda2006-07-12 23:55:10 +010039# core git executable to use
40# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070041our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020042
Kay Sieversb87d78d2005-08-07 20:21:04 +020043# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020044#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070045our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020046
Luke Luca5e9492007-10-16 20:45:25 -070047# fs traversing limit for getting project list
48# the number is relative to the projectroot
49our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
50
Kay Sieversb87d78d2005-08-07 20:21:04 +020051# target of the home link on top of all pages
Martin Waitz6132b7e2006-08-17 00:28:39 +020052our $home_link = $my_uri || "/";
Kay Sieversb87d78d2005-08-07 20:21:04 +020053
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090054# string of the home link on top of all pages
55our $home_link_str = "++GITWEB_HOME_LINK_STR++";
56
Alp Toker49da1da2006-07-11 21:10:26 +010057# name of your site or organization to appear in page titles
58# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020059our $site_name = "++GITWEB_SITENAME++"
60 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010061
Alan Chandlerb2d34762006-10-03 13:49:03 +010062# filename of html text to include at top of each page
63our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020064# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070065our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010066# filename of html text to include at bottom of each page
67our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020068
Alan Chandlerb2d34762006-10-03 13:49:03 +010069# URI of stylesheets
70our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +020071# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
72our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020073# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -070074our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +020075# URI of GIT favicon, assumed to be image/png type
76our $favicon = "++GITWEB_FAVICON++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +020077
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020078# URI and label (title) of GIT logo link
79#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
80#our $logo_label = "git documentation";
81our $logo_url = "http://git.or.cz/";
82our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -070083
Kay Sievers09bd7892005-08-07 20:21:23 +020084# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -070085our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020086
Michael Hendricks55feb122007-07-04 18:36:48 -060087# the width (in characters) of the projects list "Description" column
88our $projects_list_description_width = 25;
89
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +020090# default order of projects list
91# valid values are none, project, descr, owner, and age
92our $default_projects_order = "project";
93
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +020094# show repository only if this file exists
95# (only effective if this variable evaluates to true)
96our $export_ok = "++GITWEB_EXPORT_OK++";
97
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +030098# show repository only if this subroutine returns true
99# when given the path to the project, for example:
100# sub { return -e "$_[0]/git-daemon-export-ok"; }
101our $export_auth_hook = undef;
102
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200103# only allow viewing of repositories also shown on the overview page
104our $strict_export = "++GITWEB_STRICT_EXPORT++";
105
Jakub Narebski19a87212006-08-15 23:03:17 +0200106# list of git base URLs used for URL to where fetch project from,
107# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +0200108our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +0200109
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200110# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200111our $default_blob_plain_mimetype = 'text/plain';
112our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +0200113
Petr Baudis2d007372006-06-18 00:01:06 +0200114# file to use for guessing MIME types before trying /etc/mime.types
115# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200116our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200117
Martin Koegler00f429a2007-06-03 17:42:44 +0200118# assume this charset if line contains non-UTF-8 characters;
119# it should be valid encoding (see Encoding::Supported(3pm) for list),
120# for which encoding all byte sequences are valid, for example
121# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
122# could be even 'utf-8' for the old behavior)
123our $fallback_encoding = 'latin1';
124
Jakub Narebski69a9b412007-07-20 02:15:09 +0200125# rename detection options for git-diff and git-diff-tree
126# - default is '-M', with the cost proportional to
127# (number of removed files) * (number of new files).
128# - more costly is '-C' (which implies '-M'), with the cost proportional to
129# (number of changed files + number of removed files) * (number of new files)
130# - even more costly is '-C', '--find-copies-harder' with cost
131# (number of files in the original tree) * (number of new files)
132# - one might want to include '-B' option, e.g. '-B', '-M'
133our @diff_opts = ('-M'); # taken from git_commit
134
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200135# information about snapshot formats that gitweb is capable of serving
136our %known_snapshot_formats = (
137 # name => {
138 # 'display' => display name,
139 # 'type' => mime type,
140 # 'suffix' => filename suffix,
141 # 'format' => --format for git-archive,
142 # 'compressor' => [compressor command and arguments]
143 # (array reference, optional)}
144 #
145 'tgz' => {
146 'display' => 'tar.gz',
147 'type' => 'application/x-gzip',
148 'suffix' => '.tar.gz',
149 'format' => 'tar',
150 'compressor' => ['gzip']},
151
152 'tbz2' => {
153 'display' => 'tar.bz2',
154 'type' => 'application/x-bzip2',
155 'suffix' => '.tar.bz2',
156 'format' => 'tar',
157 'compressor' => ['bzip2']},
158
159 'zip' => {
160 'display' => 'zip',
161 'type' => 'application/x-zip',
162 'suffix' => '.zip',
163 'format' => 'zip'},
164);
165
166# Aliases so we understand old gitweb.snapshot values in repository
167# configuration.
168our %known_snapshot_format_aliases = (
169 'gzip' => 'tgz',
170 'bzip2' => 'tbz2',
171
172 # backward compatibility: legacy gitweb config support
173 'x-gzip' => undef, 'gz' => undef,
174 'x-bzip2' => undef, 'bz2' => undef,
175 'x-zip' => undef, '' => undef,
176);
177
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530178# You define site-wide feature defaults here; override them with
179# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200180our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200181 # feature => {
182 # 'sub' => feature-sub (subroutine),
183 # 'override' => allow-override (boolean),
184 # 'default' => [ default options...] (array reference)}
185 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200186 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200187 # then feature-sub will be called with default options as parameters;
188 # return value of feature-sub indicates if to enable specified feature
189 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200190 # if there is no 'sub' key (no feature-sub), then feature cannot be
191 # overriden
192 #
Giuseppe Bilottaff3c0ff2008-12-02 14:57:28 -0800193 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
194 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
195 # is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530196
Petr Baudis45a3b122006-10-07 15:17:47 +0200197 # Enable the 'blame' blob view, showing the last commit that modified
198 # each line in the file. This can be very CPU-intensive.
199
200 # To enable system wide have in $GITWEB_CONFIG
201 # $feature{'blame'}{'default'} = [1];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'blame'}{'override'} = 1;
204 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200205 'blame' => {
206 'sub' => \&feature_blame,
207 'override' => 0,
208 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530209
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200210 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200211 # tree. This can potentially generate high traffic if you have large
212 # project.
213
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200214 # Value is a list of formats defined in %known_snapshot_formats that
215 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200216 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200217 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200218 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100219 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200220 # and in project config, a comma-separated list of formats or "none"
221 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200222 'snapshot' => {
223 'sub' => \&feature_snapshot,
224 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200225 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200226
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000227 # Enable text search, which will list the commits which match author,
228 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200229 # Project specific override is not supported.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000230 'search' => {
231 'override' => 0,
232 'default' => [1]},
233
Petr Baudise7738552007-05-17 04:31:12 +0200234 # Enable grep search, which will list the files in currently selected
235 # tree containing the given string. Enabled by default. This can be
236 # potentially CPU-intensive, of course.
237
238 # To enable system wide have in $GITWEB_CONFIG
239 # $feature{'grep'}{'default'} = [1];
240 # To have project specific config enable override in $GITWEB_CONFIG
241 # $feature{'grep'}{'override'} = 1;
242 # and in project config gitweb.grep = 0|1;
243 'grep' => {
Jakub Narebskibcc6a832008-12-07 10:36:36 +0100244 'sub' => \&feature_grep,
Petr Baudise7738552007-05-17 04:31:12 +0200245 'override' => 0,
246 'default' => [1]},
247
Petr Baudis45a3b122006-10-07 15:17:47 +0200248 # Enable the pickaxe search, which will list the commits that modified
249 # a given string in a file. This can be practical and quite faster
250 # alternative to 'blame', but still potentially CPU-intensive.
251
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pickaxe'}{'default'} = [1];
254 # To have project specific config enable override in $GITWEB_CONFIG
255 # $feature{'pickaxe'}{'override'} = 1;
256 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200257 'pickaxe' => {
258 'sub' => \&feature_pickaxe,
259 'override' => 0,
260 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200261
Petr Baudis45a3b122006-10-07 15:17:47 +0200262 # Make gitweb use an alternative format of the URLs which can be
263 # more readable and natural-looking: project name is embedded
264 # directly in the path and the query string contains other
265 # auxiliary information. All gitweb installations recognize
266 # URL in either format; this configures in which formats gitweb
267 # generates links.
268
269 # To enable system wide have in $GITWEB_CONFIG
270 # $feature{'pathinfo'}{'default'} = [1];
271 # Project specific override is not supported.
272
273 # Note that you will need to change the default location of CSS,
274 # favicon, logo and possibly other files to an absolute URL. Also,
275 # if gitweb.cgi serves as your indexfile, you will need to force
276 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200277 'pathinfo' => {
278 'override' => 0,
279 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200280
281 # Make gitweb consider projects in project root subdirectories
282 # to be forks of existing projects. Given project $projname.git,
283 # projects matching $projname/*.git will not be shown in the main
284 # projects list, instead a '+' mark will be added to $projname
285 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200286 # all the forks. If project list is taken from a file, forks have
287 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200288
289 # To enable system wide have in $GITWEB_CONFIG
290 # $feature{'forks'}{'default'} = [1];
291 # Project specific override is not supported.
292 'forks' => {
293 'override' => 0,
294 'default' => [0]},
Petr Baudisd627f682008-10-02 16:36:52 +0200295
296 # Insert custom links to the action bar of all project pages.
297 # This enables you mainly to link to third-party scripts integrating
298 # into gitweb; e.g. git-browser for graphical history representation
299 # or custom web-based repository administration interface.
300
301 # The 'default' value consists of a list of triplets in the form
302 # (label, link, position) where position is the label after which
Jakub Narebski2b11e052008-10-12 00:02:32 +0200303 # to insert the link and link is a format string where %n expands
Petr Baudisd627f682008-10-02 16:36:52 +0200304 # to the project name, %f to the project path within the filesystem,
305 # %h to the current hash (h gitweb parameter) and %b to the current
Jakub Narebski2b11e052008-10-12 00:02:32 +0200306 # hash base (hb gitweb parameter); %% expands to %.
Petr Baudisd627f682008-10-02 16:36:52 +0200307
308 # To enable system wide have in $GITWEB_CONFIG e.g.
309 # $feature{'actions'}{'default'} = [('graphiclog',
310 # '/git-browser/by-commit.html?r=%n', 'summary')];
311 # Project specific override is not supported.
312 'actions' => {
313 'override' => 0,
314 'default' => []},
Shawn O. Pearce3e3d4ee2008-10-03 07:41:25 -0700315
Petr Baudisaed93de2008-10-02 17:13:02 +0200316 # Allow gitweb scan project content tags described in ctags/
317 # of project repository, and display the popular Web 2.0-ish
318 # "tag cloud" near the project list. Note that this is something
319 # COMPLETELY different from the normal Git tags.
320
321 # gitweb by itself can show existing tags, but it does not handle
322 # tagging itself; you need an external application for that.
323 # For an example script, check Girocco's cgi/tagproj.cgi.
324 # You may want to install the HTML::TagCloud Perl module to get
325 # a pretty tag cloud instead of just a list of tags.
326
327 # To enable system wide have in $GITWEB_CONFIG
328 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
329 # Project specific override is not supported.
330 'ctags' => {
331 'override' => 0,
332 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530333);
334
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800335sub gitweb_get_feature {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530336 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200337 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200338 my ($sub, $override, @defaults) = (
339 $feature{$name}{'sub'},
340 $feature{$name}{'override'},
341 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530342 if (!$override) { return @defaults; }
Martin Waitza9455912006-10-03 20:07:43 +0200343 if (!defined $sub) {
344 warn "feature $name is not overrideable";
345 return @defaults;
346 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530347 return $sub->(@defaults);
348}
349
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800350# A wrapper to check if a given feature is enabled.
351# With this, you can say
352#
353# my $bool_feat = gitweb_check_feature('bool_feat');
354# gitweb_check_feature('bool_feat') or somecode;
355#
356# instead of
357#
358# my ($bool_feat) = gitweb_get_feature('bool_feat');
359# (gitweb_get_feature('bool_feat'))[0] or somecode;
360#
361sub gitweb_check_feature {
362 return (gitweb_get_feature(@_))[0];
363}
364
365
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530366sub feature_blame {
367 my ($val) = git_get_project_config('blame', '--bool');
368
369 if ($val eq 'true') {
370 return 1;
371 } elsif ($val eq 'false') {
372 return 0;
373 }
374
375 return $_[0];
376}
377
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530378sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200379 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530380
381 my ($val) = git_get_project_config('snapshot');
382
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200383 if ($val) {
384 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530385 }
386
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200387 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700388}
389
Petr Baudise7738552007-05-17 04:31:12 +0200390sub feature_grep {
391 my ($val) = git_get_project_config('grep', '--bool');
392
393 if ($val eq 'true') {
394 return (1);
395 } elsif ($val eq 'false') {
396 return (0);
397 }
398
399 return ($_[0]);
400}
401
Jakub Narebski04f7a942006-09-11 00:29:27 +0200402sub feature_pickaxe {
403 my ($val) = git_get_project_config('pickaxe', '--bool');
404
405 if ($val eq 'true') {
406 return (1);
407 } elsif ($val eq 'false') {
408 return (0);
409 }
410
411 return ($_[0]);
412}
413
Junio C Hamano2172ce42006-10-03 02:30:47 -0700414# checking HEAD file with -e is fragile if the repository was
415# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
416# and then pruned.
417sub check_head_link {
418 my ($dir) = @_;
419 my $headfile = "$dir/HEAD";
420 return ((-e $headfile) ||
421 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
422}
423
424sub check_export_ok {
425 my ($dir) = @_;
426 return (check_head_link($dir) &&
Alexander Gavrilovdd7f5f12008-11-06 01:36:23 +0300427 (!$export_ok || -e "$dir/$export_ok") &&
428 (!$export_auth_hook || $export_auth_hook->($dir)));
Junio C Hamano2172ce42006-10-03 02:30:47 -0700429}
430
Jakub Narebskia7817852007-07-22 23:41:20 +0200431# process alternate names for backward compatibility
432# filter out unsupported (unknown) snapshot formats
433sub filter_snapshot_fmts {
434 my @fmts = @_;
435
436 @fmts = map {
437 exists $known_snapshot_format_aliases{$_} ?
438 $known_snapshot_format_aliases{$_} : $_} @fmts;
439 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
440
441}
442
Junio C Hamano06c084d2006-08-02 13:50:20 -0700443our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Gerrit Pape17a8b252008-03-26 18:11:19 +0000444if (-e $GITWEB_CONFIG) {
445 do $GITWEB_CONFIG;
446} else {
447 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
448 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
449}
Jeff Kingc8d138a2006-08-02 15:23:34 -0400450
451# version of the core git binary
Jakub Narebski66115d32008-06-14 20:37:59 +0200452our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
Jeff Kingc8d138a2006-08-02 15:23:34 -0400453
454$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400455
Jakub Narebski154b4d72006-08-05 12:55:20 +0200456# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200457# input validation and dispatch
Kay Sieversb87d78d2005-08-07 20:21:04 +0200458
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200459# input parameters can be collected from a variety of sources (presently, CGI
460# and PATH_INFO), so we define an %input_params hash that collects them all
461# together during validation: this allows subsequent uses (e.g. href()) to be
462# agnostic of the parameter origin
Kay Sievers6191f8e2005-08-07 20:19:56 +0200463
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300464our %input_params = ();
Martin Waitz5c95fab2006-08-17 00:28:38 +0200465
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200466# input parameters are stored with the long parameter name as key. This will
467# also be used in the href subroutine to convert parameters to their CGI
468# equivalent, and since the href() usage is the most frequent one, we store
469# the name -> CGI key mapping here, instead of the reverse.
470#
471# XXX: Warning: If you touch this, check the search form for updating,
472# too.
Jakub Narebski24d06932006-09-26 01:57:02 +0200473
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300474our @cgi_param_mapping = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200475 project => "p",
476 action => "a",
477 file_name => "f",
478 file_parent => "fp",
479 hash => "h",
480 hash_parent => "hp",
481 hash_base => "hb",
482 hash_parent_base => "hpb",
483 page => "pg",
484 order => "o",
485 searchtext => "s",
486 searchtype => "st",
487 snapshot_format => "sf",
488 extra_options => "opt",
489 search_use_regexp => "sr",
Miklos Vajna868bc062007-07-12 20:39:27 +0200490);
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300491our %cgi_param_mapping = @cgi_param_mapping;
Miklos Vajna868bc062007-07-12 20:39:27 +0200492
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200493# we will also need to know the possible actions, for validation
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300494our %actions = (
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +0200495 "blame" => \&git_blame,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200496 "blobdiff" => \&git_blobdiff,
497 "blobdiff_plain" => \&git_blobdiff_plain,
498 "blob" => \&git_blob,
499 "blob_plain" => \&git_blob_plain,
500 "commitdiff" => \&git_commitdiff,
501 "commitdiff_plain" => \&git_commitdiff_plain,
502 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200503 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200504 "heads" => \&git_heads,
505 "history" => \&git_history,
506 "log" => \&git_log,
507 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100508 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200509 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200510 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200511 "shortlog" => \&git_shortlog,
512 "summary" => \&git_summary,
513 "tag" => \&git_tag,
514 "tags" => \&git_tags,
515 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530516 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100517 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200518 # those below don't need $project
519 "opml" => \&git_opml,
520 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200521 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200522);
523
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200524# finally, we have the hash of allowed extra_options for the commands that
525# allow them
Alexander Gavrilovdde80d92008-11-06 01:10:07 +0300526our %allowed_options = (
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200527 "--no-merges" => [ qw(rss atom log shortlog history) ],
528);
529
530# fill %input_params with the CGI parameters. All values except for 'opt'
531# should be single values, but opt can be an array. We should probably
532# build an array of parameters that can be multi-valued, but since for the time
533# being it's only this one, we just single it out
534while (my ($name, $symbol) = each %cgi_param_mapping) {
535 if ($symbol eq 'opt') {
536 $input_params{$name} = [ $cgi->param($symbol) ];
537 } else {
538 $input_params{$name} = $cgi->param($symbol);
539 }
540}
541
542# now read PATH_INFO and update the parameter list for missing parameters
543sub evaluate_path_info {
544 return if defined $input_params{'project'};
545 return if !$path_info;
546 $path_info =~ s,^/+,,;
547 return if !$path_info;
548
549 # find which part of PATH_INFO is project
550 my $project = $path_info;
551 $project =~ s,/+$,,;
552 while ($project && !check_head_link("$projectroot/$project")) {
553 $project =~ s,/*[^/]*$,,;
554 }
555 return unless $project;
556 $input_params{'project'} = $project;
557
558 # do not change any parameters if an action is given using the query string
559 return if $input_params{'action'};
560 $path_info =~ s,^\Q$project\E/*,,;
561
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200562 # next, check if we have an action
563 my $action = $path_info;
564 $action =~ s,/.*$,,;
565 if (exists $actions{$action}) {
566 $path_info =~ s,^$action/*,,;
567 $input_params{'action'} = $action;
568 }
569
570 # list of actions that want hash_base instead of hash, but can have no
571 # pathname (f) parameter
572 my @wants_base = (
573 'tree',
574 'history',
575 );
576
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200577 # we want to catch
578 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
579 my ($parentrefname, $parentpathname, $refname, $pathname) =
580 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
581
582 # first, analyze the 'current' part
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200583 if (defined $pathname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200584 # we got "branch:filename" or "branch:dir/"
585 # we could use git_get_type(branch:pathname), but:
586 # - it needs $git_dir
587 # - it does a git() call
588 # - the convention of terminating directories with a slash
589 # makes it superfluous
590 # - embedding the action in the PATH_INFO would make it even
591 # more superfluous
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200592 $pathname =~ s,^/+,,;
593 if (!$pathname || substr($pathname, -1) eq "/") {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200594 $input_params{'action'} ||= "tree";
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200595 $pathname =~ s,/$,,;
596 } else {
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200597 # the default action depends on whether we had parent info
598 # or not
599 if ($parentrefname) {
600 $input_params{'action'} ||= "blobdiff_plain";
601 } else {
602 $input_params{'action'} ||= "blob_plain";
603 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200604 }
605 $input_params{'hash_base'} ||= $refname;
606 $input_params{'file_name'} ||= $pathname;
607 } elsif (defined $refname) {
Giuseppe Bilottad8c28822008-10-21 21:34:50 +0200608 # we got "branch". In this case we have to choose if we have to
609 # set hash or hash_base.
610 #
611 # Most of the actions without a pathname only want hash to be
612 # set, except for the ones specified in @wants_base that want
613 # hash_base instead. It should also be noted that hand-crafted
614 # links having 'history' as an action and no pathname or hash
615 # set will fail, but that happens regardless of PATH_INFO.
616 $input_params{'action'} ||= "shortlog";
617 if (grep { $_ eq $input_params{'action'} } @wants_base) {
618 $input_params{'hash_base'} ||= $refname;
619 } else {
620 $input_params{'hash'} ||= $refname;
621 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200622 }
Giuseppe Bilottab0be3832008-10-21 21:34:53 +0200623
624 # next, handle the 'parent' part, if present
625 if (defined $parentrefname) {
626 # a missing pathspec defaults to the 'current' filename, allowing e.g.
627 # someproject/blobdiff/oldrev..newrev:/filename
628 if ($parentpathname) {
629 $parentpathname =~ s,^/+,,;
630 $parentpathname =~ s,/$,,;
631 $input_params{'file_parent'} ||= $parentpathname;
632 } else {
633 $input_params{'file_parent'} ||= $input_params{'file_name'};
634 }
635 # we assume that hash_parent_base is wanted if a path was specified,
636 # or if the action wants hash_base instead of hash
637 if (defined $input_params{'file_parent'} ||
638 grep { $_ eq $input_params{'action'} } @wants_base) {
639 $input_params{'hash_parent_base'} ||= $parentrefname;
640 } else {
641 $input_params{'hash_parent'} ||= $parentrefname;
642 }
643 }
Giuseppe Bilotta1ec2fb52008-11-02 10:21:38 +0100644
645 # for the snapshot action, we allow URLs in the form
646 # $project/snapshot/$hash.ext
647 # where .ext determines the snapshot and gets removed from the
648 # passed $refname to provide the $hash.
649 #
650 # To be able to tell that $refname includes the format extension, we
651 # require the following two conditions to be satisfied:
652 # - the hash input parameter MUST have been set from the $refname part
653 # of the URL (i.e. they must be equal)
654 # - the snapshot format MUST NOT have been defined already (e.g. from
655 # CGI parameter sf)
656 # It's also useless to try any matching unless $refname has a dot,
657 # so we check for that too
658 if (defined $input_params{'action'} &&
659 $input_params{'action'} eq 'snapshot' &&
660 defined $refname && index($refname, '.') != -1 &&
661 $refname eq $input_params{'hash'} &&
662 !defined $input_params{'snapshot_format'}) {
663 # We loop over the known snapshot formats, checking for
664 # extensions. Allowed extensions are both the defined suffix
665 # (which includes the initial dot already) and the snapshot
666 # format key itself, with a prepended dot
667 while (my ($fmt, %opt) = each %known_snapshot_formats) {
668 my $hash = $refname;
669 my $sfx;
670 $hash =~ s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//;
671 next unless $sfx = $1;
672 # a valid suffix was found, so set the snapshot format
673 # and reset the hash parameter
674 $input_params{'snapshot_format'} = $fmt;
675 $input_params{'hash'} = $hash;
676 # we also set the format suffix to the one requested
677 # in the URL: this way a request for e.g. .tgz returns
678 # a .tgz instead of a .tar.gz
679 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
680 last;
681 }
682 }
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200683}
684evaluate_path_info();
685
686our $action = $input_params{'action'};
687if (defined $action) {
688 if (!validate_action($action)) {
689 die_error(400, "Invalid action parameter");
690 }
691}
692
693# parameters which are pathnames
694our $project = $input_params{'project'};
695if (defined $project) {
696 if (!validate_project($project)) {
697 undef $project;
698 die_error(404, "No such project");
699 }
700}
701
702our $file_name = $input_params{'file_name'};
703if (defined $file_name) {
704 if (!validate_pathname($file_name)) {
705 die_error(400, "Invalid file parameter");
706 }
707}
708
709our $file_parent = $input_params{'file_parent'};
710if (defined $file_parent) {
711 if (!validate_pathname($file_parent)) {
712 die_error(400, "Invalid file parent parameter");
713 }
714}
715
716# parameters which are refnames
717our $hash = $input_params{'hash'};
718if (defined $hash) {
719 if (!validate_refname($hash)) {
720 die_error(400, "Invalid hash parameter");
721 }
722}
723
724our $hash_parent = $input_params{'hash_parent'};
725if (defined $hash_parent) {
726 if (!validate_refname($hash_parent)) {
727 die_error(400, "Invalid hash parent parameter");
728 }
729}
730
731our $hash_base = $input_params{'hash_base'};
732if (defined $hash_base) {
733 if (!validate_refname($hash_base)) {
734 die_error(400, "Invalid hash base parameter");
735 }
736}
737
738our @extra_options = @{$input_params{'extra_options'}};
739# @extra_options is always defined, since it can only be (currently) set from
740# CGI, and $cgi->param() returns the empty array in array context if the param
741# is not set
742foreach my $opt (@extra_options) {
743 if (not exists $allowed_options{$opt}) {
744 die_error(400, "Invalid option parameter");
745 }
746 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
747 die_error(400, "Invalid option parameter for this action");
748 }
749}
750
751our $hash_parent_base = $input_params{'hash_parent_base'};
752if (defined $hash_parent_base) {
753 if (!validate_refname($hash_parent_base)) {
754 die_error(400, "Invalid hash parent base parameter");
755 }
756}
757
758# other parameters
759our $page = $input_params{'page'};
760if (defined $page) {
761 if ($page =~ m/[^0-9]/) {
762 die_error(400, "Invalid page parameter");
763 }
764}
765
766our $searchtype = $input_params{'searchtype'};
767if (defined $searchtype) {
768 if ($searchtype =~ m/[^a-z]/) {
769 die_error(400, "Invalid searchtype parameter");
770 }
771}
772
773our $search_use_regexp = $input_params{'search_use_regexp'};
774
775our $searchtext = $input_params{'searchtext'};
776our $search_regexp;
777if (defined $searchtext) {
778 if (length($searchtext) < 2) {
779 die_error(403, "At least two characters are required for search parameter");
780 }
781 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
782}
783
784# path to the current git repository
785our $git_dir;
786$git_dir = "$projectroot/$project" if $project;
787
Giuseppe Bilotta5e166842008-11-02 10:21:37 +0100788# list of supported snapshot formats
Junio C Hamanoa7c5a282008-11-29 13:02:08 -0800789our @snapshot_fmts = gitweb_get_feature('snapshot');
Giuseppe Bilotta5e166842008-11-02 10:21:37 +0100790@snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
791
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200792# dispatch
Gerrit Pape7f9778b2007-05-10 07:32:07 +0000793if (!defined $action) {
794 if (defined $hash) {
795 $action = git_get_type($hash);
796 } elsif (defined $hash_base && defined $file_name) {
797 $action = git_get_type("$hash_base:$file_name");
798 } elsif (defined $project) {
799 $action = 'summary';
800 } else {
801 $action = 'project_list';
802 }
Jakub Narebski77a153f2006-08-22 16:59:20 +0200803}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200804if (!defined($actions{$action})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +0200805 die_error(400, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200806}
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200807if ($action !~ m/^(opml|project_list|project_index)$/ &&
808 !$project) {
Lea Wiemann074afaa2008-06-19 22:03:21 +0200809 die_error(400, "Project needed");
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200810}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200811$actions{$action}->();
812exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200813
Jakub Narebski717b8312006-07-31 21:22:15 +0200814## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200815## action links
816
Jakub Narebski35621982008-04-20 22:09:48 +0200817sub href (%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200818 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +0100819 # default is to use -absolute url() i.e. $my_uri
820 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +0200821
Jakub Narebskiafa9b622008-02-14 09:22:30 +0100822 $params{'project'} = $project unless exists $params{'project'};
823
Jakub Narebski1cad2832007-11-01 13:06:27 +0100824 if ($params{-replay}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200825 while (my ($name, $symbol) = each %cgi_param_mapping) {
Jakub Narebski1cad2832007-11-01 13:06:27 +0100826 if (!exists $params{$name}) {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200827 $params{$name} = $input_params{$name};
Jakub Narebski1cad2832007-11-01 13:06:27 +0100828 }
829 }
830 }
831
Giuseppe Bilotta25b27902008-11-29 13:07:29 -0800832 my $use_pathinfo = gitweb_check_feature('pathinfo');
Martin Waitz9e756902006-10-01 23:57:48 +0200833 if ($use_pathinfo) {
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200834 # try to put as many parameters as possible in PATH_INFO:
835 # - project name
836 # - action
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200837 # - hash_parent or hash_parent_base:/file_parent
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +0200838 # - hash or hash_base:/filename
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100839 # - the snapshot_format as an appropriate suffix
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200840
841 # When the script is the root DirectoryIndex for the domain,
842 # $href here would be something like http://gitweb.example.com/
843 # Thus, we strip any trailing / from $href, to spare us double
844 # slashes in the final URL
845 $href =~ s,/$,,;
846
847 # Then add the project name, if present
martin f. krafft85d17a12008-04-20 23:23:38 +0200848 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
Martin Waitz9e756902006-10-01 23:57:48 +0200849 delete $params{'project'};
850
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100851 # since we destructively absorb parameters, we keep this
852 # boolean that remembers if we're handling a snapshot
853 my $is_snapshot = $params{'action'} eq 'snapshot';
854
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200855 # Summary just uses the project path URL, any other action is
856 # added to the URL
857 if (defined $params{'action'}) {
858 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
Martin Waitz9e756902006-10-01 23:57:48 +0200859 delete $params{'action'};
860 }
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200861
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200862 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
863 # stripping nonexistent or useless pieces
864 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
865 || $params{'hash_parent'} || $params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200866 if (defined $params{'hash_base'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200867 if (defined $params{'hash_parent_base'}) {
868 $href .= esc_url($params{'hash_parent_base'});
869 # skip the file_parent if it's the same as the file_name
870 delete $params{'file_parent'} if $params{'file_parent'} eq $params{'file_name'};
871 if (defined $params{'file_parent'} && $params{'file_parent'} !~ /\.\./) {
872 $href .= ":/".esc_url($params{'file_parent'});
873 delete $params{'file_parent'};
874 }
875 $href .= "..";
876 delete $params{'hash_parent'};
877 delete $params{'hash_parent_base'};
878 } elsif (defined $params{'hash_parent'}) {
879 $href .= esc_url($params{'hash_parent'}). "..";
880 delete $params{'hash_parent'};
881 }
882
883 $href .= esc_url($params{'hash_base'});
884 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
Giuseppe Bilotta3550ea72008-10-21 21:34:52 +0200885 $href .= ":/".esc_url($params{'file_name'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200886 delete $params{'file_name'};
887 }
888 delete $params{'hash'};
889 delete $params{'hash_base'};
890 } elsif (defined $params{'hash'}) {
Giuseppe Bilotta8db49a72008-10-21 21:34:54 +0200891 $href .= esc_url($params{'hash'});
Giuseppe Bilottab02bd7a2008-10-21 21:34:51 +0200892 delete $params{'hash'};
893 }
Giuseppe Bilottac752a0e2008-11-02 10:21:39 +0100894
895 # If the action was a snapshot, we can absorb the
896 # snapshot_format parameter too
897 if ($is_snapshot) {
898 my $fmt = $params{'snapshot_format'};
899 # snapshot_format should always be defined when href()
900 # is called, but just in case some code forgets, we
901 # fall back to the default
902 $fmt ||= $snapshot_fmts[0];
903 $href .= $known_snapshot_formats{$fmt}{'suffix'};
904 delete $params{'snapshot_format'};
905 }
Martin Waitz9e756902006-10-01 23:57:48 +0200906 }
907
908 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +0200909 my @result = ();
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200910 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
911 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
Jakub Narebski498fe002006-08-22 19:05:25 +0200912 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +0200913 if (ref($params{$name}) eq "ARRAY") {
914 foreach my $par (@{$params{$name}}) {
915 push @result, $symbol . "=" . esc_param($par);
916 }
917 } else {
918 push @result, $symbol . "=" . esc_param($params{$name});
919 }
Jakub Narebski498fe002006-08-22 19:05:25 +0200920 }
921 }
Martin Waitz9e756902006-10-01 23:57:48 +0200922 $href .= "?" . join(';', @result) if scalar @result;
923
924 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +0200925}
926
927
928## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200929## validation, quoting/unquoting and escaping
930
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200931sub validate_action {
932 my $input = shift || return undef;
933 return undef unless exists $actions{$input};
934 return $input;
935}
936
937sub validate_project {
938 my $input = shift || return undef;
939 if (!validate_pathname($input) ||
940 !(-d "$projectroot/$input") ||
Alexander Gavrilovec26f092008-11-06 01:15:56 +0300941 !check_export_ok("$projectroot/$input") ||
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +0200942 ($strict_export && !project_in_list($input))) {
943 return undef;
944 } else {
945 return $input;
946 }
947}
948
Jakub Narebski24d06932006-09-26 01:57:02 +0200949sub validate_pathname {
950 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +0200951
Jakub Narebski24d06932006-09-26 01:57:02 +0200952 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
953 # at the beginning, at the end, and between slashes.
954 # also this catches doubled slashes
955 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
956 return undef;
957 }
958 # no null characters
959 if ($input =~ m!\0!) {
960 return undef;
961 }
962 return $input;
963}
964
965sub validate_refname {
966 my $input = shift || return undef;
967
968 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +0200969 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
970 return $input;
971 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200972 # it must be correct pathname
973 $input = validate_pathname($input)
974 or return undef;
975 # restrictions on ref name according to git-check-ref-format
976 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200977 return undef;
978 }
979 return $input;
980}
981
Martin Koegler00f429a2007-06-03 17:42:44 +0200982# decode sequences of octets in utf8 into Perl's internal form,
983# which is utf-8 with utf8 flag set if needed. gitweb writes out
984# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
985sub to_utf8 {
986 my $str = shift;
İsmail Dönmeze5d3de52007-12-04 10:55:41 +0200987 if (utf8::valid($str)) {
988 utf8::decode($str);
989 return $str;
Martin Koegler00f429a2007-06-03 17:42:44 +0200990 } else {
991 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
992 }
993}
994
Kay Sievers232ff552005-11-24 16:56:55 +0100995# quote unsafe chars, but keep the slash, even when it's not
996# correct, but quoted slashes look too horrible in bookmarks
997sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100998 my $str = shift;
Petr Baudisa2f3db22006-09-24 00:18:41 +0200999 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +01001000 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +01001001 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +01001002 return $str;
1003}
1004
Jakub Narebskif93bff82006-09-26 01:58:41 +02001005# quote unsafe chars in whole URL, so some charactrs cannot be quoted
1006sub esc_url {
1007 my $str = shift;
1008 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1009 $str =~ s/\+/%2B/g;
1010 $str =~ s/ /\+/g;
1011 return $str;
1012}
1013
Kay Sievers232ff552005-11-24 16:56:55 +01001014# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski6255ef02006-11-01 14:33:21 +01001015sub esc_html ($;%) {
Kay Sievers40c13812005-11-19 17:41:29 +01001016 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +01001017 my %opts = @_;
1018
Martin Koegler00f429a2007-06-03 17:42:44 +02001019 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001020 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +01001021 if ($opts{'-nbsp'}) {
1022 $str =~ s/ /&nbsp;/g;
1023 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -08001024 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +01001025 return $str;
1026}
1027
Jakub Narebski391862e2006-11-25 09:43:59 +01001028# quote control characters and escape filename to HTML
1029sub esc_path {
1030 my $str = shift;
1031 my %opts = @_;
1032
Martin Koegler00f429a2007-06-03 17:42:44 +02001033 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +08001034 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +01001035 if ($opts{'-nbsp'}) {
1036 $str =~ s/ /&nbsp;/g;
1037 }
1038 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1039 return $str;
1040}
1041
1042# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001043sub quot_cec {
1044 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001045 my %opts = @_;
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001046 my %es = ( # character escape codes, aka escape sequences
Jakub Narebskic84c4832008-02-17 18:48:13 +01001047 "\t" => '\t', # tab (HT)
1048 "\n" => '\n', # line feed (LF)
1049 "\r" => '\r', # carrige return (CR)
1050 "\f" => '\f', # form feed (FF)
1051 "\b" => '\b', # backspace (BS)
1052 "\a" => '\a', # alarm (bell) (BEL)
1053 "\e" => '\e', # escape (ESC)
1054 "\013" => '\v', # vertical tab (VT)
1055 "\000" => '\0', # nul character (NUL)
1056 );
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001057 my $chr = ( (exists $es{$cntrl})
1058 ? $es{$cntrl}
Petr Baudis25dfd172008-10-01 22:11:54 +02001059 : sprintf('\%2x', ord($cntrl)) );
Jakub Narebskic84c4832008-02-17 18:48:13 +01001060 if ($opts{-nohtml}) {
1061 return $chr;
1062 } else {
1063 return "<span class=\"cntrl\">$chr</span>";
1064 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001065}
1066
Jakub Narebski391862e2006-11-25 09:43:59 +01001067# Alternatively use unicode control pictures codepoints,
1068# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001069sub quot_upr {
1070 my $cntrl = shift;
Jakub Narebskic84c4832008-02-17 18:48:13 +01001071 my %opts = @_;
1072
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001073 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
Jakub Narebskic84c4832008-02-17 18:48:13 +01001074 if ($opts{-nohtml}) {
1075 return $chr;
1076 } else {
1077 return "<span class=\"cntrl\">$chr</span>";
1078 }
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +01001079}
1080
Kay Sievers232ff552005-11-24 16:56:55 +01001081# git may return quoted and escaped filenames
1082sub unquote {
1083 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +01001084
1085 sub unq {
1086 my $seq = shift;
1087 my %es = ( # character escape codes, aka escape sequences
1088 't' => "\t", # tab (HT, TAB)
1089 'n' => "\n", # newline (NL)
1090 'r' => "\r", # return (CR)
1091 'f' => "\f", # form feed (FF)
1092 'b' => "\b", # backspace (BS)
1093 'a' => "\a", # alarm (bell) (BEL)
1094 'e' => "\e", # escape (ESC)
1095 'v' => "\013", # vertical tab (VT)
1096 );
1097
1098 if ($seq =~ m/^[0-7]{1,3}$/) {
1099 # octal char sequence
1100 return chr(oct($seq));
1101 } elsif (exists $es{$seq}) {
1102 # C escape sequence, aka character escape code
Jakub Narebskic84c4832008-02-17 18:48:13 +01001103 return $es{$seq};
Jakub Narebski403d0902006-11-08 11:48:56 +01001104 }
1105 # quoted ordinary character
1106 return $seq;
1107 }
1108
Kay Sievers232ff552005-11-24 16:56:55 +01001109 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +01001110 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +01001111 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +01001112 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +01001113 }
1114 return $str;
1115}
1116
Jakub Narebskif16db172006-08-06 02:08:31 +02001117# escape tabs (convert tabs to spaces)
1118sub untabify {
1119 my $line = shift;
1120
1121 while ((my $pos = index($line, "\t")) != -1) {
1122 if (my $count = (8 - ($pos % 8))) {
1123 my $spaces = ' ' x $count;
1124 $line =~ s/\t/$spaces/;
1125 }
1126 }
1127
1128 return $line;
1129}
1130
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +02001131sub project_in_list {
1132 my $project = shift;
1133 my @list = git_get_projects_list();
1134 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1135}
1136
Jakub Narebski717b8312006-07-31 21:22:15 +02001137## ----------------------------------------------------------------------
1138## HTML aware string manipulation
1139
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001140# Try to chop given string on a word boundary between position
1141# $len and $len+$add_len. If there is no word boundary there,
1142# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1143# (marking chopped part) would be longer than given string.
Jakub Narebski717b8312006-07-31 21:22:15 +02001144sub chop_str {
1145 my $str = shift;
1146 my $len = shift;
1147 my $add_len = shift || 10;
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001148 my $where = shift || 'right'; # 'left' | 'center' | 'right'
Jakub Narebski717b8312006-07-31 21:22:15 +02001149
Anders Waldenborgdee27752008-05-21 13:44:43 +02001150 # Make sure perl knows it is utf8 encoded so we don't
1151 # cut in the middle of a utf8 multibyte char.
1152 $str = to_utf8($str);
1153
Jakub Narebski717b8312006-07-31 21:22:15 +02001154 # allow only $len chars, but don't cut a word if it would fit in $add_len
1155 # 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 +01001156 # remove chopped character entities entirely
1157
1158 # when chopping in the middle, distribute $len into left and right part
1159 # return early if chopping wouldn't make string shorter
1160 if ($where eq 'center') {
1161 return $str if ($len + 5 >= length($str)); # filler is length 5
1162 $len = int($len/2);
1163 } else {
1164 return $str if ($len + 4 >= length($str)); # filler is length 4
Jakub Narebski717b8312006-07-31 21:22:15 +02001165 }
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001166
1167 # regexps: ending and beginning with word part up to $add_len
1168 my $endre = qr/.{$len}\w{0,$add_len}/;
1169 my $begre = qr/\w{0,$add_len}.{$len}/;
1170
1171 if ($where eq 'left') {
1172 $str =~ m/^(.*?)($begre)$/;
1173 my ($lead, $body) = ($1, $2);
1174 if (length($lead) > 4) {
1175 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1176 $lead = " ...";
1177 }
1178 return "$lead$body";
1179
1180 } elsif ($where eq 'center') {
1181 $str =~ m/^($endre)(.*)$/;
1182 my ($left, $str) = ($1, $2);
1183 $str =~ m/^(.*?)($begre)$/;
1184 my ($mid, $right) = ($1, $2);
1185 if (length($mid) > 5) {
1186 $left =~ s/&[^;]*$//;
1187 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1188 $mid = " ... ";
1189 }
1190 return "$left$mid$right";
1191
1192 } else {
1193 $str =~ m/^($endre)(.*)$/;
1194 my $body = $1;
1195 my $tail = $2;
1196 if (length($tail) > 4) {
1197 $body =~ s/&[^;]*$//;
1198 $tail = "... ";
1199 }
1200 return "$body$tail";
1201 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001202}
1203
David Symondsce58ec92007-10-23 11:31:22 +10001204# takes the same arguments as chop_str, but also wraps a <span> around the
1205# result with a title attribute if it does get chopped. Additionally, the
1206# string is HTML-escaped.
1207sub chop_and_escape_str {
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001208 my ($str) = @_;
David Symondsce58ec92007-10-23 11:31:22 +10001209
Jakub Narebskib8d97d02008-02-25 21:07:57 +01001210 my $chopped = chop_str(@_);
David Symondsce58ec92007-10-23 11:31:22 +10001211 if ($chopped eq $str) {
1212 return esc_html($chopped);
1213 } else {
Jakub Narebski850b90a2008-02-16 23:07:46 +01001214 $str =~ s/([[:cntrl:]])/?/g;
1215 return $cgi->span({-title=>$str}, esc_html($chopped));
David Symondsce58ec92007-10-23 11:31:22 +10001216 }
1217}
1218
Jakub Narebski717b8312006-07-31 21:22:15 +02001219## ----------------------------------------------------------------------
1220## functions returning short strings
1221
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001222# CSS class for given age value (in seconds)
1223sub age_class {
1224 my $age = shift;
1225
Jakub Narebski785cdea2007-05-13 12:39:22 +02001226 if (!defined $age) {
1227 return "noage";
1228 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00001229 return "age0";
1230 } elsif ($age < 60*60*24*2) {
1231 return "age1";
1232 } else {
1233 return "age2";
1234 }
1235}
1236
Jakub Narebski717b8312006-07-31 21:22:15 +02001237# convert age in seconds to "nn units ago" string
1238sub age_string {
1239 my $age = shift;
1240 my $age_str;
1241
1242 if ($age > 60*60*24*365*2) {
1243 $age_str = (int $age/60/60/24/365);
1244 $age_str .= " years ago";
1245 } elsif ($age > 60*60*24*(365/12)*2) {
1246 $age_str = int $age/60/60/24/(365/12);
1247 $age_str .= " months ago";
1248 } elsif ($age > 60*60*24*7*2) {
1249 $age_str = int $age/60/60/24/7;
1250 $age_str .= " weeks ago";
1251 } elsif ($age > 60*60*24*2) {
1252 $age_str = int $age/60/60/24;
1253 $age_str .= " days ago";
1254 } elsif ($age > 60*60*2) {
1255 $age_str = int $age/60/60;
1256 $age_str .= " hours ago";
1257 } elsif ($age > 60*2) {
1258 $age_str = int $age/60;
1259 $age_str .= " min ago";
1260 } elsif ($age > 2) {
1261 $age_str = int $age;
1262 $age_str .= " sec ago";
1263 } else {
1264 $age_str .= " right now";
1265 }
1266 return $age_str;
1267}
1268
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001269use constant {
1270 S_IFINVALID => 0030000,
1271 S_IFGITLINK => 0160000,
1272};
1273
1274# submodule/subproject, a commit object reference
1275sub S_ISGITLINK($) {
1276 my $mode = shift;
1277
1278 return (($mode & S_IFMT) == S_IFGITLINK)
1279}
1280
Jakub Narebski717b8312006-07-31 21:22:15 +02001281# convert file mode in octal to symbolic file mode string
1282sub mode_str {
1283 my $mode = oct shift;
1284
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001285 if (S_ISGITLINK($mode)) {
1286 return 'm---------';
1287 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001288 return 'drwxr-xr-x';
1289 } elsif (S_ISLNK($mode)) {
1290 return 'lrwxrwxrwx';
1291 } elsif (S_ISREG($mode)) {
1292 # git cares only about the executable bit
1293 if ($mode & S_IXUSR) {
1294 return '-rwxr-xr-x';
1295 } else {
1296 return '-rw-r--r--';
1297 };
1298 } else {
1299 return '----------';
1300 }
1301}
1302
1303# convert file mode in octal to file type string
1304sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001305 my $mode = shift;
1306
1307 if ($mode !~ m/^[0-7]+$/) {
1308 return $mode;
1309 } else {
1310 $mode = oct $mode;
1311 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001312
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001313 if (S_ISGITLINK($mode)) {
1314 return "submodule";
1315 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001316 return "directory";
1317 } elsif (S_ISLNK($mode)) {
1318 return "symlink";
1319 } elsif (S_ISREG($mode)) {
1320 return "file";
1321 } else {
1322 return "unknown";
1323 }
1324}
1325
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001326# convert file mode in octal to file type description string
1327sub file_type_long {
1328 my $mode = shift;
1329
1330 if ($mode !~ m/^[0-7]+$/) {
1331 return $mode;
1332 } else {
1333 $mode = oct $mode;
1334 }
1335
Jakub Narebski01ac1e32007-07-28 16:27:31 +02001336 if (S_ISGITLINK($mode)) {
1337 return "submodule";
1338 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +01001339 return "directory";
1340 } elsif (S_ISLNK($mode)) {
1341 return "symlink";
1342 } elsif (S_ISREG($mode)) {
1343 if ($mode & S_IXUSR) {
1344 return "executable";
1345 } else {
1346 return "file";
1347 };
1348 } else {
1349 return "unknown";
1350 }
1351}
1352
1353
Jakub Narebski717b8312006-07-31 21:22:15 +02001354## ----------------------------------------------------------------------
1355## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05001356## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02001357
Junio C Hamano225932e2006-11-09 00:57:13 -08001358# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02001359sub format_log_line_html {
1360 my $line = shift;
1361
Junio C Hamano225932e2006-11-09 00:57:13 -08001362 $line = esc_html($line, -nbsp=>1);
Jakub Narebskibfe21912006-12-10 13:25:49 +01001363 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001364 my $hash_text = $1;
Jakub Narebskibfe21912006-12-10 13:25:49 +01001365 my $link =
1366 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1367 -class => "text"}, $hash_text);
1368 $line =~ s/$hash_text/$link/;
Jakub Narebski717b8312006-07-31 21:22:15 +02001369 }
1370 return $line;
1371}
1372
1373# format marker of refs pointing to given object
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001374
1375# the destination action is chosen based on object type and current context:
1376# - for annotated tags, we choose the tag view unless it's the current view
1377# already, in which case we go to shortlog view
1378# - for other refs, we keep the current view if we're in history, shortlog or
1379# log view, and select shortlog otherwise
Jakub Narebski847e01f2006-08-14 02:05:47 +02001380sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02001381 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001382 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02001383
1384 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001385 foreach my $ref (@{$refs->{$id}}) {
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001386 # this code exploits the fact that non-lightweight tags are the
1387 # only indirect objects, and that they are the only objects for which
1388 # we want to use tag instead of shortlog as action
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001389 my ($type, $name) = qw();
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001390 my $indirect = ($ref =~ s/\^\{\}$//);
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001391 # e.g. tags/v2.6.11 or heads/next
1392 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1393 $type = $1;
1394 $name = $2;
1395 } else {
1396 $type = "ref";
1397 $name = $ref;
1398 }
1399
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02001400 my $class = $type;
1401 $class .= " indirect" if $indirect;
1402
1403 my $dest_action = "shortlog";
1404
1405 if ($indirect) {
1406 $dest_action = "tag" unless $action eq "tag";
1407 } elsif ($action =~ /^(history|(short)?log)$/) {
1408 $dest_action = $action;
1409 }
1410
1411 my $dest = "";
1412 $dest .= "refs/" unless $ref =~ m!^refs/!;
1413 $dest .= $ref;
1414
1415 my $link = $cgi->a({
1416 -href => href(
1417 action=>$dest_action,
1418 hash=>$dest
1419 )}, $name);
1420
1421 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1422 $link . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001423 }
1424 }
1425
1426 if ($markers) {
1427 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02001428 } else {
1429 return "";
1430 }
1431}
1432
Jakub Narebski17d07442006-08-14 02:08:27 +02001433# format, perhaps shortened and with markers, title line
1434sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001435 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02001436 $extra = '' unless defined($extra);
1437
1438 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +02001439 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02001440 -title => to_utf8($long)},
Jakub Narebski17d07442006-08-14 02:08:27 +02001441 esc_html($short) . $extra);
1442 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02001443 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +02001444 esc_html($long) . $extra);
1445 }
1446}
1447
Jakub Narebski90921742007-06-08 13:27:42 +02001448# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1449sub format_git_diff_header_line {
1450 my $line = shift;
1451 my $diffinfo = shift;
1452 my ($from, $to) = @_;
1453
1454 if ($diffinfo->{'nparents'}) {
1455 # combined diff
1456 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1457 if ($to->{'href'}) {
1458 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1459 esc_path($to->{'file'}));
1460 } else { # file was deleted (no href)
1461 $line .= esc_path($to->{'file'});
1462 }
1463 } else {
1464 # "ordinary" diff
1465 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1466 if ($from->{'href'}) {
1467 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1468 'a/' . esc_path($from->{'file'}));
1469 } else { # file was added (no href)
1470 $line .= 'a/' . esc_path($from->{'file'});
1471 }
1472 $line .= ' ';
1473 if ($to->{'href'}) {
1474 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1475 'b/' . esc_path($to->{'file'}));
1476 } else { # file was deleted
1477 $line .= 'b/' . esc_path($to->{'file'});
1478 }
1479 }
1480
1481 return "<div class=\"diff header\">$line</div>\n";
1482}
1483
1484# format extended diff header line, before patch itself
1485sub format_extended_diff_header_line {
1486 my $line = shift;
1487 my $diffinfo = shift;
1488 my ($from, $to) = @_;
1489
1490 # match <path>
1491 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1492 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1493 esc_path($from->{'file'}));
1494 }
1495 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1496 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1497 esc_path($to->{'file'}));
1498 }
1499 # match single <mode>
1500 if ($line =~ m/\s(\d{6})$/) {
1501 $line .= '<span class="info"> (' .
1502 file_type_long($1) .
1503 ')</span>';
1504 }
1505 # match <hash>
1506 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1507 # can match only for combined diff
1508 $line = 'index ';
1509 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1510 if ($from->{'href'}[$i]) {
1511 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1512 -class=>"hash"},
1513 substr($diffinfo->{'from_id'}[$i],0,7));
1514 } else {
1515 $line .= '0' x 7;
1516 }
1517 # separator
1518 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1519 }
1520 $line .= '..';
1521 if ($to->{'href'}) {
1522 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1523 substr($diffinfo->{'to_id'},0,7));
1524 } else {
1525 $line .= '0' x 7;
1526 }
1527
1528 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1529 # can match only for ordinary diff
1530 my ($from_link, $to_link);
1531 if ($from->{'href'}) {
1532 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1533 substr($diffinfo->{'from_id'},0,7));
1534 } else {
1535 $from_link = '0' x 7;
1536 }
1537 if ($to->{'href'}) {
1538 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1539 substr($diffinfo->{'to_id'},0,7));
1540 } else {
1541 $to_link = '0' x 7;
1542 }
1543 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1544 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1545 }
1546
1547 return $line . "<br/>\n";
1548}
1549
1550# format from-file/to-file diff header
1551sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001552 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02001553 my $line;
1554 my $result = '';
1555
1556 $line = $from_line;
1557 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001558 # no extra formatting for "^--- /dev/null"
1559 if (! $diffinfo->{'nparents'}) {
1560 # ordinary (single parent) diff
1561 if ($line =~ m!^--- "?a/!) {
1562 if ($from->{'href'}) {
1563 $line = '--- a/' .
1564 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1565 esc_path($from->{'file'}));
1566 } else {
1567 $line = '--- a/' .
1568 esc_path($from->{'file'});
1569 }
1570 }
1571 $result .= qq!<div class="diff from_file">$line</div>\n!;
1572
1573 } else {
1574 # combined diff (merge commit)
1575 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1576 if ($from->{'href'}[$i]) {
1577 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001578 $cgi->a({-href=>href(action=>"blobdiff",
1579 hash_parent=>$diffinfo->{'from_id'}[$i],
1580 hash_parent_base=>$parents[$i],
1581 file_parent=>$from->{'file'}[$i],
1582 hash=>$diffinfo->{'to_id'},
1583 hash_base=>$hash,
1584 file_name=>$to->{'file'}),
1585 -class=>"path",
1586 -title=>"diff" . ($i+1)},
1587 $i+1) .
1588 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001589 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1590 esc_path($from->{'file'}[$i]));
1591 } else {
1592 $line = '--- /dev/null';
1593 }
1594 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02001595 }
1596 }
Jakub Narebski90921742007-06-08 13:27:42 +02001597
1598 $line = $to_line;
1599 #assert($line =~ m/^\+\+\+/) if DEBUG;
1600 # no extra formatting for "^+++ /dev/null"
1601 if ($line =~ m!^\+\+\+ "?b/!) {
1602 if ($to->{'href'}) {
1603 $line = '+++ b/' .
1604 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1605 esc_path($to->{'file'}));
1606 } else {
1607 $line = '+++ b/' .
1608 esc_path($to->{'file'});
1609 }
1610 }
1611 $result .= qq!<div class="diff to_file">$line</div>\n!;
1612
1613 return $result;
1614}
1615
Jakub Narebskicd030c32007-06-08 13:33:28 +02001616# create note for patch simplified by combined diff
1617sub format_diff_cc_simplified {
1618 my ($diffinfo, @parents) = @_;
1619 my $result = '';
1620
1621 $result .= "<div class=\"diff header\">" .
1622 "diff --cc ";
1623 if (!is_deleted($diffinfo)) {
1624 $result .= $cgi->a({-href => href(action=>"blob",
1625 hash_base=>$hash,
1626 hash=>$diffinfo->{'to_id'},
1627 file_name=>$diffinfo->{'to_file'}),
1628 -class => "path"},
1629 esc_path($diffinfo->{'to_file'}));
1630 } else {
1631 $result .= esc_path($diffinfo->{'to_file'});
1632 }
1633 $result .= "</div>\n" . # class="diff header"
1634 "<div class=\"diff nodifferences\">" .
1635 "Simple merge" .
1636 "</div>\n"; # class="diff nodifferences"
1637
1638 return $result;
1639}
1640
Jakub Narebski90921742007-06-08 13:27:42 +02001641# format patch (diff) line (not to be used for diff headers)
Jakub Narebskieee08902006-08-24 00:15:14 +02001642sub format_diff_line {
1643 my $line = shift;
Jakub Narebski59e3b142006-11-18 23:35:40 +01001644 my ($from, $to) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001645 my $diff_class = "";
1646
1647 chomp $line;
1648
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001649 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1650 # combined diff
1651 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1652 if ($line =~ m/^\@{3}/) {
1653 $diff_class = " chunk_header";
1654 } elsif ($line =~ m/^\\/) {
1655 $diff_class = " incomplete";
1656 } elsif ($prefix =~ tr/+/+/) {
1657 $diff_class = " add";
1658 } elsif ($prefix =~ tr/-/-/) {
1659 $diff_class = " rem";
1660 }
1661 } else {
1662 # assume ordinary diff
1663 my $char = substr($line, 0, 1);
1664 if ($char eq '+') {
1665 $diff_class = " add";
1666 } elsif ($char eq '-') {
1667 $diff_class = " rem";
1668 } elsif ($char eq '@') {
1669 $diff_class = " chunk_header";
1670 } elsif ($char eq "\\") {
1671 $diff_class = " incomplete";
1672 }
Jakub Narebskieee08902006-08-24 00:15:14 +02001673 }
1674 $line = untabify($line);
Jakub Narebski59e3b142006-11-18 23:35:40 +01001675 if ($from && $to && $line =~ m/^\@{2} /) {
1676 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1677 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1678
1679 $from_lines = 0 unless defined $from_lines;
1680 $to_lines = 0 unless defined $to_lines;
1681
1682 if ($from->{'href'}) {
1683 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1684 -class=>"list"}, $from_text);
1685 }
1686 if ($to->{'href'}) {
1687 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1688 -class=>"list"}, $to_text);
1689 }
1690 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1691 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1692 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001693 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1694 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1695 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1696
1697 @from_text = split(' ', $ranges);
1698 for (my $i = 0; $i < @from_text; ++$i) {
1699 ($from_start[$i], $from_nlines[$i]) =
1700 (split(',', substr($from_text[$i], 1)), 0);
1701 }
1702
1703 $to_text = pop @from_text;
1704 $to_start = pop @from_start;
1705 $to_nlines = pop @from_nlines;
1706
1707 $line = "<span class=\"chunk_info\">$prefix ";
1708 for (my $i = 0; $i < @from_text; ++$i) {
1709 if ($from->{'href'}[$i]) {
1710 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1711 -class=>"list"}, $from_text[$i]);
1712 } else {
1713 $line .= $from_text[$i];
1714 }
1715 $line .= " ";
1716 }
1717 if ($to->{'href'}) {
1718 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1719 -class=>"list"}, $to_text);
1720 } else {
1721 $line .= $to_text;
1722 }
1723 $line .= " $prefix</span>" .
1724 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1725 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebski59e3b142006-11-18 23:35:40 +01001726 }
Jakub Narebski6255ef02006-11-01 14:33:21 +01001727 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001728}
1729
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001730# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1731# linked. Pass the hash of the tree/commit to snapshot.
1732sub format_snapshot_links {
1733 my ($hash) = @_;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001734 my $num_fmts = @snapshot_fmts;
1735 if ($num_fmts > 1) {
1736 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02001737 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001738 return "snapshot (" . join(' ', map
1739 $cgi->a({
1740 -href => href(
1741 action=>"snapshot",
1742 hash=>$hash,
1743 snapshot_format=>$_
1744 )
1745 }, $known_snapshot_formats{$_}{'display'})
1746 , @snapshot_fmts) . ")";
1747 } elsif ($num_fmts == 1) {
1748 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02001749 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001750 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02001751 return
1752 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001753 -href => href(
1754 action=>"snapshot",
1755 hash=>$hash,
1756 snapshot_format=>$fmt
1757 ),
1758 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1759 }, "snapshot");
1760 } else { # $num_fmts == 0
1761 return undef;
1762 }
1763}
1764
Jakub Narebski35621982008-04-20 22:09:48 +02001765## ......................................................................
1766## functions returning values to be passed, perhaps after some
1767## transformation, to other functions; e.g. returning arguments to href()
1768
1769# returns hash to be passed to href to generate gitweb URL
1770# in -title key it returns description of link
1771sub get_feed_info {
1772 my $format = shift || 'Atom';
1773 my %res = (action => lc($format));
1774
1775 # feed links are possible only for project views
1776 return unless (defined $project);
1777 # some views should link to OPML, or to generic project feed,
1778 # or don't have specific feed yet (so they should use generic)
1779 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1780
1781 my $branch;
1782 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1783 # from tag links; this also makes possible to detect branch links
1784 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1785 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1786 $branch = $1;
1787 }
1788 # find log type for feed description (title)
1789 my $type = 'log';
1790 if (defined $file_name) {
1791 $type = "history of $file_name";
1792 $type .= "/" if ($action eq 'tree');
1793 $type .= " on '$branch'" if (defined $branch);
1794 } else {
1795 $type = "log of $branch" if (defined $branch);
1796 }
1797
1798 $res{-title} = $type;
1799 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1800 $res{'file_name'} = $file_name;
1801
1802 return %res;
1803}
1804
Jakub Narebski717b8312006-07-31 21:22:15 +02001805## ----------------------------------------------------------------------
1806## git utility subroutines, invoking git commands
1807
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001808# returns path to the core git executable and the --git-dir parameter as list
1809sub git_cmd {
1810 return $GIT, '--git-dir='.$git_dir;
1811}
1812
Lea Wiemann516381d2008-06-17 23:46:35 +02001813# quote the given arguments for passing them to the shell
1814# quote_command("command", "arg 1", "arg with ' and ! characters")
1815# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1816# Try to avoid using this function wherever possible.
1817sub quote_command {
1818 return join(' ',
1819 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001820}
1821
Jakub Narebski717b8312006-07-31 21:22:15 +02001822# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02001823sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +02001824 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001825 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001826 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001827 $git_dir = "$projectroot/$project";
1828 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +02001829 my $head = <$fd>;
1830 close $fd;
1831 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1832 $retval = $1;
1833 }
1834 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001835 if (defined $o_git_dir) {
1836 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001837 }
1838 return $retval;
1839}
1840
1841# get type of given object
1842sub git_get_type {
1843 my $hash = shift;
1844
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001845 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001846 my $type = <$fd>;
1847 close $fd or return;
1848 chomp $type;
1849 return $type;
1850}
1851
Jakub Narebskib2019272007-11-03 00:41:19 +01001852# repository configuration
1853our $config_file = '';
1854our %config;
1855
1856# store multiple values for single key as anonymous array reference
1857# single values stored directly in the hash, not as [ <value> ]
1858sub hash_set_multi {
1859 my ($hash, $key, $value) = @_;
1860
1861 if (!exists $hash->{$key}) {
1862 $hash->{$key} = $value;
1863 } elsif (!ref $hash->{$key}) {
1864 $hash->{$key} = [ $hash->{$key}, $value ];
1865 } else {
1866 push @{$hash->{$key}}, $value;
1867 }
1868}
1869
1870# return hash of git project configuration
1871# optionally limited to some section, e.g. 'gitweb'
1872sub git_parse_project_config {
1873 my $section_regexp = shift;
1874 my %config;
1875
1876 local $/ = "\0";
1877
1878 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1879 or return;
1880
1881 while (my $keyval = <$fh>) {
1882 chomp $keyval;
1883 my ($key, $value) = split(/\n/, $keyval, 2);
1884
1885 hash_set_multi(\%config, $key, $value)
1886 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1887 }
1888 close $fh;
1889
1890 return %config;
1891}
1892
1893# convert config value to boolean, 'true' or 'false'
1894# no value, number > 0, 'true' and 'yes' values are true
1895# rest of values are treated as false (never as error)
1896sub config_to_bool {
1897 my $val = shift;
1898
1899 # strip leading and trailing whitespace
1900 $val =~ s/^\s+//;
1901 $val =~ s/\s+$//;
1902
1903 return (!defined $val || # section.key
1904 ($val =~ /^\d+$/ && $val) || # section.key = 1
1905 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1906}
1907
1908# convert config value to simple decimal number
1909# an optional value suffix of 'k', 'm', or 'g' will cause the value
1910# to be multiplied by 1024, 1048576, or 1073741824
1911sub config_to_int {
1912 my $val = shift;
1913
1914 # strip leading and trailing whitespace
1915 $val =~ s/^\s+//;
1916 $val =~ s/\s+$//;
1917
1918 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1919 $unit = lc($unit);
1920 # unknown unit is treated as 1
1921 return $num * ($unit eq 'g' ? 1073741824 :
1922 $unit eq 'm' ? 1048576 :
1923 $unit eq 'k' ? 1024 : 1);
1924 }
1925 return $val;
1926}
1927
1928# convert config value to array reference, if needed
1929sub config_to_multi {
1930 my $val = shift;
1931
Jakub Narebskid76a5852007-12-20 10:48:09 +01001932 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
Jakub Narebskib2019272007-11-03 00:41:19 +01001933}
1934
Jakub Narebski717b8312006-07-31 21:22:15 +02001935sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301936 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001937
Jakub Narebskib2019272007-11-03 00:41:19 +01001938 # key sanity check
Jakub Narebski717b8312006-07-31 21:22:15 +02001939 return unless ($key);
1940 $key =~ s/^gitweb\.//;
1941 return if ($key =~ m/\W/);
1942
Jakub Narebskib2019272007-11-03 00:41:19 +01001943 # type sanity check
1944 if (defined $type) {
1945 $type =~ s/^--//;
1946 $type = undef
1947 unless ($type eq 'bool' || $type eq 'int');
1948 }
1949
1950 # get config
1951 if (!defined $config_file ||
1952 $config_file ne "$git_dir/config") {
1953 %config = git_parse_project_config('gitweb');
1954 $config_file = "$git_dir/config";
1955 }
1956
1957 # ensure given type
1958 if (!defined $type) {
1959 return $config{"gitweb.$key"};
1960 } elsif ($type eq 'bool') {
1961 # backward compatibility: 'git config --bool' returns true/false
1962 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1963 } elsif ($type eq 'int') {
1964 return config_to_int($config{"gitweb.$key"});
1965 }
1966 return $config{"gitweb.$key"};
Jakub Narebski717b8312006-07-31 21:22:15 +02001967}
1968
Jakub Narebski717b8312006-07-31 21:22:15 +02001969# get hash of given path at given ref
1970sub git_get_hash_by_path {
1971 my $base = shift;
1972 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001973 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02001974
Jakub Narebski4b02f482006-09-26 01:54:24 +02001975 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02001976
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001977 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Lea Wiemann074afaa2008-06-19 22:03:21 +02001978 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02001979 my $line = <$fd>;
1980 close $fd or return undef;
1981
Jakub Narebski198a2a82007-05-12 21:16:34 +02001982 if (!defined $line) {
1983 # there is no tree or hash given by $path at $base
1984 return undef;
1985 }
1986
Jakub Narebski717b8312006-07-31 21:22:15 +02001987 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01001988 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001989 if (defined $type && $type ne $2) {
1990 # type doesn't match
1991 return undef;
1992 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001993 return $3;
1994}
1995
Jakub Narebskied224de2007-05-07 01:10:04 +02001996# get path of entry with given hash at given tree-ish (ref)
1997# used to get 'from' filename for combined diff (merge commit) for renames
1998sub git_get_path_by_hash {
1999 my $base = shift || return;
2000 my $hash = shift || return;
2001
2002 local $/ = "\0";
2003
2004 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2005 or return undef;
2006 while (my $line = <$fd>) {
2007 chomp $line;
2008
2009 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2010 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2011 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2012 close $fd;
2013 return $1;
2014 }
2015 }
2016 close $fd;
2017 return undef;
2018}
2019
Jakub Narebski717b8312006-07-31 21:22:15 +02002020## ......................................................................
2021## git utility functions, directly accessing git repository
2022
Jakub Narebski847e01f2006-08-14 02:05:47 +02002023sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +02002024 my $path = shift;
2025
Jakub Narebski0e121a22007-11-03 00:41:20 +01002026 $git_dir = "$projectroot/$path";
Bruno Ribasc1dcf7e2008-01-30 03:37:56 -02002027 open my $fd, "$git_dir/description"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002028 or return git_get_project_config('description');
Jakub Narebski717b8312006-07-31 21:22:15 +02002029 my $descr = <$fd>;
2030 close $fd;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07002031 if (defined $descr) {
2032 chomp $descr;
2033 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002034 return $descr;
2035}
2036
Petr Baudisaed93de2008-10-02 17:13:02 +02002037sub git_get_project_ctags {
2038 my $path = shift;
2039 my $ctags = {};
2040
2041 $git_dir = "$projectroot/$path";
Junio C Hamanoeee01842008-10-14 21:27:12 -07002042 unless (opendir D, "$git_dir/ctags") {
2043 return $ctags;
2044 }
2045 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir(D)) {
Petr Baudisaed93de2008-10-02 17:13:02 +02002046 open CT, $_ or next;
2047 my $val = <CT>;
2048 chomp $val;
2049 close CT;
2050 my $ctag = $_; $ctag =~ s#.*/##;
2051 $ctags->{$ctag} = $val;
2052 }
Junio C Hamanoeee01842008-10-14 21:27:12 -07002053 closedir D;
Petr Baudisaed93de2008-10-02 17:13:02 +02002054 $ctags;
2055}
2056
2057sub git_populate_project_tagcloud {
2058 my $ctags = shift;
2059
2060 # First, merge different-cased tags; tags vote on casing
2061 my %ctags_lc;
2062 foreach (keys %$ctags) {
2063 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2064 if (not $ctags_lc{lc $_}->{topcount}
2065 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2066 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2067 $ctags_lc{lc $_}->{topname} = $_;
2068 }
2069 }
2070
2071 my $cloud;
2072 if (eval { require HTML::TagCloud; 1; }) {
2073 $cloud = HTML::TagCloud->new;
2074 foreach (sort keys %ctags_lc) {
2075 # Pad the title with spaces so that the cloud looks
2076 # less crammed.
2077 my $title = $ctags_lc{$_}->{topname};
2078 $title =~ s/ /&nbsp;/g;
2079 $title =~ s/^/&nbsp;/g;
2080 $title =~ s/$/&nbsp;/g;
2081 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2082 }
2083 } else {
2084 $cloud = \%ctags_lc;
2085 }
2086 $cloud;
2087}
2088
2089sub git_show_project_tagcloud {
2090 my ($cloud, $count) = @_;
2091 print STDERR ref($cloud)."..\n";
2092 if (ref $cloud eq 'HTML::TagCloud') {
2093 return $cloud->html_and_css($count);
2094 } else {
2095 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2096 return '<p align="center">' . join (', ', map {
2097 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2098 } splice(@tags, 0, $count)) . '</p>';
2099 }
2100}
2101
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002102sub git_get_project_url_list {
2103 my $path = shift;
2104
Jakub Narebski0e121a22007-11-03 00:41:20 +01002105 $git_dir = "$projectroot/$path";
Bruno Ribas201945e2008-02-06 15:15:12 -02002106 open my $fd, "$git_dir/cloneurl"
Jakub Narebski0e121a22007-11-03 00:41:20 +01002107 or return wantarray ?
2108 @{ config_to_multi(git_get_project_config('url')) } :
2109 config_to_multi(git_get_project_config('url'));
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002110 my @git_project_url_list = map { chomp; $_ } <$fd>;
2111 close $fd;
2112
2113 return wantarray ? @git_project_url_list : \@git_project_url_list;
2114}
2115
Jakub Narebski847e01f2006-08-14 02:05:47 +02002116sub git_get_projects_list {
Petr Baudise30496d2006-10-24 05:33:17 +02002117 my ($filter) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02002118 my @list;
2119
Petr Baudise30496d2006-10-24 05:33:17 +02002120 $filter ||= '';
2121 $filter =~ s/\.git$//;
2122
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08002123 my $check_forks = gitweb_check_feature('forks');
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002124
Jakub Narebski717b8312006-07-31 21:22:15 +02002125 if (-d $projects_list) {
2126 # search in directory
Petr Baudise30496d2006-10-24 05:33:17 +02002127 my $dir = $projects_list . ($filter ? "/$filter" : '');
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05302128 # remove the trailing "/"
2129 $dir =~ s!/+$!!;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002130 my $pfxlen = length("$dir");
Luke Luca5e9492007-10-16 20:45:25 -07002131 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002132
2133 File::Find::find({
2134 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07002135 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002136 dangling_symlinks => 0, # ignore dangling symlinks, silently
2137 wanted => sub {
2138 # skip project-list toplevel, if we get it.
2139 return if (m!^[/.]$!);
2140 # only directories can be git repositories
2141 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07002142 # don't traverse too deep (Find is super slow on os x)
2143 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2144 $File::Find::prune = 1;
2145 return;
2146 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002147
2148 my $subdir = substr($File::Find::name, $pfxlen + 1);
2149 # we check related file in $projectroot
Petr Baudis42326112008-10-02 17:17:01 +02002150 if (check_export_ok("$projectroot/$filter/$subdir")) {
Petr Baudise30496d2006-10-24 05:33:17 +02002151 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02002152 $File::Find::prune = 1;
2153 }
2154 },
2155 }, "$dir");
2156
Jakub Narebski717b8312006-07-31 21:22:15 +02002157 } elsif (-f $projects_list) {
2158 # read from file(url-encoded):
2159 # 'git%2Fgit.git Linus+Torvalds'
2160 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2161 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002162 my %paths;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +02002163 open my ($fd), $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002164 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02002165 while (my $line = <$fd>) {
2166 chomp $line;
2167 my ($path, $owner) = split ' ', $line;
2168 $path = unescape($path);
2169 $owner = unescape($owner);
2170 if (!defined $path) {
2171 next;
2172 }
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002173 if ($filter ne '') {
2174 # looking for forks;
2175 my $pfx = substr($path, 0, length($filter));
2176 if ($pfx ne $filter) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002177 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002178 }
2179 my $sfx = substr($path, length($filter));
2180 if ($sfx !~ /^\/.*\.git$/) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002181 next PROJECT;
2182 }
2183 } elsif ($check_forks) {
2184 PATH:
2185 foreach my $filter (keys %paths) {
2186 # looking for forks;
2187 my $pfx = substr($path, 0, length($filter));
2188 if ($pfx ne $filter) {
2189 next PATH;
2190 }
2191 my $sfx = substr($path, length($filter));
2192 if ($sfx !~ /^\/.*\.git$/) {
2193 next PATH;
2194 }
2195 # is a fork, don't include it in
2196 # the list
2197 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08002198 }
2199 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07002200 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002201 my $pr = {
2202 path => $path,
Martin Koegler00f429a2007-06-03 17:42:44 +02002203 owner => to_utf8($owner),
Jakub Narebski717b8312006-07-31 21:22:15 +02002204 };
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02002205 push @list, $pr;
2206 (my $forks_path = $path) =~ s/\.git$//;
2207 $paths{$forks_path}++;
Jakub Narebski717b8312006-07-31 21:22:15 +02002208 }
2209 }
2210 close $fd;
2211 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002212 return @list;
2213}
2214
Junio C Hamano47852452007-07-03 22:10:42 -07002215our $gitweb_project_owner = undef;
2216sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002217
Junio C Hamano47852452007-07-03 22:10:42 -07002218 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002219
Junio C Hamano47852452007-07-03 22:10:42 -07002220 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002221 # read from file (url-encoded):
2222 # 'git%2Fgit.git Linus+Torvalds'
2223 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2224 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2225 if (-f $projects_list) {
2226 open (my $fd , $projects_list);
2227 while (my $line = <$fd>) {
2228 chomp $line;
2229 my ($pr, $ow) = split ' ', $line;
2230 $pr = unescape($pr);
2231 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07002232 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002233 }
2234 close $fd;
2235 }
Junio C Hamano47852452007-07-03 22:10:42 -07002236}
2237
2238sub git_get_project_owner {
2239 my $project = shift;
2240 my $owner;
2241
2242 return undef unless $project;
Bruno Ribasb59012e2008-02-08 14:38:04 -02002243 $git_dir = "$projectroot/$project";
Junio C Hamano47852452007-07-03 22:10:42 -07002244
2245 if (!defined $gitweb_project_owner) {
2246 git_get_project_list_from_file();
2247 }
2248
2249 if (exists $gitweb_project_owner->{$project}) {
2250 $owner = $gitweb_project_owner->{$project};
2251 }
Bruno Ribasb59012e2008-02-08 14:38:04 -02002252 if (!defined $owner){
2253 $owner = git_get_project_config('owner');
2254 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002255 if (!defined $owner) {
Bruno Ribasb59012e2008-02-08 14:38:04 -02002256 $owner = get_file_owner("$git_dir");
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002257 }
2258
2259 return $owner;
2260}
2261
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002262sub git_get_last_activity {
2263 my ($path) = @_;
2264 my $fd;
2265
2266 $git_dir = "$projectroot/$path";
2267 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00002268 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002269 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00002270 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002271 'refs/heads') or return;
2272 my $most_recent = <$fd>;
2273 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02002274 if (defined $most_recent &&
2275 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002276 my $timestamp = $1;
2277 my $age = time - $timestamp;
2278 return ($age, age_string($age));
2279 }
Matt McCutchenc9563952007-06-28 18:15:22 -04002280 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02002281}
2282
Jakub Narebski847e01f2006-08-14 02:05:47 +02002283sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02002284 my $type = shift || "";
2285 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01002286 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2287 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2288 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2289 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02002290 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002291
Jakub Narebski717b8312006-07-31 21:22:15 +02002292 while (my $line = <$fd>) {
2293 chomp $line;
Giuseppe Bilotta4afbaef2008-09-02 21:47:05 +02002294 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002295 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002296 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02002297 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02002298 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02002299 }
2300 }
2301 }
2302 close $fd or return;
2303 return \%refs;
2304}
2305
Jakub Narebski56a322f2006-08-24 19:41:23 +02002306sub git_get_rev_name_tags {
2307 my $hash = shift || return undef;
2308
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002309 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02002310 or return;
2311 my $name_rev = <$fd>;
2312 close $fd;
2313
2314 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2315 return $1;
2316 } else {
2317 # catches also '$hash undefined' output
2318 return undef;
2319 }
2320}
2321
Jakub Narebski717b8312006-07-31 21:22:15 +02002322## ----------------------------------------------------------------------
2323## parse to hash functions
2324
Jakub Narebski847e01f2006-08-14 02:05:47 +02002325sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02002326 my $epoch = shift;
2327 my $tz = shift || "-0000";
2328
2329 my %date;
2330 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2331 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2332 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2333 $date{'hour'} = $hour;
2334 $date{'minute'} = $min;
2335 $date{'mday'} = $mday;
2336 $date{'day'} = $days[$wday];
2337 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002338 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2339 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02002340 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2341 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002342 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
Vincent Zanottia62d6d82007-11-10 19:55:27 +01002343 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02002344
2345 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2346 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2347 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2348 $date{'hour_local'} = $hour;
2349 $date{'minute_local'} = $min;
2350 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002351 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2352 1900+$year, $mon+1, $mday,
2353 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02002354 return %date;
2355}
2356
Jakub Narebski847e01f2006-08-14 02:05:47 +02002357sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02002358 my $tag_id = shift;
2359 my %tag;
2360 my @comment;
2361
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002362 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02002363 $tag{'id'} = $tag_id;
2364 while (my $line = <$fd>) {
2365 chomp $line;
2366 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2367 $tag{'object'} = $1;
2368 } elsif ($line =~ m/^type (.+)$/) {
2369 $tag{'type'} = $1;
2370 } elsif ($line =~ m/^tag (.+)$/) {
2371 $tag{'name'} = $1;
2372 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2373 $tag{'author'} = $1;
2374 $tag{'epoch'} = $2;
2375 $tag{'tz'} = $3;
2376 } elsif ($line =~ m/--BEGIN/) {
2377 push @comment, $line;
2378 last;
2379 } elsif ($line eq "") {
2380 last;
2381 }
2382 }
2383 push @comment, <$fd>;
2384 $tag{'comment'} = \@comment;
2385 close $fd or return;
2386 if (!defined $tag{'name'}) {
2387 return
2388 };
2389 return %tag
2390}
2391
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002392sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002393 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002394 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02002395 my %co;
2396
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002397 pop @commit_lines; # Remove '\0'
2398
Jakub Narebski198a2a82007-05-12 21:16:34 +02002399 if (! @commit_lines) {
2400 return;
2401 }
2402
Jakub Narebski717b8312006-07-31 21:22:15 +02002403 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02002404 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02002405 return;
2406 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002407 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02002408 while (my $line = shift @commit_lines) {
2409 last if $line eq "\n";
2410 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2411 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002412 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00002413 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02002414 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2415 $co{'author'} = $1;
2416 $co{'author_epoch'} = $2;
2417 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01002418 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2419 $co{'author_name'} = $1;
2420 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02002421 } else {
2422 $co{'author_name'} = $co{'author'};
2423 }
2424 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2425 $co{'committer'} = $1;
2426 $co{'committer_epoch'} = $2;
2427 $co{'committer_tz'} = $3;
2428 $co{'committer_name'} = $co{'committer'};
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01002429 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2430 $co{'committer_name'} = $1;
2431 $co{'committer_email'} = $2;
2432 } else {
2433 $co{'committer_name'} = $co{'committer'};
2434 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002435 }
2436 }
2437 if (!defined $co{'tree'}) {
2438 return;
2439 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00002440 $co{'parents'} = \@parents;
2441 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02002442
2443 foreach my $title (@commit_lines) {
2444 $title =~ s/^ //;
2445 if ($title ne "") {
2446 $co{'title'} = chop_str($title, 80, 5);
2447 # remove leading stuff of merges to make the interesting part visible
2448 if (length($title) > 50) {
2449 $title =~ s/^Automatic //;
2450 $title =~ s/^merge (of|with) /Merge ... /i;
2451 if (length($title) > 50) {
2452 $title =~ s/(http|rsync):\/\///;
2453 }
2454 if (length($title) > 50) {
2455 $title =~ s/(master|www|rsync)\.//;
2456 }
2457 if (length($title) > 50) {
2458 $title =~ s/kernel.org:?//;
2459 }
2460 if (length($title) > 50) {
2461 $title =~ s/\/pub\/scm//;
2462 }
2463 }
2464 $co{'title_short'} = chop_str($title, 50, 5);
2465 last;
2466 }
2467 }
Joey Hess53c39672008-09-05 14:26:29 -04002468 if (! defined $co{'title'} || $co{'title'} eq "") {
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02002469 $co{'title'} = $co{'title_short'} = '(no commit message)';
2470 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002471 # remove added spaces
2472 foreach my $line (@commit_lines) {
2473 $line =~ s/^ //;
2474 }
2475 $co{'comment'} = \@commit_lines;
2476
2477 my $age = time - $co{'committer_epoch'};
2478 $co{'age'} = $age;
2479 $co{'age_string'} = age_string($age);
2480 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2481 if ($age > 60*60*24*7*2) {
2482 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2483 $co{'age_string_age'} = $co{'age_string'};
2484 } else {
2485 $co{'age_string_date'} = $co{'age_string'};
2486 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2487 }
2488 return %co;
2489}
2490
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002491sub parse_commit {
2492 my ($commit_id) = @_;
2493 my %co;
2494
2495 local $/ = "\0";
2496
2497 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002498 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002499 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002500 "--max-count=1",
2501 $commit_id,
2502 "--",
Lea Wiemann074afaa2008-06-19 22:03:21 +02002503 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00002504 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002505 close $fd;
2506
2507 return %co;
2508}
2509
2510sub parse_commits {
Jakub Narebski311e5522008-02-26 13:22:06 +01002511 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002512 my @cos;
2513
2514 $maxcount ||= 1;
2515 $skip ||= 0;
2516
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002517 local $/ = "\0";
2518
2519 open my $fd, "-|", git_cmd(), "rev-list",
2520 "--header",
Jakub Narebski311e5522008-02-26 13:22:06 +01002521 @args,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002522 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00002523 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02002524 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002525 $commit_id,
2526 "--",
2527 ($filename ? ($filename) : ())
Lea Wiemann074afaa2008-06-19 22:03:21 +02002528 or die_error(500, "Open git-rev-list failed");
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00002529 while (my $line = <$fd>) {
2530 my %co = parse_commit_text($line);
2531 push @cos, \%co;
2532 }
2533 close $fd;
2534
2535 return wantarray ? @cos : \@cos;
2536}
2537
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002538# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02002539sub parse_difftree_raw_line {
2540 my $line = shift;
2541 my %res;
2542
2543 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2544 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2545 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2546 $res{'from_mode'} = $1;
2547 $res{'to_mode'} = $2;
2548 $res{'from_id'} = $3;
2549 $res{'to_id'} = $4;
Jakub Narebski4ed4a342008-04-05 21:13:24 +01002550 $res{'status'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02002551 $res{'similarity'} = $6;
2552 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002553 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002554 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01002555 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002556 }
2557 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02002558 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2559 # combined diff (for merge commit)
2560 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2561 $res{'nparents'} = length($1);
2562 $res{'from_mode'} = [ split(' ', $2) ];
2563 $res{'to_mode'} = pop @{$res{'from_mode'}};
2564 $res{'from_id'} = [ split(' ', $3) ];
2565 $res{'to_id'} = pop @{$res{'from_id'}};
2566 $res{'status'} = [ split('', $4) ];
2567 $res{'to_file'} = unquote($5);
2568 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002569 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02002570 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2571 $res{'commit'} = $1;
2572 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002573
2574 return wantarray ? %res : \%res;
2575}
2576
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002577# wrapper: return parsed line of git-diff-tree "raw" output
2578# (the argument might be raw line, or parsed info)
2579sub parsed_difftree_line {
2580 my $line_or_ref = shift;
2581
2582 if (ref($line_or_ref) eq "HASH") {
2583 # pre-parsed (or generated by hand)
2584 return $line_or_ref;
2585 } else {
2586 return parse_difftree_raw_line($line_or_ref);
2587 }
2588}
2589
Jakub Narebskicb849b42006-08-31 00:32:15 +02002590# parse line of git-ls-tree output
2591sub parse_ls_tree_line ($;%) {
2592 my $line = shift;
2593 my %opts = @_;
2594 my %res;
2595
2596 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002597 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002598
2599 $res{'mode'} = $1;
2600 $res{'type'} = $2;
2601 $res{'hash'} = $3;
2602 if ($opts{'-z'}) {
2603 $res{'name'} = $4;
2604 } else {
2605 $res{'name'} = unquote($4);
2606 }
2607
2608 return wantarray ? %res : \%res;
2609}
2610
Jakub Narebski90921742007-06-08 13:27:42 +02002611# generates _two_ hashes, references to which are passed as 2 and 3 argument
2612sub parse_from_to_diffinfo {
2613 my ($diffinfo, $from, $to, @parents) = @_;
2614
2615 if ($diffinfo->{'nparents'}) {
2616 # combined diff
2617 $from->{'file'} = [];
2618 $from->{'href'} = [];
2619 fill_from_file_info($diffinfo, @parents)
2620 unless exists $diffinfo->{'from_file'};
2621 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01002622 $from->{'file'}[$i] =
2623 defined $diffinfo->{'from_file'}[$i] ?
2624 $diffinfo->{'from_file'}[$i] :
2625 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002626 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2627 $from->{'href'}[$i] = href(action=>"blob",
2628 hash_base=>$parents[$i],
2629 hash=>$diffinfo->{'from_id'}[$i],
2630 file_name=>$from->{'file'}[$i]);
2631 } else {
2632 $from->{'href'}[$i] = undef;
2633 }
2634 }
2635 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002636 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01002637 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002638 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2639 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2640 hash=>$diffinfo->{'from_id'},
2641 file_name=>$from->{'file'});
2642 } else {
2643 delete $from->{'href'};
2644 }
2645 }
2646
Jakub Narebski9d301452007-11-01 12:38:08 +01002647 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002648 if (!is_deleted($diffinfo)) { # file exists in result
2649 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2650 hash=>$diffinfo->{'to_id'},
2651 file_name=>$to->{'file'});
2652 } else {
2653 delete $to->{'href'};
2654 }
2655}
2656
Jakub Narebski717b8312006-07-31 21:22:15 +02002657## ......................................................................
2658## parse to array of hashes functions
2659
Jakub Narebskicd146402006-11-02 20:23:11 +01002660sub git_get_heads_list {
2661 my $limit = shift;
2662 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002663
Jakub Narebskicd146402006-11-02 20:23:11 +01002664 open my $fd, '-|', git_cmd(), 'for-each-ref',
2665 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2666 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2667 'refs/heads'
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002668 or return;
2669 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01002670 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02002671
Jakub Narebskicd146402006-11-02 20:23:11 +01002672 chomp $line;
2673 my ($refinfo, $committerinfo) = split(/\0/, $line);
2674 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2675 my ($committer, $epoch, $tz) =
2676 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01002677 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01002678 $name =~ s!^refs/heads/!!;
2679
2680 $ref_item{'name'} = $name;
2681 $ref_item{'id'} = $hash;
2682 $ref_item{'title'} = $title || '(no commit message)';
2683 $ref_item{'epoch'} = $epoch;
2684 if ($epoch) {
2685 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2686 } else {
2687 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02002688 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002689
2690 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002691 }
2692 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07002693
Jakub Narebskicd146402006-11-02 20:23:11 +01002694 return wantarray ? @headslist : \@headslist;
2695}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002696
Jakub Narebskicd146402006-11-02 20:23:11 +01002697sub git_get_tags_list {
2698 my $limit = shift;
2699 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002700
Jakub Narebskicd146402006-11-02 20:23:11 +01002701 open my $fd, '-|', git_cmd(), 'for-each-ref',
2702 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2703 '--format=%(objectname) %(objecttype) %(refname) '.
2704 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2705 'refs/tags'
2706 or return;
2707 while (my $line = <$fd>) {
2708 my %ref_item;
2709
2710 chomp $line;
2711 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2712 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2713 my ($creator, $epoch, $tz) =
2714 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
Jakub Narebskibf901f82007-12-15 15:40:28 +01002715 $ref_item{'fullname'} = $name;
Jakub Narebskicd146402006-11-02 20:23:11 +01002716 $name =~ s!^refs/tags/!!;
2717
2718 $ref_item{'type'} = $type;
2719 $ref_item{'id'} = $id;
2720 $ref_item{'name'} = $name;
2721 if ($type eq "tag") {
2722 $ref_item{'subject'} = $title;
2723 $ref_item{'reftype'} = $reftype;
2724 $ref_item{'refid'} = $refid;
2725 } else {
2726 $ref_item{'reftype'} = $type;
2727 $ref_item{'refid'} = $id;
2728 }
2729
2730 if ($type eq "tag" || $type eq "commit") {
2731 $ref_item{'epoch'} = $epoch;
2732 if ($epoch) {
2733 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2734 } else {
2735 $ref_item{'age'} = "unknown";
2736 }
2737 }
2738
2739 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02002740 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002741 close $fd;
2742
2743 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002744}
2745
2746## ----------------------------------------------------------------------
2747## filesystem-related functions
2748
2749sub get_file_owner {
2750 my $path = shift;
2751
2752 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2753 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2754 if (!defined $gcos) {
2755 return undef;
2756 }
2757 my $owner = $gcos;
2758 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02002759 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02002760}
2761
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01002762# assume that file exists
2763sub insert_file {
2764 my $filename = shift;
2765
2766 open my $fd, '<', $filename;
2767 print map(to_utf8, <$fd>);
2768 close $fd;
2769}
2770
Jakub Narebski717b8312006-07-31 21:22:15 +02002771## ......................................................................
2772## mimetype related functions
2773
2774sub mimetype_guess_file {
2775 my $filename = shift;
2776 my $mimemap = shift;
2777 -r $mimemap or return undef;
2778
2779 my %mimemap;
2780 open(MIME, $mimemap) or return undef;
2781 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02002782 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02002783 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07002784 if (defined $exts) {
2785 my @exts = split(/\s+/, $exts);
2786 foreach my $ext (@exts) {
2787 $mimemap{$ext} = $mime;
2788 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002789 }
2790 }
2791 close(MIME);
2792
Jakub Narebski80593192006-09-19 13:57:03 +02002793 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02002794 return $mimemap{$1};
2795}
2796
2797sub mimetype_guess {
2798 my $filename = shift;
2799 my $mime;
2800 $filename =~ /\./ or return undef;
2801
2802 if ($mimetypes_file) {
2803 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02002804 if ($file !~ m!^/!) { # if it is relative path
2805 # it is relative to project
2806 $file = "$projectroot/$project/$file";
2807 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002808 $mime = mimetype_guess_file($filename, $file);
2809 }
2810 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2811 return $mime;
2812}
2813
Jakub Narebski847e01f2006-08-14 02:05:47 +02002814sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02002815 my $fd = shift;
2816 my $filename = shift;
2817
2818 if ($filename) {
2819 my $mime = mimetype_guess($filename);
2820 $mime and return $mime;
2821 }
2822
2823 # just in case
2824 return $default_blob_plain_mimetype unless $fd;
2825
2826 if (-T $fd) {
Jakub Narebski7f718e82008-06-03 16:47:10 +02002827 return 'text/plain';
Jakub Narebski717b8312006-07-31 21:22:15 +02002828 } elsif (! $filename) {
2829 return 'application/octet-stream';
2830 } elsif ($filename =~ m/\.png$/i) {
2831 return 'image/png';
2832 } elsif ($filename =~ m/\.gif$/i) {
2833 return 'image/gif';
2834 } elsif ($filename =~ m/\.jpe?g$/i) {
2835 return 'image/jpeg';
2836 } else {
2837 return 'application/octet-stream';
2838 }
2839}
2840
Jakub Narebski7f718e82008-06-03 16:47:10 +02002841sub blob_contenttype {
2842 my ($fd, $file_name, $type) = @_;
2843
2844 $type ||= blob_mimetype($fd, $file_name);
2845 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2846 $type .= "; charset=$default_text_plain_charset";
2847 }
2848
2849 return $type;
2850}
2851
Jakub Narebski717b8312006-07-31 21:22:15 +02002852## ======================================================================
2853## functions printing HTML: header, footer, error page
2854
Kay Sievers12a88f22005-08-07 20:02:47 +02002855sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02002856 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02002857 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002858
Petr Baudis8be28902006-10-24 05:18:39 +02002859 my $title = "$site_name";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002860 if (defined $project) {
Martin Koegler00f429a2007-06-03 17:42:44 +02002861 $title .= " - " . to_utf8($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002862 if (defined $action) {
2863 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002864 if (defined $file_name) {
Jakub Narebski403d0902006-11-08 11:48:56 +01002865 $title .= " - " . esc_path($file_name);
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002866 if ($action eq "tree" && $file_name !~ m|/$|) {
2867 $title .= "/";
2868 }
2869 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02002870 }
2871 }
Alp Tokerf6801d62006-07-11 11:19:34 +01002872 my $content_type;
2873 # require explicit support from the UA if we are to send the page as
2874 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2875 # we have to do this because MSIE sometimes globs '*/*', pretending to
2876 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02002877 if (defined $cgi->http('HTTP_ACCEPT') &&
2878 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2879 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01002880 $content_type = 'application/xhtml+xml';
2881 } else {
2882 $content_type = 'text/html';
2883 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002884 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2885 -status=> $status, -expires => $expires);
Jakub Narebski45c9a752006-12-27 23:59:51 +01002886 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02002887 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02002888<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02002889<!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 +02002890<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02002891<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02002892<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02002893<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01002894<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01002895<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02002896<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02002897<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02002898EOF
Alan Chandlerb2d34762006-10-03 13:49:03 +01002899# print out each stylesheet that exist
2900 if (defined $stylesheet) {
2901#provides backwards capability for those people who define style sheet in a config file
2902 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2903 } else {
2904 foreach my $stylesheet (@stylesheets) {
2905 next unless $stylesheet;
2906 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2907 }
2908 }
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002909 if (defined $project) {
Jakub Narebski35621982008-04-20 22:09:48 +02002910 my %href_params = get_feed_info();
2911 if (!exists $href_params{'-title'}) {
2912 $href_params{'-title'} = 'log';
2913 }
2914
2915 foreach my $format qw(RSS Atom) {
2916 my $type = lc($format);
2917 my %link_attr = (
2918 '-rel' => 'alternate',
2919 '-title' => "$project - $href_params{'-title'} - $format feed",
2920 '-type' => "application/$type+xml"
2921 );
2922
2923 $href_params{'action'} = $type;
2924 $link_attr{'-href'} = href(%href_params);
2925 print "<link ".
2926 "rel=\"$link_attr{'-rel'}\" ".
2927 "title=\"$link_attr{'-title'}\" ".
2928 "href=\"$link_attr{'-href'}\" ".
2929 "type=\"$link_attr{'-type'}\" ".
2930 "/>\n";
2931
2932 $href_params{'extra_options'} = '--no-merges';
2933 $link_attr{'-href'} = href(%href_params);
2934 $link_attr{'-title'} .= ' (no merges)';
2935 print "<link ".
2936 "rel=\"$link_attr{'-rel'}\" ".
2937 "title=\"$link_attr{'-title'}\" ".
2938 "href=\"$link_attr{'-href'}\" ".
2939 "type=\"$link_attr{'-type'}\" ".
2940 "/>\n";
2941 }
2942
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002943 } else {
2944 printf('<link rel="alternate" title="%s projects list" '.
Jakub Narebski35621982008-04-20 22:09:48 +02002945 'href="%s" type="text/plain; charset=utf-8" />'."\n",
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002946 $site_name, href(project=>undef, action=>"project_index"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002947 printf('<link rel="alternate" title="%s projects feeds" '.
Jakub Narebski35621982008-04-20 22:09:48 +02002948 'href="%s" type="text/x-opml" />'."\n",
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002949 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002950 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002951 if (defined $favicon) {
Jakub Narebski35621982008-04-20 22:09:48 +02002952 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002953 }
Jakub Narebski10161352006-08-05 13:18:58 +02002954
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002955 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01002956 "<body>\n";
2957
2958 if (-f $site_header) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01002959 insert_file($site_header);
Alan Chandlerb2d34762006-10-03 13:49:03 +01002960 }
2961
2962 print "<div class=\"page_header\">\n" .
Jakub Narebski9a7a62f2006-10-06 12:31:05 +02002963 $cgi->a({-href => esc_url($logo_url),
2964 -title => $logo_label},
2965 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
Jakub Narebskif93bff82006-09-26 01:58:41 +02002966 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002967 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002968 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02002969 if (defined $action) {
2970 print " / $action";
2971 }
Kay Sievers19806692005-08-07 20:26:27 +02002972 print "\n";
Robert Fitzsimons6be93512006-12-23 03:35:16 +00002973 }
Petr Baudisd77b5672007-05-17 04:24:19 +02002974 print "</div>\n";
2975
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08002976 my $have_search = gitweb_check_feature('search');
Jakub Narebskif70dda22008-06-02 11:54:41 +02002977 if (defined $project && $have_search) {
Kay Sievers19806692005-08-07 20:26:27 +02002978 if (!defined $searchtext) {
2979 $searchtext = "";
2980 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02002981 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03002982 if (defined $hash_base) {
2983 $search_hash = $hash_base;
2984 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02002985 $search_hash = $hash;
2986 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002987 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02002988 }
Matt McCutchen40375a82007-06-28 14:57:07 -04002989 my $action = $my_uri;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08002990 my $use_pathinfo = gitweb_check_feature('pathinfo');
Matt McCutchen40375a82007-06-28 14:57:07 -04002991 if ($use_pathinfo) {
martin f. krafft85d17a12008-04-20 23:23:38 +02002992 $action .= "/".esc_url($project);
Matt McCutchen40375a82007-06-28 14:57:07 -04002993 }
Matt McCutchen40375a82007-06-28 14:57:07 -04002994 print $cgi->startform(-method => "get", -action => $action) .
Kay Sieversc994d622005-08-07 20:27:18 +02002995 "<div class=\"search\">\n" .
Jakub Narebskif70dda22008-06-02 11:54:41 +02002996 (!$use_pathinfo &&
2997 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2998 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2999 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
Petr Baudis88ad7292006-10-24 05:15:46 +02003000 $cgi->popup_menu(-name => 'st', -default => 'commit',
Petr Baudise7738552007-05-17 04:31:12 +02003001 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Petr Baudis88ad7292006-10-24 05:15:46 +02003002 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3003 " search:\n",
Kay Sieversc994d622005-08-07 20:27:18 +02003004 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
Petr Baudis0e559912008-02-26 13:22:08 +01003005 "<span title=\"Extended regular expression\">" .
3006 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3007 -checked => $search_use_regexp) .
3008 "</span>" .
Kay Sieversc994d622005-08-07 20:27:18 +02003009 "</div>" .
3010 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003011 }
Kay Sievers161332a2005-08-07 19:49:46 +02003012}
3013
Kay Sievers12a88f22005-08-07 20:02:47 +02003014sub git_footer_html {
Jakub Narebski35621982008-04-20 22:09:48 +02003015 my $feed_class = 'rss_logo';
3016
Kay Sievers6191f8e2005-08-07 20:19:56 +02003017 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02003018 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003019 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02003020 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01003021 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02003022 }
Jakub Narebski35621982008-04-20 22:09:48 +02003023
3024 my %href_params = get_feed_info();
3025 if (!%href_params) {
3026 $feed_class .= ' generic';
3027 }
3028 $href_params{'-title'} ||= 'log';
3029
3030 foreach my $format qw(RSS Atom) {
3031 $href_params{'action'} = lc($format);
3032 print $cgi->a({-href => href(%href_params),
3033 -title => "$href_params{'-title'} $format feed",
3034 -class => $feed_class}, $format)."\n";
3035 }
3036
Kay Sieversc994d622005-08-07 20:27:18 +02003037 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02003038 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski35621982008-04-20 22:09:48 +02003039 -class => $feed_class}, "OPML") . " ";
Jakub Narebski9d0734a2006-09-15 11:11:33 +02003040 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
Jakub Narebski35621982008-04-20 22:09:48 +02003041 -class => $feed_class}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02003042 }
Jakub Narebski35621982008-04-20 22:09:48 +02003043 print "</div>\n"; # class="page_footer"
Alan Chandlerb2d34762006-10-03 13:49:03 +01003044
3045 if (-f $site_footer) {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01003046 insert_file($site_footer);
Alan Chandlerb2d34762006-10-03 13:49:03 +01003047 }
3048
3049 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02003050 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02003051}
3052
Lea Wiemann074afaa2008-06-19 22:03:21 +02003053# die_error(<http_status_code>, <error_message>)
3054# Example: die_error(404, 'Hash not found')
3055# By convention, use the following status codes (as defined in RFC 2616):
3056# 400: Invalid or missing CGI parameters, or
3057# requested object exists but has wrong type.
3058# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3059# this server or project.
3060# 404: Requested object/revision/project doesn't exist.
3061# 500: The server isn't configured properly, or
3062# an internal error occurred (e.g. failed assertions caused by bugs), or
3063# an unknown error occurred (e.g. the git binary died unexpectedly).
Kay Sievers061cc7c2005-08-07 20:15:57 +02003064sub die_error {
Lea Wiemann074afaa2008-06-19 22:03:21 +02003065 my $status = shift || 500;
3066 my $error = shift || "Internal server error";
Kay Sievers664f4cc2005-08-07 20:17:19 +02003067
Lea Wiemann074afaa2008-06-19 22:03:21 +02003068 my %http_responses = (400 => '400 Bad Request',
3069 403 => '403 Forbidden',
3070 404 => '404 Not Found',
3071 500 => '500 Internal Server Error');
3072 git_header_html($http_responses{$status});
Jakub Narebski59b9f612006-08-22 23:42:53 +02003073 print <<EOF;
3074<div class="page_body">
3075<br /><br />
3076$status - $error
3077<br />
3078</div>
3079EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02003080 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02003081 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02003082}
3083
Jakub Narebski717b8312006-07-31 21:22:15 +02003084## ----------------------------------------------------------------------
3085## functions printing or outputting HTML: navigation
3086
Jakub Narebski847e01f2006-08-14 02:05:47 +02003087sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003088 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3089 $extra = '' if !defined $extra; # pager or formats
3090
3091 my @navs = qw(summary shortlog log commit commitdiff tree);
3092 if ($suppress) {
3093 @navs = grep { $_ ne $suppress } @navs;
3094 }
3095
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003096 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003097 if (defined $head) {
3098 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02003099 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003100 }
3101 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3102 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02003103 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003104 }
3105 }
3106 }
Petr Baudisd627f682008-10-02 16:36:52 +02003107
Jakub Narebski3be8e722007-04-01 22:22:21 +02003108 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3109 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003110
Junio C Hamanoa7c5a282008-11-29 13:02:08 -08003111 my @actions = gitweb_get_feature('actions');
Jakub Narebski2b11e052008-10-12 00:02:32 +02003112 my %repl = (
3113 '%' => '%',
3114 'n' => $project, # project name
3115 'f' => $git_dir, # project path within filesystem
3116 'h' => $treehead || '', # current hash ('h' parameter)
3117 'b' => $treebase || '', # hash base ('hb' parameter)
3118 );
Petr Baudisd627f682008-10-02 16:36:52 +02003119 while (@actions) {
Jakub Narebski2b11e052008-10-12 00:02:32 +02003120 my ($label, $link, $pos) = splice(@actions,0,3);
3121 # insert
Petr Baudisd627f682008-10-02 16:36:52 +02003122 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3123 # munch munch
Jakub Narebski2b11e052008-10-12 00:02:32 +02003124 $link =~ s/%([%nfhb])/$repl{$1}/g;
Petr Baudisd627f682008-10-02 16:36:52 +02003125 $arg{$label}{'_href'} = $link;
3126 }
3127
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003128 print "<div class=\"page_nav\">\n" .
3129 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003130 map { $_ eq $current ?
Petr Baudisd627f682008-10-02 16:36:52 +02003131 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003132 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02003133 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02003134 "</div>\n";
3135}
3136
Jakub Narebski847e01f2006-08-14 02:05:47 +02003137sub format_paging_nav {
Lea Wiemann1f684dc2008-05-28 01:25:42 +02003138 my ($action, $hash, $head, $page, $has_next_link) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02003139 my $paging_nav;
3140
3141
3142 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003143 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003144 } else {
3145 $paging_nav .= "HEAD";
3146 }
3147
3148 if ($page > 0) {
3149 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01003150 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02003151 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003152 } else {
3153 $paging_nav .= " &sdot; prev";
3154 }
3155
Lea Wiemann1f684dc2008-05-28 01:25:42 +02003156 if ($has_next_link) {
Jakub Narebski43ffc062006-07-30 17:49:00 +02003157 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01003158 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02003159 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02003160 } else {
3161 $paging_nav .= " &sdot; next";
3162 }
3163
3164 return $paging_nav;
3165}
3166
Jakub Narebski717b8312006-07-31 21:22:15 +02003167## ......................................................................
3168## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02003169
Jakub Narebski847e01f2006-08-14 02:05:47 +02003170sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02003171 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003172 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02003173
Jakub Narebski3be8e722007-04-01 22:22:21 +02003174 $args{'action'} = $action;
3175 $args{'hash'} = $hash if $hash;
3176 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02003177
3178 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003179 $cgi->a({-href => href(%args), -class => "title"},
3180 $title ? $title : $action) .
3181 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02003182}
3183
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003184#sub git_print_authorship (\%) {
3185sub git_print_authorship {
3186 my $co = shift;
3187
Jakub Narebskia44465c2006-08-28 23:17:31 +02003188 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003189 print "<div class=\"author_date\">" .
3190 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02003191 " [$ad{'rfc2822'}";
3192 if ($ad{'hour_local'} < 6) {
3193 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3194 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3195 } else {
3196 printf(" (%02d:%02d %s)",
3197 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3198 }
3199 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003200}
3201
Jakub Narebski717b8312006-07-31 21:22:15 +02003202sub git_print_page_path {
3203 my $name = shift;
3204 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003205 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01003206
Jakub Narebski4df118e2006-10-21 17:53:55 +02003207
3208 print "<div class=\"page_path\">";
3209 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02003210 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02003211 print " / ";
3212 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02003213 my @dirname = split '/', $name;
3214 my $basename = pop @dirname;
3215 my $fullname = '';
3216
Jakub Narebski762c7202006-09-04 18:17:58 +02003217 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02003218 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02003219 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3220 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003221 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02003222 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02003223 }
3224 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02003225 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02003226 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003227 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02003228 } elsif (defined $type && $type eq 'tree') {
3229 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3230 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01003231 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02003232 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003233 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01003234 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003235 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02003236 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02003237 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003238}
3239
Jakub Narebskib7f92532006-08-28 14:48:10 +02003240# sub git_print_log (\@;%) {
3241sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02003242 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02003243 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02003244
Jakub Narebskib7f92532006-08-28 14:48:10 +02003245 if ($opts{'-remove_title'}) {
3246 # remove title, i.e. first line of log
3247 shift @$log;
3248 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02003249 # remove leading empty lines
3250 while (defined $log->[0] && $log->[0] eq "") {
3251 shift @$log;
3252 }
3253
3254 # print log
3255 my $signoff = 0;
3256 my $empty = 0;
3257 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02003258 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3259 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02003260 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02003261 if (! $opts{'-remove_signoff'}) {
3262 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3263 next;
3264 } else {
3265 # remove signoff lines
3266 next;
3267 }
3268 } else {
3269 $signoff = 0;
3270 }
3271
Jakub Narebskid16d0932006-08-17 11:21:23 +02003272 # print only one empty line
3273 # do not print empty line after signoff
3274 if ($line eq "") {
3275 next if ($empty || $signoff);
3276 $empty = 1;
3277 } else {
3278 $empty = 0;
3279 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02003280
3281 print format_log_line_html($line) . "<br/>\n";
3282 }
3283
3284 if ($opts{'-final_empty_line'}) {
3285 # end with single empty line
3286 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02003287 }
3288}
3289
Jakub Narebskie33fba42006-12-10 13:25:46 +01003290# return link target (what link points to)
3291sub git_get_link_target {
3292 my $hash = shift;
3293 my $link_target;
3294
3295 # read link
3296 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3297 or return;
3298 {
3299 local $/;
3300 $link_target = <$fd>;
3301 }
3302 close $fd
3303 or return;
3304
3305 return $link_target;
3306}
3307
Jakub Narebski3bf9d572006-12-10 13:25:48 +01003308# given link target, and the directory (basedir) the link is in,
3309# return target of link relative to top directory (top tree);
3310# return undef if it is not possible (including absolute links).
3311sub normalize_link_target {
3312 my ($link_target, $basedir, $hash_base) = @_;
3313
3314 # we can normalize symlink target only if $hash_base is provided
3315 return unless $hash_base;
3316
3317 # absolute symlinks (beginning with '/') cannot be normalized
3318 return if (substr($link_target, 0, 1) eq '/');
3319
3320 # normalize link target to path from top (root) tree (dir)
3321 my $path;
3322 if ($basedir) {
3323 $path = $basedir . '/' . $link_target;
3324 } else {
3325 # we are in top (root) tree (dir)
3326 $path = $link_target;
3327 }
3328
3329 # remove //, /./, and /../
3330 my @path_parts;
3331 foreach my $part (split('/', $path)) {
3332 # discard '.' and ''
3333 next if (!$part || $part eq '.');
3334 # handle '..'
3335 if ($part eq '..') {
3336 if (@path_parts) {
3337 pop @path_parts;
3338 } else {
3339 # link leads outside repository (outside top dir)
3340 return;
3341 }
3342 } else {
3343 push @path_parts, $part;
3344 }
3345 }
3346 $path = join('/', @path_parts);
3347
3348 return $path;
3349}
Jakub Narebskie33fba42006-12-10 13:25:46 +01003350
Jakub Narebskifa702002006-08-31 00:35:07 +02003351# print tree entry (row of git_tree), but without encompassing <tr> element
3352sub git_print_tree_entry {
3353 my ($t, $basedir, $hash_base, $have_blame) = @_;
3354
3355 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01003356 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02003357
Luben Tuikov4de741b2006-09-25 22:38:16 -07003358 # The format of a table row is: mode list link. Where mode is
3359 # the mode of the entry, list is the name of the entry, an href,
3360 # and link is the action links of the entry.
3361
Jakub Narebskifa702002006-08-31 00:35:07 +02003362 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3363 if ($t->{'type'} eq "blob") {
3364 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07003365 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003366 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01003367 -class => "list"}, esc_path($t->{'name'}));
3368 if (S_ISLNK(oct $t->{'mode'})) {
3369 my $link_target = git_get_link_target($t->{'hash'});
3370 if ($link_target) {
Jakub Narebski3bf9d572006-12-10 13:25:48 +01003371 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3372 if (defined $norm_target) {
3373 print " -> " .
3374 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3375 file_name=>$norm_target),
3376 -title => $norm_target}, esc_path($link_target));
3377 } else {
3378 print " -> " . esc_path($link_target);
3379 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01003380 }
3381 }
3382 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07003383 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02003384 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003385 file_name=>"$basedir$t->{'name'}", %base_key)},
3386 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02003387 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02003388 print " | " .
3389 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003390 file_name=>"$basedir$t->{'name'}", %base_key)},
3391 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02003392 }
3393 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02003394 print " | " .
3395 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02003396 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3397 "history");
3398 }
3399 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07003400 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003401 file_name=>"$basedir$t->{'name'}")},
3402 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07003403 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02003404
3405 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07003406 print "<td class=\"list\">";
3407 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskifa702002006-08-31 00:35:07 +02003408 file_name=>"$basedir$t->{'name'}", %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01003409 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07003410 print "</td>\n";
3411 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02003412 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01003413 file_name=>"$basedir$t->{'name'}", %base_key)},
3414 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02003415 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02003416 print " | " .
3417 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02003418 file_name=>"$basedir$t->{'name'}")},
3419 "history");
3420 }
3421 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02003422 } else {
3423 # unknown object: we can only present history for it
3424 # (this includes 'commit' object, i.e. submodule support)
3425 print "<td class=\"list\">" .
3426 esc_path($t->{'name'}) .
3427 "</td>\n";
3428 print "<td class=\"link\">";
3429 if (defined $hash_base) {
3430 print $cgi->a({-href => href(action=>"history",
3431 hash_base=>$hash_base,
3432 file_name=>"$basedir$t->{'name'}")},
3433 "history");
3434 }
3435 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02003436 }
3437}
3438
Jakub Narebski717b8312006-07-31 21:22:15 +02003439## ......................................................................
3440## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02003441
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003442# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003443sub fill_from_file_info {
3444 my ($diff, @parents) = @_;
3445
3446 $diff->{'from_file'} = [ ];
3447 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3448 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3449 if ($diff->{'status'}[$i] eq 'R' ||
3450 $diff->{'status'}[$i] eq 'C') {
3451 $diff->{'from_file'}[$i] =
3452 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3453 }
3454 }
3455
3456 return $diff;
3457}
3458
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003459# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02003460sub is_deleted {
3461 my $diffinfo = shift;
3462
Jakub Narebski4ed4a342008-04-05 21:13:24 +01003463 return $diffinfo->{'to_id'} eq ('0' x 40);
Jakub Narebski90921742007-06-08 13:27:42 +02003464}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003465
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003466# does patch correspond to [previous] difftree raw line
3467# $diffinfo - hashref of parsed raw diff format
3468# $patchinfo - hashref of parsed patch diff format
3469# (the same keys as in $diffinfo)
3470sub is_patch_split {
3471 my ($diffinfo, $patchinfo) = @_;
3472
3473 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01003474 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003475}
3476
3477
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003478sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02003479 my ($difftree, $hash, @parents) = @_;
3480 my ($parent) = $parents[0];
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003481 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003482 print "<div class=\"list_head\">\n";
3483 if ($#{$difftree} > 10) {
3484 print(($#{$difftree} + 1) . " files changed:\n");
3485 }
3486 print "</div>\n";
3487
Jakub Narebskied224de2007-05-07 01:10:04 +02003488 print "<table class=\"" .
3489 (@parents > 1 ? "combined " : "") .
3490 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02003491
3492 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02003493 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02003494 if ($has_header) {
3495 # table header
3496 print "<thead><tr>\n" .
3497 "<th></th><th></th>\n"; # filename, patchN link
3498 for (my $i = 0; $i < @parents; $i++) {
3499 my $par = $parents[$i];
3500 print "<th>" .
3501 $cgi->a({-href => href(action=>"commitdiff",
3502 hash=>$hash, hash_parent=>$par),
3503 -title => 'commitdiff to parent number ' .
3504 ($i+1) . ': ' . substr($par,0,7)},
3505 $i+1) .
3506 "&nbsp;</th>\n";
3507 }
3508 print "</tr></thead>\n<tbody>\n";
3509 }
3510
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003511 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02003512 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003513 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003514 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003515
3516 if ($alternate) {
3517 print "<tr class=\"dark\">\n";
3518 } else {
3519 print "<tr class=\"light\">\n";
3520 }
3521 $alternate ^= 1;
3522
Jakub Narebski493e01d2007-05-07 01:10:06 +02003523 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02003524
Jakub Narebski493e01d2007-05-07 01:10:06 +02003525 fill_from_file_info($diff, @parents)
3526 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003527
Jakub Narebski90921742007-06-08 13:27:42 +02003528 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003529 # file exists in the result (child) commit
3530 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02003531 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3532 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003533 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02003534 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02003535 "</td>\n";
3536 } else {
3537 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02003538 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02003539 "</td>\n";
3540 }
3541
3542 if ($action eq 'commitdiff') {
3543 # link to patch
3544 $patchno++;
3545 print "<td class=\"link\">" .
3546 $cgi->a({-href => "#patch$patchno"}, "patch") .
3547 " | " .
3548 "</td>\n";
3549 }
3550
3551 my $has_history = 0;
3552 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02003553 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003554 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02003555 my $from_hash = $diff->{'from_id'}[$i];
3556 my $from_path = $diff->{'from_file'}[$i];
3557 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02003558
3559 $has_history ||= ($status ne 'A');
3560 $not_deleted ||= ($status ne 'D');
3561
Jakub Narebskied224de2007-05-07 01:10:04 +02003562 if ($status eq 'A') {
3563 print "<td class=\"link\" align=\"right\"> | </td>\n";
3564 } elsif ($status eq 'D') {
3565 print "<td class=\"link\">" .
3566 $cgi->a({-href => href(action=>"blob",
3567 hash_base=>$hash,
3568 hash=>$from_hash,
3569 file_name=>$from_path)},
3570 "blob" . ($i+1)) .
3571 " | </td>\n";
3572 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02003573 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02003574 print "<td class=\"link nochange\">";
3575 } else {
3576 print "<td class=\"link\">";
3577 }
3578 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003579 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003580 hash_parent=>$from_hash,
3581 hash_base=>$hash,
3582 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003583 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003584 file_parent=>$from_path)},
3585 "diff" . ($i+1)) .
3586 " | </td>\n";
3587 }
3588 }
3589
3590 print "<td class=\"link\">";
3591 if ($not_deleted) {
3592 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003593 hash=>$diff->{'to_id'},
3594 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003595 hash_base=>$hash)},
3596 "blob");
3597 print " | " if ($has_history);
3598 }
3599 if ($has_history) {
3600 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003601 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02003602 hash_base=>$hash)},
3603 "history");
3604 }
3605 print "</td>\n";
3606
3607 print "</tr>\n";
3608 next; # instead of 'else' clause, to avoid extra indent
3609 }
3610 # else ordinary diff
3611
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003612 my ($to_mode_oct, $to_mode_str, $to_file_type);
3613 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02003614 if ($diff->{'to_mode'} ne ('0' x 6)) {
3615 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003616 if (S_ISREG($to_mode_oct)) { # only for regular file
3617 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003618 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003619 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003620 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003621 if ($diff->{'from_mode'} ne ('0' x 6)) {
3622 $from_mode_oct = oct $diff->{'from_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003623 if (S_ISREG($to_mode_oct)) { # only for regular file
3624 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3625 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003626 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003627 }
3628
Jakub Narebski493e01d2007-05-07 01:10:06 +02003629 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003630 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3631 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3632 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003633 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003634 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3635 hash_base=>$hash, file_name=>$diff->{'file'}),
3636 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003637 print "</td>\n";
3638 print "<td>$mode_chng</td>\n";
3639 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003640 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003641 # link to patch
3642 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003643 print $cgi->a({-href => "#patch$patchno"}, "patch");
Jakub Narebski897d1d22006-11-19 22:51:39 +01003644 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003645 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003646 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3647 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01003648 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02003649 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003650
Jakub Narebski493e01d2007-05-07 01:10:06 +02003651 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003652 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003653 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003654 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3655 hash_base=>$parent, file_name=>$diff->{'file'}),
3656 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003657 print "</td>\n";
3658 print "<td>$mode_chng</td>\n";
3659 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003660 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003661 # link to patch
3662 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003663 print $cgi->a({-href => "#patch$patchno"}, "patch");
3664 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003665 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003666 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3667 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003668 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003669 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003670 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003671 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003672 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003673 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02003674 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003675 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003676 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07003677 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003678
Jakub Narebski493e01d2007-05-07 01:10:06 +02003679 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003680 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003681 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003682 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01003683 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003684 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003685 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003686 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3687 if ($from_mode_str && $to_mode_str) {
3688 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3689 } elsif ($to_mode_str) {
3690 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003691 }
3692 }
3693 $mode_chnge .= "]</span>\n";
3694 }
3695 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003696 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3697 hash_base=>$hash, file_name=>$diff->{'file'}),
3698 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003699 print "</td>\n";
3700 print "<td>$mode_chnge</td>\n";
3701 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003702 if ($action eq 'commitdiff') {
3703 # link to patch
3704 $patchno++;
3705 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3706 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003707 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003708 # "commit" view and modified file (not onlu mode changed)
3709 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003710 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003711 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003712 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003713 "diff") .
3714 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003715 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003716 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3717 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003718 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003719 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003720 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003721 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003722 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003723 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07003724 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003725 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003726 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003727 print "</td>\n";
3728
Jakub Narebski493e01d2007-05-07 01:10:06 +02003729 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003730 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02003731 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003732 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003733 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003734 # mode also for directories, so we cannot use $to_mode_str
3735 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003736 }
3737 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003738 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003739 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3740 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003741 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3742 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003743 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3744 -class => "list"}, esc_path($diff->{'from_file'})) .
3745 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07003746 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003747 if ($action eq 'commitdiff') {
3748 # link to patch
3749 $patchno++;
3750 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3751 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003752 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003753 # "commit" view and modified file (not only pure rename or copy)
3754 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003755 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003756 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003757 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003758 "diff") .
3759 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003760 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003761 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3762 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003763 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003764 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003765 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003766 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003767 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003768 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01003769 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003770 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003771 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003772 print "</td>\n";
3773
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003774 } # we should not encounter Unmerged (U) or Unknown (X) status
3775 print "</tr>\n";
3776 }
Jakub Narebski47598d72007-06-08 13:24:56 +02003777 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003778 print "</table>\n";
3779}
3780
Jakub Narebskieee08902006-08-24 00:15:14 +02003781sub git_patchset_body {
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003782 my ($fd, $difftree, $hash, @hash_parents) = @_;
3783 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02003784
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003785 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02003786 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07003787 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003788 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02003789 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003790 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003791 my (%from, %to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003792
3793 print "<div class=\"patchset\">\n";
3794
Jakub Narebski6d55f052006-11-18 23:35:39 +01003795 # skip to first patch
3796 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02003797 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02003798
Jakub Narebski6d55f052006-11-18 23:35:39 +01003799 last if ($patch_line =~ m/^diff /);
3800 }
3801
3802 PATCH:
3803 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01003804
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003805 # parse "git diff" header line
3806 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3807 # $1 is from_name, which we do not use
3808 $to_name = unquote($2);
3809 $to_name =~ s!^b/!!;
3810 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3811 # $1 is 'cc' or 'combined', which we do not use
3812 $to_name = unquote($2);
3813 } else {
3814 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003815 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003816
3817 # check if current patch belong to current raw line
3818 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003819 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02003820 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01003821 print "<div class=\"patch cont\">\n";
3822 } else {
3823 # advance raw git-diff output if needed
3824 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02003825
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003826 # read and prepare patch information
3827 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3828
Jakub Narebskicd030c32007-06-08 13:33:28 +02003829 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003830 # find which patch (using pathname of result) we are at now;
3831 if ($is_combined) {
3832 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003833 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3834 format_diff_cc_simplified($diffinfo, @hash_parents) .
3835 "</div>\n"; # class="patch"
3836
3837 $patch_idx++;
3838 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003839
3840 last if $patch_idx > $#$difftree;
3841 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003842 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003843 }
Jakub Narebski711fa742007-09-08 21:49:11 +02003844
Jakub Narebski90921742007-06-08 13:27:42 +02003845 # modifies %from, %to hashes
3846 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02003847
Jakub Narebski6d55f052006-11-18 23:35:39 +01003848 # this is first patch for raw difftree line with $patch_idx index
3849 # we index @$difftree array from 0, but number patches from 1
3850 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003851 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003852
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003853 # git diff header
3854 #assert($patch_line =~ m/^diff /) if DEBUG;
3855 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3856 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003857 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02003858 print format_git_diff_header_line($patch_line, $diffinfo,
3859 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003860
Jakub Narebski6d55f052006-11-18 23:35:39 +01003861 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003862 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01003863 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003864 while ($patch_line = <$fd>) {
3865 chomp $patch_line;
3866
3867 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3868
Jakub Narebski90921742007-06-08 13:27:42 +02003869 print format_extended_diff_header_line($patch_line, $diffinfo,
3870 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003871 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003872 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02003873
Jakub Narebski6d55f052006-11-18 23:35:39 +01003874 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01003875 if (! $patch_line) {
3876 print "</div>\n"; # class="patch"
3877 last PATCH;
3878 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01003879 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003880 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003881
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003882 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003883 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003884 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02003885 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003886
Jakub Narebski90921742007-06-08 13:27:42 +02003887 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02003888 $diffinfo, \%from, \%to,
3889 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003890
3891 # the patch itself
3892 LINE:
3893 while ($patch_line = <$fd>) {
3894 chomp $patch_line;
3895
3896 next PATCH if ($patch_line =~ m/^diff /);
3897
Jakub Narebski59e3b142006-11-18 23:35:40 +01003898 print format_diff_line($patch_line, \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003899 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003900
Jakub Narebski6d55f052006-11-18 23:35:39 +01003901 } continue {
3902 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02003903 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02003904
Jakub Narebskicd030c32007-06-08 13:33:28 +02003905 # for compact combined (--cc) format, with chunk and patch simpliciaction
3906 # patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003907 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02003908 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003909 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003910 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003911 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003912
3913 # generate anchor for "patch" links in difftree / whatchanged part
3914 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3915 format_diff_cc_simplified($diffinfo, @hash_parents) .
3916 "</div>\n"; # class="patch"
3917
3918 $patch_number++;
3919 }
3920
Jakub Narebskid26c4262007-05-17 00:05:55 +02003921 if ($patch_number == 0) {
3922 if (@hash_parents > 1) {
3923 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3924 } else {
3925 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3926 }
3927 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003928
3929 print "</div>\n"; # class="patchset"
3930}
3931
3932# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3933
Jakub Narebski69913412008-06-10 19:21:01 +02003934# fills project list info (age, description, owner, forks) for each
3935# project in the list, removing invalid projects from returned list
3936# NOTE: modifies $projlist, but does not remove entries from it
3937sub fill_project_list_info {
3938 my ($projlist, $check_forks) = @_;
Petr Baudise30496d2006-10-24 05:33:17 +02003939 my @projects;
Jakub Narebski69913412008-06-10 19:21:01 +02003940
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003941 my $show_ctags = gitweb_check_feature('ctags');
Jakub Narebski69913412008-06-10 19:21:01 +02003942 PROJECT:
Petr Baudise30496d2006-10-24 05:33:17 +02003943 foreach my $pr (@$projlist) {
Jakub Narebski69913412008-06-10 19:21:01 +02003944 my (@activity) = git_get_last_activity($pr->{'path'});
3945 unless (@activity) {
3946 next PROJECT;
Petr Baudise30496d2006-10-24 05:33:17 +02003947 }
Jakub Narebski69913412008-06-10 19:21:01 +02003948 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
Petr Baudise30496d2006-10-24 05:33:17 +02003949 if (!defined $pr->{'descr'}) {
3950 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski69913412008-06-10 19:21:01 +02003951 $descr = to_utf8($descr);
3952 $pr->{'descr_long'} = $descr;
Michael Hendricks55feb122007-07-04 18:36:48 -06003953 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02003954 }
3955 if (!defined $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003956 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02003957 }
3958 if ($check_forks) {
3959 my $pname = $pr->{'path'};
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003960 if (($pname =~ s/\.git$//) &&
3961 ($pname !~ /\/$/) &&
3962 (-d "$projectroot/$pname")) {
3963 $pr->{'forks'} = "-d $projectroot/$pname";
Jakub Narebski69913412008-06-10 19:21:01 +02003964 } else {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003965 $pr->{'forks'} = 0;
3966 }
Petr Baudise30496d2006-10-24 05:33:17 +02003967 }
Petr Baudisaed93de2008-10-02 17:13:02 +02003968 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
Petr Baudise30496d2006-10-24 05:33:17 +02003969 push @projects, $pr;
3970 }
3971
Jakub Narebski69913412008-06-10 19:21:01 +02003972 return @projects;
3973}
3974
Petr Baudis6b28da62008-09-25 18:48:37 +02003975# print 'sort by' <th> element, generating 'sort by $name' replay link
3976# if that order is not selected
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02003977sub print_sort_th {
Petr Baudis6b28da62008-09-25 18:48:37 +02003978 my ($name, $order, $header) = @_;
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02003979 $header ||= ucfirst($name);
3980
3981 if ($order eq $name) {
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02003982 print "<th>$header</th>\n";
3983 } else {
3984 print "<th>" .
3985 $cgi->a({-href => href(-replay=>1, order=>$name),
3986 -class => "header"}, $header) .
3987 "</th>\n";
3988 }
3989}
3990
Jakub Narebski69913412008-06-10 19:21:01 +02003991sub git_project_list_body {
Petr Baudis42326112008-10-02 17:17:01 +02003992 # actually uses global variable $project
Jakub Narebski69913412008-06-10 19:21:01 +02003993 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3994
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08003995 my $check_forks = gitweb_check_feature('forks');
Jakub Narebski69913412008-06-10 19:21:01 +02003996 my @projects = fill_project_list_info($projlist, $check_forks);
3997
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003998 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02003999 $from = 0 unless defined $from;
4000 $to = $#projects if (!defined $to || $#projects < $to);
4001
Petr Baudis6b28da62008-09-25 18:48:37 +02004002 my %order_info = (
4003 project => { key => 'path', type => 'str' },
4004 descr => { key => 'descr_long', type => 'str' },
4005 owner => { key => 'owner', type => 'str' },
4006 age => { key => 'age', type => 'num' }
4007 );
4008 my $oi = $order_info{$order};
4009 if ($oi->{'type'} eq 'str') {
4010 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4011 } else {
4012 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4013 }
4014
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004015 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02004016 if ($show_ctags) {
4017 my %ctags;
4018 foreach my $p (@projects) {
4019 foreach my $ct (keys %{$p->{'ctags'}}) {
4020 $ctags{$ct} += $p->{'ctags'}->{$ct};
4021 }
4022 }
4023 my $cloud = git_populate_project_tagcloud(\%ctags);
4024 print git_show_project_tagcloud($cloud, 64);
4025 }
4026
Petr Baudise30496d2006-10-24 05:33:17 +02004027 print "<table class=\"project_list\">\n";
4028 unless ($no_header) {
4029 print "<tr>\n";
4030 if ($check_forks) {
4031 print "<th></th>\n";
4032 }
Petr Baudis6b28da62008-09-25 18:48:37 +02004033 print_sort_th('project', $order, 'Project');
4034 print_sort_th('descr', $order, 'Description');
4035 print_sort_th('owner', $order, 'Owner');
4036 print_sort_th('age', $order, 'Last Change');
Jakub Narebski7da0f3a2008-06-10 19:21:44 +02004037 print "<th></th>\n" . # for links
Petr Baudise30496d2006-10-24 05:33:17 +02004038 "</tr>\n";
4039 }
4040 my $alternate = 1;
Petr Baudisaed93de2008-10-02 17:13:02 +02004041 my $tagfilter = $cgi->param('by_tag');
Petr Baudise30496d2006-10-24 05:33:17 +02004042 for (my $i = $from; $i <= $to; $i++) {
4043 my $pr = $projects[$i];
Petr Baudis42326112008-10-02 17:17:01 +02004044
Petr Baudisaed93de2008-10-02 17:13:02 +02004045 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
Petr Baudis0d1d1542008-10-03 09:29:45 +02004046 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4047 and not $pr->{'descr_long'} =~ /$searchtext/;
4048 # Weed out forks or non-matching entries of search
Petr Baudis42326112008-10-02 17:17:01 +02004049 if ($check_forks) {
4050 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4051 $forkbase="^$forkbase" if $forkbase;
Petr Baudis0d1d1542008-10-03 09:29:45 +02004052 next if not $searchtext and not $tagfilter and $show_ctags
4053 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
Petr Baudis42326112008-10-02 17:17:01 +02004054 }
4055
Petr Baudise30496d2006-10-24 05:33:17 +02004056 if ($alternate) {
4057 print "<tr class=\"dark\">\n";
4058 } else {
4059 print "<tr class=\"light\">\n";
4060 }
4061 $alternate ^= 1;
4062 if ($check_forks) {
4063 print "<td>";
4064 if ($pr->{'forks'}) {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08004065 print "<!-- $pr->{'forks'} -->\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004066 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4067 }
4068 print "</td>\n";
4069 }
4070 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4071 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01004072 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4073 -class => "list", -title => $pr->{'descr_long'}},
4074 esc_html($pr->{'descr'})) . "</td>\n" .
David Symondsd3cd2492007-10-23 11:31:23 +10004075 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004076 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
Jakub Narebski785cdea2007-05-13 12:39:22 +02004077 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
Petr Baudise30496d2006-10-24 05:33:17 +02004078 "<td class=\"link\">" .
4079 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
Alexandre Julliardfaa1bbf2006-11-15 21:37:50 +01004080 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
Petr Baudise30496d2006-10-24 05:33:17 +02004081 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4082 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4083 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4084 "</td>\n" .
4085 "</tr>\n";
4086 }
4087 if (defined $extra) {
4088 print "<tr>\n";
4089 if ($check_forks) {
4090 print "<td></td>\n";
4091 }
4092 print "<td colspan=\"5\">$extra</td>\n" .
4093 "</tr>\n";
4094 }
4095 print "</table>\n";
4096}
4097
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004098sub git_shortlog_body {
4099 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004100 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304101
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004102 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004103 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004104
Jakub Narebski591ebf62007-11-19 14:16:11 +01004105 print "<table class=\"shortlog\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004106 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004107 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00004108 my %co = %{$commitlist->[$i]};
4109 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02004110 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004111 if ($alternate) {
4112 print "<tr class=\"dark\">\n";
4113 } else {
4114 print "<tr class=\"light\">\n";
4115 }
4116 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10004117 my $author = chop_and_escape_str($co{'author_name'}, 10);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004118 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4119 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004120 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004121 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004122 print format_subject_html($co{'title'}, $co{'title_short'},
4123 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004124 print "</td>\n" .
4125 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02004126 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02004127 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02004128 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004129 my $snapshot_links = format_snapshot_links($commit);
4130 if (defined $snapshot_links) {
4131 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02004132 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304133 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004134 "</tr>\n";
4135 }
4136 if (defined $extra) {
4137 print "<tr>\n" .
4138 "<td colspan=\"4\">$extra</td>\n" .
4139 "</tr>\n";
4140 }
4141 print "</table>\n";
4142}
4143
Jakub Narebski581860e2006-08-14 02:09:08 +02004144sub git_history_body {
4145 # Warning: assumes constant type (blob or tree) during history
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004146 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02004147
4148 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004149 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02004150
Jakub Narebski591ebf62007-11-19 14:16:11 +01004151 print "<table class=\"history\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004152 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02004153 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004154 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02004155 if (!%co) {
4156 next;
4157 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004158 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02004159
4160 my $ref = format_ref_marker($refs, $commit);
4161
4162 if ($alternate) {
4163 print "<tr class=\"dark\">\n";
4164 } else {
4165 print "<tr class=\"light\">\n";
4166 }
4167 $alternate ^= 1;
David Symondse076a0e2007-10-22 10:28:03 +10004168 # shortlog uses chop_str($co{'author_name'}, 10)
David Symondsce58ec92007-10-23 11:31:22 +10004169 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
Jakub Narebski581860e2006-08-14 02:09:08 +02004170 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004171 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski581860e2006-08-14 02:09:08 +02004172 "<td>";
4173 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02004174 print format_subject_html($co{'title'}, $co{'title_short'},
4175 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02004176 print "</td>\n" .
4177 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07004178 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4179 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02004180
4181 if ($ftype eq 'blob') {
4182 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4183 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4184 if (defined $blob_current && defined $blob_parent &&
4185 $blob_current ne $blob_parent) {
4186 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02004187 $cgi->a({-href => href(action=>"blobdiff",
4188 hash=>$blob_current, hash_parent=>$blob_parent,
4189 hash_base=>$hash_base, hash_parent_base=>$commit,
4190 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02004191 "diff to current");
4192 }
4193 }
4194 print "</td>\n" .
4195 "</tr>\n";
4196 }
4197 if (defined $extra) {
4198 print "<tr>\n" .
4199 "<td colspan=\"4\">$extra</td>\n" .
4200 "</tr>\n";
4201 }
4202 print "</table>\n";
4203}
4204
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004205sub git_tags_body {
4206 # uses global variable $project
4207 my ($taglist, $from, $to, $extra) = @_;
4208 $from = 0 unless defined $from;
4209 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4210
Jakub Narebski591ebf62007-11-19 14:16:11 +01004211 print "<table class=\"tags\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004212 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004213 for (my $i = $from; $i <= $to; $i++) {
4214 my $entry = $taglist->[$i];
4215 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01004216 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004217 my $comment_short;
4218 if (defined $comment) {
4219 $comment_short = chop_str($comment, 30, 5);
4220 }
4221 if ($alternate) {
4222 print "<tr class=\"dark\">\n";
4223 } else {
4224 print "<tr class=\"light\">\n";
4225 }
4226 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01004227 if (defined $tag{'age'}) {
4228 print "<td><i>$tag{'age'}</i></td>\n";
4229 } else {
4230 print "<td></td>\n";
4231 }
4232 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004233 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02004234 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004235 "</td>\n" .
4236 "<td>";
4237 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004238 print format_subject_html($comment, $comment_short,
4239 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004240 }
4241 print "</td>\n" .
4242 "<td class=\"selflink\">";
4243 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004244 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004245 } else {
4246 print "&nbsp;";
4247 }
4248 print "</td>\n" .
4249 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004250 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004251 if ($tag{'reftype'} eq "commit") {
Jakub Narebskibf901f82007-12-15 15:40:28 +01004252 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4253 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004254 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004255 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004256 }
4257 print "</td>\n" .
4258 "</tr>";
4259 }
4260 if (defined $extra) {
4261 print "<tr>\n" .
4262 "<td colspan=\"5\">$extra</td>\n" .
4263 "</tr>\n";
4264 }
4265 print "</table>\n";
4266}
4267
4268sub git_heads_body {
4269 # uses global variable $project
Jakub Narebski120ddde2006-09-19 14:33:22 +02004270 my ($headlist, $head, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004271 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02004272 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004273
Jakub Narebski591ebf62007-11-19 14:16:11 +01004274 print "<table class=\"heads\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004275 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004276 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02004277 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01004278 my %ref = %$entry;
4279 my $curr = $ref{'id'} eq $head;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004280 if ($alternate) {
4281 print "<tr class=\"dark\">\n";
4282 } else {
4283 print "<tr class=\"light\">\n";
4284 }
4285 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01004286 print "<td><i>$ref{'age'}</i></td>\n" .
4287 ($curr ? "<td class=\"current_head\">" : "<td>") .
Jakub Narebskibf901f82007-12-15 15:40:28 +01004288 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
Jakub Narebskicd146402006-11-02 20:23:11 +01004289 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004290 "</td>\n" .
4291 "<td class=\"link\">" .
Jakub Narebskibf901f82007-12-15 15:40:28 +01004292 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4293 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4294 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004295 "</td>\n" .
4296 "</tr>";
4297 }
4298 if (defined $extra) {
4299 print "<tr>\n" .
4300 "<td colspan=\"3\">$extra</td>\n" .
4301 "</tr>\n";
4302 }
4303 print "</table>\n";
4304}
4305
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004306sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004307 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004308 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004309 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004310
Jakub Narebski591ebf62007-11-19 14:16:11 +01004311 print "<table class=\"commit_search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004312 my $alternate = 1;
4313 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004314 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004315 if (!%co) {
4316 next;
4317 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00004318 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004319 if ($alternate) {
4320 print "<tr class=\"dark\">\n";
4321 } else {
4322 print "<tr class=\"light\">\n";
4323 }
4324 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10004325 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004326 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10004327 "<td><i>" . $author . "</i></td>\n" .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004328 "<td>" .
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004329 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4330 -class => "list subject"},
4331 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004332 my $comment = $co{'comment'};
4333 foreach my $line (@$comment) {
Jakub Narebski6dfbb302008-03-02 16:57:14 +01004334 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004335 my ($lead, $match, $trail) = ($1, $2, $3);
Jakub Narebskib8d97d02008-02-25 21:07:57 +01004336 $match = chop_str($match, 70, 5, 'center');
4337 my $contextlen = int((80 - length($match))/2);
4338 $contextlen = 30 if ($contextlen > 30);
4339 $lead = chop_str($lead, $contextlen, 10, 'left');
4340 $trail = chop_str($trail, $contextlen, 10, 'right');
Junio C Hamanobe8b9062008-02-22 17:33:47 +01004341
4342 $lead = esc_html($lead);
4343 $match = esc_html($match);
4344 $trail = esc_html($trail);
4345
4346 print "$lead<span class=\"match\">$match</span>$trail<br />";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004347 }
4348 }
4349 print "</td>\n" .
4350 "<td class=\"link\">" .
4351 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4352 " | " .
Denis Chengf1fe8f52007-11-26 20:42:06 +08004353 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4354 " | " .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00004355 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4356 print "</td>\n" .
4357 "</tr>\n";
4358 }
4359 if (defined $extra) {
4360 print "<tr>\n" .
4361 "<td colspan=\"3\">$extra</td>\n" .
4362 "</tr>\n";
4363 }
4364 print "</table>\n";
4365}
4366
Jakub Narebski717b8312006-07-31 21:22:15 +02004367## ======================================================================
4368## ======================================================================
4369## actions
4370
Jakub Narebski717b8312006-07-31 21:22:15 +02004371sub git_project_list {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02004372 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02004373 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004374 die_error(400, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02004375 }
4376
Jakub Narebski847e01f2006-08-14 02:05:47 +02004377 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02004378 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004379 die_error(404, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02004380 }
Jakub Narebski6326b602006-08-01 02:59:12 +02004381
Jakub Narebski717b8312006-07-31 21:22:15 +02004382 git_header_html();
4383 if (-f $home_text) {
4384 print "<div class=\"index_include\">\n";
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004385 insert_file($home_text);
Jakub Narebski717b8312006-07-31 21:22:15 +02004386 print "</div>\n";
4387 }
Petr Baudis0d1d1542008-10-03 09:29:45 +02004388 print $cgi->startform(-method => "get") .
4389 "<p class=\"projsearch\">Search:\n" .
4390 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4391 "</p>" .
4392 $cgi->end_form() . "\n";
Petr Baudise30496d2006-10-24 05:33:17 +02004393 git_project_list_body(\@list, $order);
4394 git_footer_html();
4395}
4396
4397sub git_forks {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02004398 my $order = $input_params{'order'};
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02004399 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004400 die_error(400, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02004401 }
Petr Baudise30496d2006-10-24 05:33:17 +02004402
4403 my @list = git_get_projects_list($project);
4404 if (!@list) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004405 die_error(404, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02004406 }
Petr Baudise30496d2006-10-24 05:33:17 +02004407
4408 git_header_html();
4409 git_print_page_nav('','');
4410 git_print_header_div('summary', "$project forks");
4411 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02004412 git_footer_html();
4413}
4414
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004415sub git_project_index {
Petr Baudise30496d2006-10-24 05:33:17 +02004416 my @projects = git_get_projects_list($project);
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004417
4418 print $cgi->header(
4419 -type => 'text/plain',
4420 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004421 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004422
4423 foreach my $pr (@projects) {
4424 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02004425 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02004426 }
4427
4428 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4429 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4430 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4431 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4432 $path =~ s/ /\+/g;
4433 $owner =~ s/ /\+/g;
4434
4435 print "$path $owner\n";
4436 }
4437}
4438
Kay Sieversede5e102005-08-07 20:23:12 +02004439sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004440 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00004441 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02004442 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00004443 my $head = $co{'id'};
Kay Sieversede5e102005-08-07 20:23:12 +02004444
Jakub Narebski1e0cf032006-08-14 02:10:06 +02004445 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004446
Jakub Narebskicd146402006-11-02 20:23:11 +01004447 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004448 # These get_*_list functions return one more to allow us to see if
4449 # there are more ...
4450 my @taglist = git_get_tags_list(16);
4451 my @headlist = git_get_heads_list(16);
Petr Baudise30496d2006-10-24 05:33:17 +02004452 my @forklist;
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004453 my $check_forks = gitweb_check_feature('forks');
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08004454
4455 if ($check_forks) {
Petr Baudise30496d2006-10-24 05:33:17 +02004456 @forklist = git_get_projects_list($project);
4457 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02004458
Kay Sieversede5e102005-08-07 20:23:12 +02004459 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004460 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004461
Kay Sievers19806692005-08-07 20:26:27 +02004462 print "<div class=\"title\">&nbsp;</div>\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01004463 print "<table class=\"projects_list\">\n" .
Petr Baudisa4761422008-10-02 16:25:05 +02004464 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4465 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02004466 if (defined $cd{'rfc2822'}) {
Petr Baudisa4761422008-10-02 16:25:05 +02004467 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02004468 }
4469
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02004470 # use per project git URL list in $projectroot/$project/cloneurl
4471 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02004472 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02004473 my @url_list = git_get_project_url_list($project);
4474 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4475 foreach my $git_url (@url_list) {
4476 next unless $git_url;
Petr Baudisa4761422008-10-02 16:25:05 +02004477 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02004478 $url_tag = "";
4479 }
Petr Baudisaed93de2008-10-02 17:13:02 +02004480
4481 # Tag cloud
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004482 my $show_ctags = gitweb_check_feature('ctags');
Petr Baudisaed93de2008-10-02 17:13:02 +02004483 if ($show_ctags) {
4484 my $ctags = git_get_project_ctags($project);
4485 my $cloud = git_populate_project_tagcloud($ctags);
4486 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4487 print "</td>\n<td>" unless %$ctags;
4488 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4489 print "</td>\n<td>" if %$ctags;
4490 print git_show_project_tagcloud($cloud, 48);
4491 print "</td></tr>";
4492 }
4493
Jakub Narebski19a87212006-08-15 23:03:17 +02004494 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004495
Petr Baudis447ef092006-10-24 05:23:46 +02004496 if (-s "$projectroot/$project/README.html") {
Jakub Narebski2dcb5e12008-12-01 19:01:42 +01004497 print "<div class=\"title\">readme</div>\n" .
4498 "<div class=\"readme\">\n";
4499 insert_file("$projectroot/$project/README.html");
4500 print "\n</div>\n"; # class="readme"
Petr Baudis447ef092006-10-24 05:23:46 +02004501 }
4502
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004503 # we need to request one more than 16 (0..15) to check if
4504 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02004505 my @commitlist = $head ? parse_commits($head, 17) : ();
4506 if (@commitlist) {
4507 git_print_header_div('shortlog');
4508 git_shortlog_body(\@commitlist, 0, 15, $refs,
4509 $#commitlist <= 15 ? undef :
4510 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4511 }
Kay Sieversede5e102005-08-07 20:23:12 +02004512
Jakub Narebski120ddde2006-09-19 14:33:22 +02004513 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004514 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02004515 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004516 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004517 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02004518 }
Kay Sievers0db37972005-08-07 20:24:35 +02004519
Jakub Narebski120ddde2006-09-19 14:33:22 +02004520 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004521 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02004522 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00004523 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004524 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02004525 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02004526
Petr Baudise30496d2006-10-24 05:33:17 +02004527 if (@forklist) {
4528 git_print_header_div('forks');
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02004529 git_project_list_body(\@forklist, 'age', 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00004530 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02004531 $cgi->a({-href => href(action=>"forks")}, "..."),
Mike Ralphsonf04f27e2008-09-25 18:48:48 +02004532 'no_header');
Petr Baudise30496d2006-10-24 05:33:17 +02004533 }
4534
Kay Sieversede5e102005-08-07 20:23:12 +02004535 git_footer_html();
4536}
4537
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004538sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004539 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004540 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004541 git_print_page_nav('','', $head,undef,$head);
4542 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02004543
4544 if (! %tag) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004545 die_error(404, "Unknown tag object");
Jakub Narebski198a2a82007-05-12 21:16:34 +02004546 }
4547
Jakub Narebski847e01f2006-08-14 02:05:47 +02004548 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004549 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01004550 "<table class=\"object_header\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02004551 "<tr>\n" .
4552 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004553 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4554 $tag{'object'}) . "</td>\n" .
4555 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4556 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02004557 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004558 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004559 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01004560 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004561 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4562 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4563 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004564 }
4565 print "</table>\n\n" .
4566 "</div>\n";
4567 print "<div class=\"page_body\">";
4568 my $comment = $tag{'comment'};
4569 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08004570 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01004571 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004572 }
4573 print "</div>\n";
4574 git_footer_html();
4575}
4576
Rafael Garcia-Suarez3a5b9192008-06-06 09:53:32 +02004577sub git_blame {
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004578 # permissions
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004579 gitweb_check_feature('blame')
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004580 or die_error(403, "Blame view not allowed");
Lea Wiemann074afaa2008-06-19 22:03:21 +02004581
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004582 # error checking
Lea Wiemann074afaa2008-06-19 22:03:21 +02004583 die_error(400, "No file name given") unless $file_name;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004584 $hash_base ||= git_get_head_hash($project);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004585 die_error(404, "Couldn't find base commit") unless $hash_base;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004586 my %co = parse_commit($hash_base)
Lea Wiemann074afaa2008-06-19 22:03:21 +02004587 or die_error(404, "Commit not found");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004588 my $ftype = "blob";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004589 if (!defined $hash) {
4590 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004591 or die_error(404, "Error looking up file");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004592 } else {
4593 $ftype = git_get_type($hash);
4594 if ($ftype !~ "blob") {
4595 die_error(400, "Object is not a blob");
4596 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004597 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004598
4599 # run git-blame --porcelain
4600 open my $fd, "-|", git_cmd(), "blame", '-p',
4601 $hash_base, '--', $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02004602 or die_error(500, "Open git-blame failed");
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004603
4604 # page header
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004605 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004606 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01004607 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02004608 "blob") .
4609 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01004610 $cgi->a({-href => href(action=>"history", -replay=>1)},
4611 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02004612 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004613 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004614 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004615 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4616 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004617 git_print_page_path($file_name, $ftype, $hash_base);
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004618
4619 # page body
4620 my @rev_color = qw(light2 dark2);
Luben Tuikovcc1bf972006-07-23 13:37:53 -07004621 my $num_colors = scalar(@rev_color);
4622 my $current_color = 0;
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004623 my %metainfo = ();
4624
Jakub Narebski59b9f612006-08-22 23:42:53 +02004625 print <<HTML;
4626<div class="page_body">
4627<table class="blame">
4628<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4629HTML
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004630 LINE:
4631 while (my $line = <$fd>) {
4632 chomp $line;
4633 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
4634 # no <lines in group> for subsequent lines in group of lines
Luben Tuikovd15c55a2006-10-11 00:30:05 -07004635 my ($full_rev, $orig_lineno, $lineno, $group_size) =
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004636 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004637 if (!exists $metainfo{$full_rev}) {
4638 $metainfo{$full_rev} = {};
4639 }
4640 my $meta = $metainfo{$full_rev};
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004641 my $data;
4642 while ($data = <$fd>) {
4643 chomp $data;
4644 last if ($data =~ s/^\t//); # contents of line
4645 if ($data =~ /^(\S+) (.*)$/) {
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004646 $meta->{$1} = $2;
4647 }
4648 }
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004649 my $short_rev = substr($full_rev, 0, 8);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004650 my $author = $meta->{'author'};
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004651 my %date =
4652 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004653 my $date = $date{'iso-tz'};
4654 if ($group_size) {
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004655 $current_color = ($current_color + 1) % $num_colors;
Luben Tuikovcc1bf972006-07-23 13:37:53 -07004656 }
Jakub Narebski4a24bfc2008-12-09 23:46:16 +01004657 print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004658 if ($group_size) {
4659 print "<td class=\"sha1\"";
Luben Tuikov5ad08282006-10-30 12:37:54 -08004660 print " title=\"". esc_html($author) . ", $date\"";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004661 print " rowspan=\"$group_size\"" if ($group_size > 1);
4662 print ">";
4663 print $cgi->a({-href => href(action=>"commit",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004664 hash=>$full_rev,
4665 file_name=>$file_name)},
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004666 esc_html($short_rev));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004667 print "</td>\n";
Luben Tuikov9dc5f8c2006-10-04 00:12:17 -07004668 }
Luben Tuikov244a70e2007-01-04 18:37:45 -08004669 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004670 or die_error(500, "Open git-rev-parse failed");
Luben Tuikov244a70e2007-01-04 18:37:45 -08004671 my $parent_commit = <$dd>;
4672 close $dd;
4673 chomp($parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004674 my $blamed = href(action => 'blame',
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004675 file_name => $meta->{'filename'},
4676 hash_base => $parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004677 print "<td class=\"linenr\">";
4678 print $cgi->a({ -href => "$blamed#l$orig_lineno",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02004679 -class => "linenr" },
4680 esc_html($lineno));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07004681 print "</td>";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004682 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4683 print "</tr>\n";
4684 }
4685 print "</table>\n";
4686 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004687 close $fd
4688 or print "Reading blob failed\n";
Jakub Narebskid2ce10d2008-12-09 23:48:51 +01004689
4690 # page footer
Luben Tuikov1f2857e2006-07-23 13:34:55 -07004691 git_footer_html();
4692}
4693
Kay Sieversede5e102005-08-07 20:23:12 +02004694sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004695 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004696 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004697 git_print_page_nav('','', $head,undef,$head);
4698 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004699
Jakub Narebskicd146402006-11-02 20:23:11 +01004700 my @tagslist = git_get_tags_list();
4701 if (@tagslist) {
4702 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02004703 }
Kay Sieversede5e102005-08-07 20:23:12 +02004704 git_footer_html();
4705}
4706
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02004707sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004708 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004709 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004710 git_print_page_nav('','', $head,undef,$head);
4711 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004712
Jakub Narebskicd146402006-11-02 20:23:11 +01004713 my @headslist = git_get_heads_list();
4714 if (@headslist) {
4715 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02004716 }
Kay Sievers0db37972005-08-07 20:24:35 +02004717 git_footer_html();
4718}
4719
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004720sub git_blob_plain {
Jakub Narebski7f718e82008-06-03 16:47:10 +02004721 my $type = shift;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004722 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004723
Luben Tuikovcff07712006-07-23 13:28:55 -07004724 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004725 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004726 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004727 $hash = git_get_hash_by_path($base, $file_name, "blob")
Lea Wiemann074afaa2008-06-19 22:03:21 +02004728 or die_error(404, "Cannot find file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004729 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004730 die_error(400, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004731 }
Martin Waitz800764c2006-09-16 23:09:02 +02004732 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4733 # blobs defined by non-textual hash id's can be cached
4734 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004735 }
Martin Waitz800764c2006-09-16 23:09:02 +02004736
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004737 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004738 or die_error(500, "Open git-cat-file blob '$hash' failed");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004739
Jakub Narebski7f718e82008-06-03 16:47:10 +02004740 # content-type (can include charset)
4741 $type = blob_contenttype($fd, $file_name, $type);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004742
Jakub Narebski7f718e82008-06-03 16:47:10 +02004743 # "save as" filename, even when no $file_name is given
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004744 my $save_as = "$hash";
4745 if (defined $file_name) {
4746 $save_as = $file_name;
4747 } elsif ($type =~ m/^text\//) {
4748 $save_as .= '.txt';
4749 }
4750
Jakub Narebskif2e73302006-08-26 19:14:25 +02004751 print $cgi->header(
Jakub Narebski7f718e82008-06-03 16:47:10 +02004752 -type => $type,
4753 -expires => $expires,
4754 -content_disposition => 'inline; filename="' . $save_as . '"');
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004755 undef $/;
4756 binmode STDOUT, ':raw';
4757 print <$fd>;
4758 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4759 $/ = "\n";
4760 close $fd;
4761}
4762
Kay Sievers09bd7892005-08-07 20:21:23 +02004763sub git_blob {
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
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004779 my $have_blame = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004780 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004781 or die_error(500, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004782 my $mimetype = blob_mimetype($fd, $file_name);
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004783 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004784 close $fd;
4785 return git_blob_plain($mimetype);
4786 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004787 # we can have blame only for text/* mimetype
4788 $have_blame &&= ($mimetype =~ m!^text/!);
4789
Jakub Narebskif2e73302006-08-26 19:14:25 +02004790 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004791 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02004792 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02004793 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02004794 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004795 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004796 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02004797 "blame") .
4798 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02004799 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004800 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004801 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004802 "history") .
4803 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01004804 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004805 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004806 " | " .
4807 $cgi->a({-href => href(action=>"blob",
4808 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004809 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02004810 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004811 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004812 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4813 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02004814 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004815 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4816 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004817 } else {
4818 print "<div class=\"page_nav\">\n" .
4819 "<br/><br/></div>\n" .
4820 "<div class=\"title\">$hash</div>\n";
4821 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004822 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02004823 print "<div class=\"page_body\">\n";
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004824 if ($mimetype =~ m!^image/!) {
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004825 print qq!<img type="$mimetype"!;
4826 if ($file_name) {
4827 print qq! alt="$file_name" title="$file_name"!;
4828 }
4829 print qq! src="! .
4830 href(action=>"blob_plain", hash=>$hash,
4831 hash_base=>$hash_base, file_name=>$file_name) .
4832 qq!" />\n!;
Jakub Narebskidfa7c7d2007-12-15 15:41:49 +01004833 } else {
4834 my $nr;
4835 while (my $line = <$fd>) {
4836 chomp $line;
4837 $nr++;
4838 $line = untabify($line);
4839 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4840 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4841 }
Kay Sievers161332a2005-08-07 19:49:46 +02004842 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02004843 close $fd
4844 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02004845 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004846 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004847}
4848
4849sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004850 if (!defined $hash_base) {
4851 $hash_base = "HEAD";
4852 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02004853 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02004854 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004855 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4856 } else {
4857 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02004858 }
Kay Sieverse925f382005-08-07 20:23:35 +02004859 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02004860 die_error(404, "No such tree") unless defined($hash);
Kay Sievers232ff552005-11-24 16:56:55 +01004861 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004862 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Lea Wiemann074afaa2008-06-19 22:03:21 +02004863 or die_error(500, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02004864 my @entries = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02004865 close $fd or die_error(404, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01004866 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02004867
Jakub Narebski847e01f2006-08-14 02:05:47 +02004868 my $refs = git_get_references();
4869 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02004870 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02004871 my $basedir = '';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08004872 my $have_blame = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02004873 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02004874 my @views_nav = ();
4875 if (defined $file_name) {
4876 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01004877 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004878 "history"),
4879 $cgi->a({-href => href(action=>"tree",
4880 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004881 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02004882 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004883 my $snapshot_links = format_snapshot_links($hash);
4884 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02004885 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004886 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02004887 }
4888 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02004889 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02004890 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02004891 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02004892 print "<div class=\"page_nav\">\n";
4893 print "<br/><br/></div>\n";
4894 print "<div class=\"title\">$hash</div>\n";
4895 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004896 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02004897 $basedir = $file_name;
4898 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4899 $basedir .= '/';
4900 }
Jakub Narebski2d7a3532008-10-02 16:50:04 +02004901 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004902 }
Kay Sieversfbb592a2005-08-07 20:12:11 +02004903 print "<div class=\"page_body\">\n";
Jakub Narebski591ebf62007-11-19 14:16:11 +01004904 print "<table class=\"tree\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004905 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02004906 # '..' (top directory) link if possible
4907 if (defined $hash_base &&
4908 defined $file_name && $file_name =~ m![^/]+$!) {
4909 if ($alternate) {
4910 print "<tr class=\"dark\">\n";
4911 } else {
4912 print "<tr class=\"light\">\n";
4913 }
4914 $alternate ^= 1;
4915
4916 my $up = $file_name;
4917 $up =~ s!/?[^/]+$!!;
4918 undef $up unless $up;
4919 # based on git_print_tree_entry
4920 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4921 print '<td class="list">';
4922 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4923 file_name=>$up)},
4924 "..");
4925 print "</td>\n";
4926 print "<td class=\"link\"></td>\n";
4927
4928 print "</tr>\n";
4929 }
Kay Sievers161332a2005-08-07 19:49:46 +02004930 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02004931 my %t = parse_ls_tree_line($line, -z => 1);
4932
Kay Sieversbddec012005-08-07 20:25:42 +02004933 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02004934 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004935 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02004936 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004937 }
4938 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02004939
Jakub Narebski300454f2006-10-21 17:53:09 +02004940 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02004941
Kay Sievers42f7eb92005-08-07 20:21:46 +02004942 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02004943 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02004944 print "</table>\n" .
4945 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004946 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004947}
4948
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304949sub git_snapshot {
Giuseppe Bilotta1b2d2972008-10-10 20:42:26 +02004950 my $format = $input_params{'snapshot_format'};
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01004951 if (!@snapshot_fmts) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004952 die_error(403, "Snapshots not allowed");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02004953 }
4954 # default to first supported snapshot format
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01004955 $format ||= $snapshot_fmts[0];
Jakub Narebski3473e7d2007-07-25 01:19:58 +02004956 if ($format !~ m/^[a-z0-9]+$/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004957 die_error(400, "Invalid snapshot format parameter");
Jakub Narebski3473e7d2007-07-25 01:19:58 +02004958 } elsif (!exists($known_snapshot_formats{$format})) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004959 die_error(400, "Unknown snapshot format");
Giuseppe Bilotta5e166842008-11-02 10:21:37 +01004960 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02004961 die_error(403, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304962 }
4963
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304964 if (!defined $hash) {
4965 $hash = git_get_head_hash($project);
4966 }
4967
Mark Levedahl072570e2007-05-20 11:46:46 -04004968 my $name = $project;
Matthias Lederhofer9a7d9412007-06-07 11:27:08 +02004969 $name =~ s,([^/])/*\.git$,$1,;
4970 $name = basename($name);
4971 my $filename = to_utf8($name);
Mark Levedahl072570e2007-05-20 11:46:46 -04004972 $name =~ s/\047/\047\\\047\047/g;
Mark Levedahl072570e2007-05-20 11:46:46 -04004973 my $cmd;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004974 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
Lea Wiemann516381d2008-06-17 23:46:35 +02004975 $cmd = quote_command(
4976 git_cmd(), 'archive',
4977 "--format=$known_snapshot_formats{$format}{'format'}",
4978 "--prefix=$name/", $hash);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004979 if (exists $known_snapshot_formats{$format}{'compressor'}) {
Lea Wiemann516381d2008-06-17 23:46:35 +02004980 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
Mark Levedahl072570e2007-05-20 11:46:46 -04004981 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304982
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004983 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004984 -type => $known_snapshot_formats{$format}{'type'},
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004985 -content_disposition => 'inline; filename="' . "$filename" . '"',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004986 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304987
Mark Levedahl072570e2007-05-20 11:46:46 -04004988 open my $fd, "-|", $cmd
Lea Wiemann074afaa2008-06-19 22:03:21 +02004989 or die_error(500, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304990 binmode STDOUT, ':raw';
4991 print <$fd>;
4992 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4993 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304994}
4995
Kay Sievers09bd7892005-08-07 20:21:23 +02004996sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004997 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004998 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02004999 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02005000 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005001 if (!defined $page) {
5002 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02005003 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005004 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02005005
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005006 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02005007
Lea Wiemann1f684dc2008-05-28 01:25:42 +02005008 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005009
5010 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02005011 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005012
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005013 if (!@commitlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005014 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02005015
Jakub Narebski847e01f2006-08-14 02:05:47 +02005016 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02005017 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02005018 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005019 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5020 for (my $i = 0; $i <= $to; $i++) {
5021 my %co = %{$commitlist[$i]};
Kay Sieversb87d78d2005-08-07 20:21:04 +02005022 next if !%co;
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005023 my $commit = $co{'id'};
5024 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski847e01f2006-08-14 02:05:47 +02005025 my %ad = parse_date($co{'author_epoch'});
5026 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02005027 "<span class=\"age\">$co{'age_string'}</span>" .
5028 esc_html($co{'title'}) . $ref,
5029 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02005030 print "<div class=\"title_text\">\n" .
5031 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005032 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005033 " | " .
5034 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Petr Baudis6ef4cb22006-09-22 03:19:48 +02005035 " | " .
Petr Baudisd7267202006-09-22 16:56:43 -07005036 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
Kay Sieverseb282402005-08-07 20:21:34 +02005037 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02005038 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01005039 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02005040 "</div>\n";
5041
5042 print "<div class=\"log_body\">\n";
Jakub Narebskif2069412006-10-24 13:52:46 +02005043 git_print_log($co{'comment'}, -final_empty_line=> 1);
Kay Sievers09bd7892005-08-07 20:21:23 +02005044 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02005045 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005046 if ($#commitlist >= 100) {
5047 print "<div class=\"page_nav\">\n";
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005048 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons719dad22006-12-24 14:31:45 +00005049 -accesskey => "n", -title => "Alt-n"}, "next");
5050 print "</div>\n";
5051 }
Kay Sievers034df392005-08-07 20:20:07 +02005052 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02005053}
5054
5055sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01005056 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02005057 my %co = parse_commit($hash)
5058 or die_error(404, "Unknown commit object");
Jakub Narebski847e01f2006-08-14 02:05:47 +02005059 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5060 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02005061
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005062 my $parent = $co{'parent'};
5063 my $parents = $co{'parents'}; # listref
5064
5065 # we need to prepare $formats_nav before any parameter munging
5066 my $formats_nav;
5067 if (!defined $parent) {
5068 # --root commitdiff
5069 $formats_nav .= '(initial)';
5070 } elsif (@$parents == 1) {
5071 # single parent commit
5072 $formats_nav .=
5073 '(parent: ' .
5074 $cgi->a({-href => href(action=>"commit",
5075 hash=>$parent)},
5076 esc_html(substr($parent, 0, 7))) .
5077 ')';
5078 } else {
5079 # merge commit
5080 $formats_nav .=
5081 '(merge: ' .
5082 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01005083 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005084 hash=>$_)},
5085 esc_html(substr($_, 0, 7)));
5086 } @$parents ) .
5087 ')';
5088 }
5089
Kay Sieversd8a20ba2005-08-07 20:28:53 +02005090 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07005091 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02005092 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01005093 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02005094 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5095 @diff_opts,
5096 (@$parents <= 1 ? $parent : '-c'),
5097 $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005098 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski208ecb22007-05-07 01:10:08 +02005099 @difftree = map { chomp; $_ } <$fd>;
Lea Wiemann074afaa2008-06-19 22:03:21 +02005100 close $fd or die_error(404, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02005101
5102 # non-textual hash id's can be cached
5103 my $expires;
5104 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5105 $expires = "+1d";
5106 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005107 my $refs = git_get_references();
5108 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05305109
Jakub Narebski594e2122006-07-31 02:21:52 +02005110 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02005111 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02005112 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01005113 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07005114
Kay Sieversb87d78d2005-08-07 20:21:04 +02005115 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005116 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02005117 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005118 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02005119 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02005120 print "<div class=\"title_text\">\n" .
Jakub Narebski591ebf62007-11-19 14:16:11 +01005121 "<table class=\"object_header\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01005122 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02005123 "<tr>" .
5124 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02005125 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005126 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5127 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02005128 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02005129 printf(" (%02d:%02d %s)",
5130 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02005131 }
Kay Sieversbddec012005-08-07 20:25:42 +02005132 print "</td>" .
5133 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01005134 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02005135 print "<tr><td></td><td> $cd{'rfc2822'}" .
5136 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5137 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00005138 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02005139 print "<tr>" .
5140 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00005141 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005142 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5143 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02005144 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005145 "<td class=\"link\">" .
5146 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5147 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02005148 my $snapshot_links = format_snapshot_links($hash);
5149 if (defined $snapshot_links) {
5150 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05305151 }
5152 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02005153 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01005154
Kay Sievers3e029292005-08-07 20:05:15 +02005155 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02005156 print "<tr>" .
5157 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005158 "<td class=\"sha1\">" .
5159 $cgi->a({-href => href(action=>"commit", hash=>$par),
5160 class => "list"}, $par) .
5161 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02005162 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02005163 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005164 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02005165 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02005166 "</td>" .
5167 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02005168 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02005169 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02005170 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02005171
Kay Sieversfbb592a2005-08-07 20:12:11 +02005172 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02005173 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02005174 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005175
Jakub Narebski208ecb22007-05-07 01:10:08 +02005176 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02005177
Kay Sievers12a88f22005-08-07 20:02:47 +02005178 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02005179}
5180
Jakub Narebskica946012006-12-10 13:25:47 +01005181sub git_object {
5182 # object is defined by:
5183 # - hash or hash_base alone
5184 # - hash_base and file_name
5185 my $type;
5186
5187 # - hash or hash_base alone
5188 if ($hash || ($hash_base && !defined $file_name)) {
5189 my $object_id = $hash || $hash_base;
5190
Lea Wiemann516381d2008-06-17 23:46:35 +02005191 open my $fd, "-|", quote_command(
5192 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
Lea Wiemann074afaa2008-06-19 22:03:21 +02005193 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005194 $type = <$fd>;
5195 chomp $type;
5196 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005197 or die_error(404, "Object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005198
5199 # - hash_base and file_name
5200 } elsif ($hash_base && defined $file_name) {
5201 $file_name =~ s,/+$,,;
5202
5203 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
Lea Wiemann074afaa2008-06-19 22:03:21 +02005204 or die_error(404, "Base object does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005205
5206 # here errors should not hapen
5207 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005208 or die_error(500, "Open git-ls-tree failed");
Jakub Narebskica946012006-12-10 13:25:47 +01005209 my $line = <$fd>;
5210 close $fd;
5211
5212 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5213 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005214 die_error(404, "File or directory for given base does not exist");
Jakub Narebskica946012006-12-10 13:25:47 +01005215 }
5216 $type = $2;
5217 $hash = $3;
5218 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005219 die_error(400, "Not enough information to find object");
Jakub Narebskica946012006-12-10 13:25:47 +01005220 }
5221
5222 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5223 hash=>$hash, hash_base=>$hash_base,
5224 file_name=>$file_name),
5225 -status => '302 Found');
5226}
5227
Kay Sievers09bd7892005-08-07 20:21:23 +02005228sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005229 my $format = shift || 'html';
5230
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005231 my $fd;
5232 my @difftree;
5233 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005234 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005235
5236 # preparing $fd and %diffinfo for git_patchset_body
5237 # new style URI
5238 if (defined $hash_base && defined $hash_parent_base) {
5239 if (defined $file_name) {
5240 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005241 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5242 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02005243 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005244 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005245 @difftree = map { chomp; $_ } <$fd>;
5246 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005247 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005248 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02005249 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005250
Jakub Narebski0aea3372006-08-27 23:45:26 +02005251 } elsif (defined $hash &&
5252 $hash =~ /[0-9a-fA-F]{40}/) {
5253 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005254
5255 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005256 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5257 $hash_parent_base, $hash_base, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005258 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005259 @difftree =
5260 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5261 # $hash == to_id
5262 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5263 map { chomp; $_ } <$fd>;
5264 close $fd
Lea Wiemann074afaa2008-06-19 22:03:21 +02005265 or die_error(404, "Reading git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005266 @difftree
Lea Wiemann074afaa2008-06-19 22:03:21 +02005267 or die_error(404, "Blob diff not found");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005268
5269 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005270 die_error(400, "Missing one of the blob diff parameters");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005271 }
5272
5273 if (@difftree > 1) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005274 die_error(400, "Ambiguous blob diff specification");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005275 }
5276
5277 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01005278 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5279 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005280
5281 $hash_parent ||= $diffinfo{'from_id'};
5282 $hash ||= $diffinfo{'to_id'};
5283
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005284 # non-textual hash id's can be cached
5285 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5286 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5287 $expires = '+1d';
5288 }
5289
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005290 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005291 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02005292 '-p', ($format eq 'html' ? "--full-index" : ()),
5293 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02005294 "--", (defined $file_parent ? $file_parent : ()), $file_name
Lea Wiemann074afaa2008-06-19 22:03:21 +02005295 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005296 }
5297
5298 # old/legacy style URI
5299 if (!%diffinfo && # if new style URI failed
5300 defined $hash && defined $hash_parent) {
5301 # fake git-diff-tree raw output
5302 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
5303 $diffinfo{'from_id'} = $hash_parent;
5304 $diffinfo{'to_id'} = $hash;
5305 if (defined $file_name) {
5306 if (defined $file_parent) {
5307 $diffinfo{'status'} = '2';
Petr Baudis83915482006-09-24 14:57:40 -07005308 $diffinfo{'from_file'} = $file_parent;
5309 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005310 } else { # assume not renamed
5311 $diffinfo{'status'} = '1';
Petr Baudis83915482006-09-24 14:57:40 -07005312 $diffinfo{'from_file'} = $file_name;
5313 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005314 }
5315 } else { # no filename given
5316 $diffinfo{'status'} = '2';
5317 $diffinfo{'from_file'} = $hash_parent;
5318 $diffinfo{'to_file'} = $hash;
5319 }
5320
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005321 # non-textual hash id's can be cached
5322 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
5323 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5324 $expires = '+1d';
5325 }
5326
5327 # open patch output
Jakub Narebski957d6ea2007-04-05 13:45:41 +02005328 open $fd, "-|", git_cmd(), "diff", @diff_opts,
5329 '-p', ($format eq 'html' ? "--full-index" : ()),
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005330 $hash_parent, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005331 or die_error(500, "Open git-diff failed");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005332 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005333 die_error(400, "Missing one of the blob diff parameters")
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005334 unless %diffinfo;
5335 }
5336
5337 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005338 if ($format eq 'html') {
5339 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01005340 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02005341 "raw");
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005342 git_header_html(undef, $expires);
5343 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5344 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5345 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5346 } else {
5347 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5348 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5349 }
5350 if (defined $file_name) {
5351 git_print_page_path($file_name, "blob", $hash_base);
5352 } else {
5353 print "<div class=\"page_path\"></div>\n";
5354 }
5355
5356 } elsif ($format eq 'plain') {
5357 print $cgi->header(
5358 -type => 'text/plain',
5359 -charset => 'utf-8',
5360 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07005361 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005362
5363 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5364
Kay Sievers09bd7892005-08-07 20:21:23 +02005365 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005366 die_error(400, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005367 }
5368
5369 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005370 if ($format eq 'html') {
5371 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005372
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005373 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5374 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02005375
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005376 print "</div>\n"; # class="page_body"
5377 git_footer_html();
5378
5379 } else {
5380 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01005381 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5382 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005383
5384 print $line;
5385
5386 last if $line =~ m!^\+\+\+!;
5387 }
5388 local $/ = undef;
5389 print <$fd>;
5390 close $fd;
5391 }
Kay Sievers09bd7892005-08-07 20:21:23 +02005392}
5393
Kay Sievers19806692005-08-07 20:26:27 +02005394sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02005395 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02005396}
5397
Kay Sievers09bd7892005-08-07 20:21:23 +02005398sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02005399 my $format = shift || 'html';
Jakub Narebski9954f772006-11-18 23:35:41 +01005400 $hash ||= $hash_base || "HEAD";
Lea Wiemann074afaa2008-06-19 22:03:21 +02005401 my %co = parse_commit($hash)
5402 or die_error(404, "Unknown commit object");
Jakub Narebski151602d2006-10-23 00:37:56 +02005403
Jakub Narebskicd030c32007-06-08 13:33:28 +02005404 # choose format for commitdiff for merge
5405 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5406 $hash_parent = '--cc';
5407 }
5408 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02005409 my $formats_nav;
5410 if ($format eq 'html') {
5411 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01005412 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02005413 "raw");
5414
Jakub Narebskicd030c32007-06-08 13:33:28 +02005415 if (defined $hash_parent &&
5416 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02005417 # commitdiff with two commits given
5418 my $hash_parent_short = $hash_parent;
5419 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5420 $hash_parent_short = substr($hash_parent, 0, 7);
5421 }
5422 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02005423 ' (from';
5424 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5425 if ($co{'parents'}[$i] eq $hash_parent) {
5426 $formats_nav .= ' parent ' . ($i+1);
5427 last;
5428 }
5429 }
5430 $formats_nav .= ': ' .
Jakub Narebski151602d2006-10-23 00:37:56 +02005431 $cgi->a({-href => href(action=>"commitdiff",
5432 hash=>$hash_parent)},
5433 esc_html($hash_parent_short)) .
5434 ')';
5435 } elsif (!$co{'parent'}) {
5436 # --root commitdiff
5437 $formats_nav .= ' (initial)';
5438 } elsif (scalar @{$co{'parents'}} == 1) {
5439 # single parent commit
5440 $formats_nav .=
5441 ' (parent: ' .
5442 $cgi->a({-href => href(action=>"commitdiff",
5443 hash=>$co{'parent'})},
5444 esc_html(substr($co{'parent'}, 0, 7))) .
5445 ')';
5446 } else {
5447 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02005448 if ($hash_parent eq '--cc') {
5449 $formats_nav .= ' | ' .
5450 $cgi->a({-href => href(action=>"commitdiff",
5451 hash=>$hash, hash_parent=>'-c')},
5452 'combined');
5453 } else { # $hash_parent eq '-c'
5454 $formats_nav .= ' | ' .
5455 $cgi->a({-href => href(action=>"commitdiff",
5456 hash=>$hash, hash_parent=>'--cc')},
5457 'compact');
5458 }
Jakub Narebski151602d2006-10-23 00:37:56 +02005459 $formats_nav .=
5460 ' (merge: ' .
5461 join(' ', map {
5462 $cgi->a({-href => href(action=>"commitdiff",
5463 hash=>$_)},
5464 esc_html(substr($_, 0, 7)));
5465 } @{$co{'parents'}} ) .
5466 ')';
5467 }
5468 }
5469
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005470 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02005471 if (!defined $hash_parent_param) {
5472 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005473 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02005474 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02005475 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005476
5477 # read commitdiff
5478 my $fd;
5479 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02005480 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005481 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01005482 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005483 $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005484 or die_error(500, "Open git-diff-tree failed");
Jakub Narebskieee08902006-08-24 00:15:14 +02005485
Jakub Narebski04408c32006-11-18 23:35:38 +01005486 while (my $line = <$fd>) {
5487 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02005488 # empty line ends raw part of diff-tree output
5489 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02005490 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02005491 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005492
Jakub Narebskieee08902006-08-24 00:15:14 +02005493 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005494 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02005495 '-p', $hash_parent_param, $hash, "--"
Lea Wiemann074afaa2008-06-19 22:03:21 +02005496 or die_error(500, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02005497
Jakub Narebskieee08902006-08-24 00:15:14 +02005498 } else {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005499 die_error(400, "Unknown commitdiff format");
Jakub Narebskieee08902006-08-24 00:15:14 +02005500 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02005501
Kay Sievers11044292005-10-19 03:18:45 +02005502 # non-textual hash id's can be cached
5503 my $expires;
5504 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5505 $expires = "+1d";
5506 }
Kay Sievers09bd7892005-08-07 20:21:23 +02005507
Jakub Narebskieee08902006-08-24 00:15:14 +02005508 # write commit message
5509 if ($format eq 'html') {
5510 my $refs = git_get_references();
5511 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02005512
Jakub Narebskieee08902006-08-24 00:15:14 +02005513 git_header_html(undef, $expires);
5514 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5515 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02005516 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02005517 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02005518 if (@{$co{'comment'}} > 1) {
5519 print "<div class=\"log\">\n";
5520 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5521 print "</div>\n"; # class="log"
5522 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02005523
Jakub Narebskieee08902006-08-24 00:15:14 +02005524 } elsif ($format eq 'plain') {
5525 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005526 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02005527 my $filename = basename($project) . "-$hash.patch";
5528
5529 print $cgi->header(
5530 -type => 'text/plain',
5531 -charset => 'utf-8',
5532 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07005533 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02005534 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
Yasushi SHOJI77202242008-01-29 21:16:02 +09005535 print "From: " . to_utf8($co{'author'}) . "\n";
5536 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5537 print "Subject: " . to_utf8($co{'title'}) . "\n";
5538
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005539 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02005540 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02005541
Jakub Narebskieee08902006-08-24 00:15:14 +02005542 foreach my $line (@{$co{'comment'}}) {
Yasushi SHOJI77202242008-01-29 21:16:02 +09005543 print to_utf8($line) . "\n";
Kay Sievers19806692005-08-07 20:26:27 +02005544 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005545 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02005546 }
Jakub Narebskieee08902006-08-24 00:15:14 +02005547
5548 # write patch
5549 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02005550 my $use_parents = !defined $hash_parent ||
5551 $hash_parent eq '-c' || $hash_parent eq '--cc';
5552 git_difftree_body(\@difftree, $hash,
5553 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02005554 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02005555
Jakub Narebskicd030c32007-06-08 13:33:28 +02005556 git_patchset_body($fd, \@difftree, $hash,
5557 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02005558 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02005559 print "</div>\n"; # class="page_body"
5560 git_footer_html();
5561
5562 } elsif ($format eq 'plain') {
5563 local $/ = undef;
5564 print <$fd>;
5565 close $fd
5566 or print "Reading git-diff-tree failed\n";
5567 }
5568}
5569
5570sub git_commitdiff_plain {
5571 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02005572}
5573
Kay Sievers09bd7892005-08-07 20:21:23 +02005574sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07005575 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005576 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02005577 }
Jakub Narebski8be68352006-09-11 00:36:04 +02005578 if (!defined $page) {
5579 $page = 0;
5580 }
Luben Tuikov63433102006-07-23 13:31:15 -07005581 my $ftype;
Lea Wiemann074afaa2008-06-19 22:03:21 +02005582 my %co = parse_commit($hash_base)
5583 or die_error(404, "Unknown commit object");
Jakub Narebski8be68352006-09-11 00:36:04 +02005584
Jakub Narebski847e01f2006-08-14 02:05:47 +02005585 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02005586 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5587
Jakub Narebski5634cf22008-04-13 14:12:15 +02005588 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
Lea Wiemann074afaa2008-06-19 22:03:21 +02005589 $file_name, "--full-history")
5590 or die_error(404, "No such file or directory on given branch");
Jakub Narebski5634cf22008-04-13 14:12:15 +02005591
Luben Tuikov93d5f062006-07-23 13:30:08 -07005592 if (!defined $hash && defined $file_name) {
Jakub Narebski5634cf22008-04-13 14:12:15 +02005593 # some commits could have deleted file in question,
5594 # and not have it in tree, but one of them has to have it
5595 for (my $i = 0; $i <= @commitlist; $i++) {
5596 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5597 last if defined $hash;
5598 }
Luben Tuikov93d5f062006-07-23 13:30:08 -07005599 }
Luben Tuikovcff07712006-07-23 13:28:55 -07005600 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07005601 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07005602 }
Jakub Narebski5634cf22008-04-13 14:12:15 +02005603 if (!defined $ftype) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005604 die_error(500, "Unknown type of object");
Jakub Narebski5634cf22008-04-13 14:12:15 +02005605 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005606
Jakub Narebski8be68352006-09-11 00:36:04 +02005607 my $paging_nav = '';
5608 if ($page > 0) {
5609 $paging_nav .=
5610 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5611 file_name=>$file_name)},
5612 "first");
5613 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005614 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski8be68352006-09-11 00:36:04 +02005615 -accesskey => "p", -title => "Alt-p"}, "prev");
5616 } else {
5617 $paging_nav .= "first";
5618 $paging_nav .= " &sdot; prev";
5619 }
Jakub Narebski8be68352006-09-11 00:36:04 +02005620 my $next_link = '';
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005621 if ($#commitlist >= 100) {
Jakub Narebski8be68352006-09-11 00:36:04 +02005622 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005623 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005624 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005625 $paging_nav .= " &sdot; $next_link";
5626 } else {
5627 $paging_nav .= " &sdot; next";
Jakub Narebski8be68352006-09-11 00:36:04 +02005628 }
Jakub Narebski581860e2006-08-14 02:09:08 +02005629
Jakub Narebski8be68352006-09-11 00:36:04 +02005630 git_header_html();
5631 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5632 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5633 git_print_page_path($file_name, $ftype, $hash_base);
5634
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005635 git_history_body(\@commitlist, 0, 99,
Jakub Narebski8be68352006-09-11 00:36:04 +02005636 $refs, $hash_base, $ftype, $next_link);
5637
Kay Sieversd51e9022005-08-07 20:16:07 +02005638 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02005639}
Kay Sievers19806692005-08-07 20:26:27 +02005640
5641sub git_search {
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005642 gitweb_check_feature('search') or die_error(403, "Search is disabled");
Kay Sievers19806692005-08-07 20:26:27 +02005643 if (!defined $searchtext) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005644 die_error(400, "Text field is empty");
Kay Sievers19806692005-08-07 20:26:27 +02005645 }
5646 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005647 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005648 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005649 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02005650 if (!%co) {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005651 die_error(404, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02005652 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005653 if (!defined $page) {
5654 $page = 0;
5655 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02005656
Petr Baudis88ad7292006-10-24 05:15:46 +02005657 $searchtype ||= 'commit';
5658 if ($searchtype eq 'pickaxe') {
Jakub Narebski04f7a942006-09-11 00:29:27 +02005659 # pickaxe may take all resources of your box and run for several minutes
5660 # with every query - so decide by yourself how public you make this feature
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005661 gitweb_check_feature('pickaxe')
Lea Wiemann074afaa2008-06-19 22:03:21 +02005662 or die_error(403, "Pickaxe is disabled");
Kay Sieversc994d622005-08-07 20:27:18 +02005663 }
Petr Baudise7738552007-05-17 04:31:12 +02005664 if ($searchtype eq 'grep') {
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005665 gitweb_check_feature('grep')
Lea Wiemann074afaa2008-06-19 22:03:21 +02005666 or die_error(403, "Grep is disabled");
Petr Baudise7738552007-05-17 04:31:12 +02005667 }
Petr Baudis88ad7292006-10-24 05:15:46 +02005668
Kay Sievers19806692005-08-07 20:26:27 +02005669 git_header_html();
Kay Sievers19806692005-08-07 20:26:27 +02005670
Petr Baudis88ad7292006-10-24 05:15:46 +02005671 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
Robert Fitzsimons8e574fb2006-12-23 03:35:14 +00005672 my $greptype;
5673 if ($searchtype eq 'commit') {
5674 $greptype = "--grep=";
5675 } elsif ($searchtype eq 'author') {
5676 $greptype = "--author=";
5677 } elsif ($searchtype eq 'committer') {
5678 $greptype = "--committer=";
5679 }
Jakub Narebski0270cd02008-02-26 13:22:07 +01005680 $greptype .= $searchtext;
5681 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
Petr Baudis0e559912008-02-26 13:22:08 +01005682 $greptype, '--regexp-ignore-case',
5683 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005684
5685 my $paging_nav = '';
5686 if ($page > 0) {
5687 $paging_nav .=
5688 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebski0270cd02008-02-26 13:22:07 +01005689 searchtext=>$searchtext,
5690 searchtype=>$searchtype)},
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005691 "first");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005692 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005693 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005694 -accesskey => "p", -title => "Alt-p"}, "prev");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005695 } else {
5696 $paging_nav .= "first";
5697 $paging_nav .= " &sdot; prev";
5698 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005699 my $next_link = '';
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005700 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005701 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005702 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005703 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005704 $paging_nav .= " &sdot; $next_link";
5705 } else {
5706 $paging_nav .= " &sdot; next";
5707 }
5708
5709 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005710 }
5711
5712 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5713 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005714 git_search_grep_body(\@commitlist, 0, 99, $next_link);
Kay Sieversc994d622005-08-07 20:27:18 +02005715 }
5716
Petr Baudis88ad7292006-10-24 05:15:46 +02005717 if ($searchtype eq 'pickaxe') {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005718 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5719 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5720
Jakub Narebski591ebf62007-11-19 14:16:11 +01005721 print "<table class=\"pickaxe search\">\n";
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005722 my $alternate = 1;
Kay Sieversc994d622005-08-07 20:27:18 +02005723 $/ = "\n";
Jakub Narebskic582aba2008-03-05 09:31:55 +01005724 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5725 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5726 ($search_use_regexp ? '--pickaxe-regex' : ());
Kay Sieversc994d622005-08-07 20:27:18 +02005727 undef %co;
5728 my @files;
5729 while (my $line = <$fd>) {
Jakub Narebskic582aba2008-03-05 09:31:55 +01005730 chomp $line;
5731 next unless $line;
5732
5733 my %set = parse_difftree_raw_line($line);
5734 if (defined $set{'commit'}) {
5735 # finish previous commit
Kay Sieversc994d622005-08-07 20:27:18 +02005736 if (%co) {
Kay Sieversc994d622005-08-07 20:27:18 +02005737 print "</td>\n" .
5738 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02005739 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005740 " | " .
5741 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02005742 print "</td>\n" .
5743 "</tr>\n";
5744 }
Jakub Narebskic582aba2008-03-05 09:31:55 +01005745
5746 if ($alternate) {
5747 print "<tr class=\"dark\">\n";
5748 } else {
5749 print "<tr class=\"light\">\n";
5750 }
5751 $alternate ^= 1;
5752 %co = parse_commit($set{'commit'});
5753 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5754 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5755 "<td><i>$author</i></td>\n" .
5756 "<td>" .
5757 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5758 -class => "list subject"},
5759 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5760 } elsif (defined $set{'to_id'}) {
5761 next if ($set{'to_id'} =~ m/^0{40}$/);
5762
5763 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5764 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5765 -class => "list"},
5766 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5767 "<br/>\n";
Kay Sieversc994d622005-08-07 20:27:18 +02005768 }
5769 }
5770 close $fd;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005771
Jakub Narebskic582aba2008-03-05 09:31:55 +01005772 # finish last commit (warning: repetition!)
5773 if (%co) {
5774 print "</td>\n" .
5775 "<td class=\"link\">" .
5776 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5777 " | " .
5778 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5779 print "</td>\n" .
5780 "</tr>\n";
5781 }
5782
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005783 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02005784 }
Petr Baudise7738552007-05-17 04:31:12 +02005785
5786 if ($searchtype eq 'grep') {
5787 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5788 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5789
Jakub Narebski591ebf62007-11-19 14:16:11 +01005790 print "<table class=\"grep_search\">\n";
Petr Baudise7738552007-05-17 04:31:12 +02005791 my $alternate = 1;
5792 my $matches = 0;
5793 $/ = "\n";
Petr Baudis0e559912008-02-26 13:22:08 +01005794 open my $fd, "-|", git_cmd(), 'grep', '-n',
5795 $search_use_regexp ? ('-E', '-i') : '-F',
5796 $searchtext, $co{'tree'};
Petr Baudise7738552007-05-17 04:31:12 +02005797 my $lastfile = '';
5798 while (my $line = <$fd>) {
5799 chomp $line;
5800 my ($file, $lno, $ltext, $binary);
5801 last if ($matches++ > 1000);
5802 if ($line =~ /^Binary file (.+) matches$/) {
5803 $file = $1;
5804 $binary = 1;
5805 } else {
5806 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5807 }
5808 if ($file ne $lastfile) {
5809 $lastfile and print "</td></tr>\n";
5810 if ($alternate++) {
5811 print "<tr class=\"dark\">\n";
5812 } else {
5813 print "<tr class=\"light\">\n";
5814 }
5815 print "<td class=\"list\">".
5816 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5817 file_name=>"$file"),
5818 -class => "list"}, esc_path($file));
5819 print "</td><td>\n";
5820 $lastfile = $file;
5821 }
5822 if ($binary) {
5823 print "<div class=\"binary\">Binary file</div>\n";
5824 } else {
5825 $ltext = untabify($ltext);
Petr Baudis0e559912008-02-26 13:22:08 +01005826 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
Petr Baudise7738552007-05-17 04:31:12 +02005827 $ltext = esc_html($1, -nbsp=>1);
5828 $ltext .= '<span class="match">';
5829 $ltext .= esc_html($2, -nbsp=>1);
5830 $ltext .= '</span>';
5831 $ltext .= esc_html($3, -nbsp=>1);
5832 } else {
5833 $ltext = esc_html($ltext, -nbsp=>1);
5834 }
5835 print "<div class=\"pre\">" .
5836 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5837 file_name=>"$file").'#l'.$lno,
5838 -class => "linenr"}, sprintf('%4i', $lno))
5839 . ' ' . $ltext . "</div>\n";
5840 }
5841 }
5842 if ($lastfile) {
5843 print "</td></tr>\n";
5844 if ($matches > 1000) {
5845 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5846 }
5847 } else {
5848 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5849 }
5850 close $fd;
5851
5852 print "</table>\n";
5853 }
Kay Sievers19806692005-08-07 20:26:27 +02005854 git_footer_html();
5855}
5856
Petr Baudis88ad7292006-10-24 05:15:46 +02005857sub git_search_help {
5858 git_header_html();
5859 git_print_page_nav('','', $hash,$hash,$hash);
5860 print <<EOT;
Petr Baudis0e559912008-02-26 13:22:08 +01005861<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5862regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5863the pattern entered is recognized as the POSIX extended
5864<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5865insensitive).</p>
Petr Baudis88ad7292006-10-24 05:15:46 +02005866<dl>
5867<dt><b>commit</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005868<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005869EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005870 my $have_grep = gitweb_check_feature('grep');
Petr Baudise7738552007-05-17 04:31:12 +02005871 if ($have_grep) {
5872 print <<EOT;
5873<dt><b>grep</b></dt>
5874<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
Petr Baudis0e559912008-02-26 13:22:08 +01005875 a different one) are searched for the given pattern. On large trees, this search can take
5876a while and put some strain on the server, so please use it with some consideration. Note that
5877due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5878case-sensitive.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005879EOT
5880 }
5881 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02005882<dt><b>author</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005883<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 +02005884<dt><b>committer</b></dt>
Petr Baudis0e559912008-02-26 13:22:08 +01005885<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 +02005886EOT
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005887 my $have_pickaxe = gitweb_check_feature('pickaxe');
Petr Baudis88ad7292006-10-24 05:15:46 +02005888 if ($have_pickaxe) {
5889 print <<EOT;
5890<dt><b>pickaxe</b></dt>
5891<dd>All commits that caused the string to appear or disappear from any file (changes that
5892added, removed or "modified" the string) will be listed. This search can take a while and
Petr Baudis0e559912008-02-26 13:22:08 +01005893takes a lot of strain on the server, so please use it wisely. Note that since you may be
5894interested even in changes just changing the case as well, this search is case sensitive.</dd>
Petr Baudis88ad7292006-10-24 05:15:46 +02005895EOT
5896 }
5897 print "</dl>\n";
5898 git_footer_html();
5899}
5900
Kay Sievers19806692005-08-07 20:26:27 +02005901sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005902 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005903 if (!defined $hash) {
5904 $hash = $head;
5905 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005906 if (!defined $page) {
5907 $page = 0;
5908 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005909 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02005910
Giuseppe Bilottaec3e97b2008-08-08 16:12:11 +02005911 my $commit_hash = $hash;
5912 if (defined $hash_parent) {
5913 $commit_hash = "$hash_parent..$hash";
5914 }
5915 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02005916
Lea Wiemann1f684dc2008-05-28 01:25:42 +02005917 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005918 my $next_link = '';
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005919 if ($#commitlist >= 100) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005920 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005921 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005922 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005923 }
5924
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005925 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02005926 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5927 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005928
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005929 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005930
Kay Sievers19806692005-08-07 20:26:27 +02005931 git_footer_html();
5932}
Jakub Narebski717b8312006-07-31 21:22:15 +02005933
5934## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005935## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02005936
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005937sub git_feed {
5938 my $format = shift || 'atom';
Giuseppe Bilotta25b27902008-11-29 13:07:29 -08005939 my $have_blame = gitweb_check_feature('blame');
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005940
5941 # Atom: http://www.atomenabled.org/developers/syndication/
5942 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5943 if ($format ne 'rss' && $format ne 'atom') {
Lea Wiemann074afaa2008-06-19 22:03:21 +02005944 die_error(400, "Unknown web feed format");
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005945 }
5946
5947 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5948 my $head = $hash || 'HEAD';
Jakub Narebski311e5522008-02-26 13:22:06 +01005949 my @commitlist = parse_commits($head, 150, 0, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005950
5951 my %latest_commit;
5952 my %latest_date;
5953 my $content_type = "application/$format+xml";
5954 if (defined $cgi->http('HTTP_ACCEPT') &&
5955 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5956 # browser (feed reader) prefers text/xml
5957 $content_type = 'text/xml';
5958 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005959 if (defined($commitlist[0])) {
5960 %latest_commit = %{$commitlist[0]};
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005961 %latest_date = parse_date($latest_commit{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005962 print $cgi->header(
5963 -type => $content_type,
5964 -charset => 'utf-8',
5965 -last_modified => $latest_date{'rfc2822'});
5966 } else {
5967 print $cgi->header(
5968 -type => $content_type,
5969 -charset => 'utf-8');
5970 }
5971
5972 # Optimization: skip generating the body if client asks only
5973 # for Last-Modified date.
5974 return if ($cgi->request_method() eq 'HEAD');
5975
5976 # header variables
5977 my $title = "$site_name - $project/$action";
5978 my $feed_type = 'log';
5979 if (defined $hash) {
5980 $title .= " - '$hash'";
5981 $feed_type = 'branch log';
5982 if (defined $file_name) {
5983 $title .= " :: $file_name";
5984 $feed_type = 'history';
5985 }
5986 } elsif (defined $file_name) {
5987 $title .= " - $file_name";
5988 $feed_type = 'history';
5989 }
5990 $title .= " $feed_type";
5991 my $descr = git_get_project_description($project);
5992 if (defined $descr) {
5993 $descr = esc_html($descr);
5994 } else {
5995 $descr = "$project " .
5996 ($format eq 'rss' ? 'RSS' : 'Atom') .
5997 " feed";
5998 }
5999 my $owner = git_get_project_owner($project);
6000 $owner = esc_html($owner);
6001
6002 #header
6003 my $alt_url;
6004 if (defined $file_name) {
6005 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6006 } elsif (defined $hash) {
6007 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6008 } else {
6009 $alt_url = href(-full=>1, action=>"summary");
6010 }
6011 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6012 if ($format eq 'rss') {
6013 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02006014<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6015<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02006016XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006017 print "<title>$title</title>\n" .
6018 "<link>$alt_url</link>\n" .
6019 "<description>$descr</description>\n" .
6020 "<language>en</language>\n";
6021 } elsif ($format eq 'atom') {
6022 print <<XML;
6023<feed xmlns="http://www.w3.org/2005/Atom">
6024XML
6025 print "<title>$title</title>\n" .
6026 "<subtitle>$descr</subtitle>\n" .
6027 '<link rel="alternate" type="text/html" href="' .
6028 $alt_url . '" />' . "\n" .
6029 '<link rel="self" type="' . $content_type . '" href="' .
6030 $cgi->self_url() . '" />' . "\n" .
6031 "<id>" . href(-full=>1) . "</id>\n" .
6032 # use project owner for feed author
6033 "<author><name>$owner</name></author>\n";
6034 if (defined $favicon) {
6035 print "<icon>" . esc_url($favicon) . "</icon>\n";
6036 }
6037 if (defined $logo_url) {
6038 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01006039 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006040 }
6041 if (! %latest_date) {
6042 # dummy date to keep the feed valid until commits trickle in:
6043 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6044 } else {
6045 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6046 }
6047 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006048
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006049 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00006050 for (my $i = 0; $i <= $#commitlist; $i++) {
6051 my %co = %{$commitlist[$i]};
6052 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02006053 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01006054 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02006055 last;
6056 }
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01006057 my %cd = parse_date($co{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006058
6059 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00006060 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02006061 $co{'parent'} || "--root",
6062 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02006063 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02006064 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02006065 close $fd
6066 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006067
6068 # print element (entry, item)
Florian La Rochee62a6412008-02-03 12:38:46 +01006069 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006070 if ($format eq 'rss') {
6071 print "<item>\n" .
6072 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6073 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6074 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6075 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6076 "<link>$co_url</link>\n" .
6077 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6078 "<content:encoded>" .
6079 "<![CDATA[\n";
6080 } elsif ($format eq 'atom') {
6081 print "<entry>\n" .
6082 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6083 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01006084 "<author>\n" .
6085 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6086 if ($co{'author_email'}) {
6087 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6088 }
6089 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006090 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01006091 "<contributor>\n" .
6092 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6093 if ($co{'committer_email'}) {
6094 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6095 }
6096 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006097 "<published>$cd{'iso-8601'}</published>\n" .
6098 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6099 "<id>$co_url</id>\n" .
6100 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6101 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6102 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006103 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006104 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006105 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006106 $line = esc_html($line);
6107 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006108 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006109 print "</pre><ul>\n";
6110 foreach my $difftree_line (@difftree) {
6111 my %difftree = parse_difftree_raw_line($difftree_line);
6112 next if !$difftree{'from_id'};
6113
6114 my $file = $difftree{'file'} || $difftree{'to_file'};
6115
6116 print "<li>" .
6117 "[" .
6118 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6119 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6120 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6121 file_name=>$file, file_parent=>$difftree{'from_file'}),
6122 -title => "diff"}, 'D');
6123 if ($have_blame) {
6124 print $cgi->a({-href => href(-full=>1, action=>"blame",
6125 file_name=>$file, hash_base=>$commit),
6126 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02006127 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006128 # if this is not a feed of a file history
6129 if (!defined $file_name || $file_name ne $file) {
6130 print $cgi->a({-href => href(-full=>1, action=>"history",
6131 file_name=>$file, hash=>$commit),
6132 -title => "history"}, 'H');
6133 }
6134 $file = esc_path($file);
6135 print "] ".
6136 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02006137 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006138 if ($format eq 'rss') {
6139 print "</ul>]]>\n" .
6140 "</content:encoded>\n" .
6141 "</item>\n";
6142 } elsif ($format eq 'atom') {
6143 print "</ul>\n</div>\n" .
6144 "</content>\n" .
6145 "</entry>\n";
6146 }
Jakub Narebski717b8312006-07-31 21:22:15 +02006147 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01006148
6149 # end of feed
6150 if ($format eq 'rss') {
6151 print "</channel>\n</rss>\n";
6152 } elsif ($format eq 'atom') {
6153 print "</feed>\n";
6154 }
6155}
6156
6157sub git_rss {
6158 git_feed('rss');
6159}
6160
6161sub git_atom {
6162 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02006163}
6164
6165sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02006166 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02006167
6168 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02006169 print <<XML;
6170<?xml version="1.0" encoding="utf-8"?>
6171<opml version="1.0">
6172<head>
Petr Baudis8be28902006-10-24 05:18:39 +02006173 <title>$site_name OPML Export</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02006174</head>
6175<body>
6176<outline text="git RSS feeds">
6177XML
Jakub Narebski717b8312006-07-31 21:22:15 +02006178
6179 foreach my $pr (@list) {
6180 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02006181 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02006182 if (!defined $head) {
6183 next;
6184 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02006185 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02006186 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02006187 if (!%co) {
6188 next;
6189 }
6190
6191 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6192 my $rss = "$my_url?p=$proj{'path'};a=rss";
6193 my $html = "$my_url?p=$proj{'path'};a=summary";
6194 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6195 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02006196 print <<XML;
6197</outline>
6198</body>
6199</opml>
6200XML
Jakub Narebski717b8312006-07-31 21:22:15 +02006201}