blob: ecb0113a790bc1c2e01c94410ab293fecc90c5b9 [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
Alp Tokere130dda2006-07-12 23:55:10 +010030# core git executable to use
31# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070032our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020033
Kay Sieversb87d78d2005-08-07 20:21:04 +020034# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020035#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070036our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020037
Luke Luca5e9492007-10-16 20:45:25 -070038# fs traversing limit for getting project list
39# the number is relative to the projectroot
40our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
41
Kay Sieversb87d78d2005-08-07 20:21:04 +020042# target of the home link on top of all pages
Martin Waitz6132b7e2006-08-17 00:28:39 +020043our $home_link = $my_uri || "/";
Kay Sieversb87d78d2005-08-07 20:21:04 +020044
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090045# string of the home link on top of all pages
46our $home_link_str = "++GITWEB_HOME_LINK_STR++";
47
Alp Toker49da1da2006-07-11 21:10:26 +010048# name of your site or organization to appear in page titles
49# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020050our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010052
Alan Chandlerb2d34762006-10-03 13:49:03 +010053# filename of html text to include at top of each page
54our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020055# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070056our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010057# filename of html text to include at bottom of each page
58our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020059
Alan Chandlerb2d34762006-10-03 13:49:03 +010060# URI of stylesheets
61our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +020062# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020064# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -070065our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +020066# URI of GIT favicon, assumed to be image/png type
67our $favicon = "++GITWEB_FAVICON++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +020068
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020069# URI and label (title) of GIT logo link
70#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71#our $logo_label = "git documentation";
72our $logo_url = "http://git.or.cz/";
73our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -070074
Kay Sievers09bd7892005-08-07 20:21:23 +020075# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -070076our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020077
Michael Hendricks55feb122007-07-04 18:36:48 -060078# the width (in characters) of the projects list "Description" column
79our $projects_list_description_width = 25;
80
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +020081# default order of projects list
82# valid values are none, project, descr, owner, and age
83our $default_projects_order = "project";
84
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +020085# show repository only if this file exists
86# (only effective if this variable evaluates to true)
87our $export_ok = "++GITWEB_EXPORT_OK++";
88
89# only allow viewing of repositories also shown on the overview page
90our $strict_export = "++GITWEB_STRICT_EXPORT++";
91
Jakub Narebski19a87212006-08-15 23:03:17 +020092# list of git base URLs used for URL to where fetch project from,
93# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +020094our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +020095
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020096# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +020097our $default_blob_plain_mimetype = 'text/plain';
98our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020099
Petr Baudis2d007372006-06-18 00:01:06 +0200100# file to use for guessing MIME types before trying /etc/mime.types
101# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200102our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +0200103
Martin Koegler00f429a2007-06-03 17:42:44 +0200104# assume this charset if line contains non-UTF-8 characters;
105# it should be valid encoding (see Encoding::Supported(3pm) for list),
106# for which encoding all byte sequences are valid, for example
107# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108# could be even 'utf-8' for the old behavior)
109our $fallback_encoding = 'latin1';
110
Jakub Narebski69a9b412007-07-20 02:15:09 +0200111# rename detection options for git-diff and git-diff-tree
112# - default is '-M', with the cost proportional to
113# (number of removed files) * (number of new files).
114# - more costly is '-C' (which implies '-M'), with the cost proportional to
115# (number of changed files + number of removed files) * (number of new files)
116# - even more costly is '-C', '--find-copies-harder' with cost
117# (number of files in the original tree) * (number of new files)
118# - one might want to include '-B' option, e.g. '-B', '-M'
119our @diff_opts = ('-M'); # taken from git_commit
120
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200121# information about snapshot formats that gitweb is capable of serving
122our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
130 #
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
137
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
144
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
150);
151
152# Aliases so we understand old gitweb.snapshot values in repository
153# configuration.
154our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
157
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
162);
163
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530164# You define site-wide feature defaults here; override them with
165# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200166our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
171 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200172 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
175 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
178 #
Jakub Narebski17848fc2006-08-26 19:14:22 +0200179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530180
Petr Baudis45a3b122006-10-07 15:17:47 +0200181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
183
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530193
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200194 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200195 # tree. This can potentially generate high traffic if you have large
196 # project.
197
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200200 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200201 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200202 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100203 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200209 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200210
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200213 # Project specific override is not supported.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000214 'search' => {
215 'override' => 0,
216 'default' => [1]},
217
Petr Baudise7738552007-05-17 04:31:12 +0200218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
221
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
230
Petr Baudis45a3b122006-10-07 15:17:47 +0200231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
234
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200244
Petr Baudis45a3b122006-10-07 15:17:47 +0200245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
251
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
255
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200263
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200271
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530278);
279
280sub gitweb_check_feature {
281 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200282 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530287 if (!$override) { return @defaults; }
Martin Waitza9455912006-10-03 20:07:43 +0200288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
291 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530292 return $sub->(@defaults);
293}
294
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530295sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
297
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
302 }
303
304 return $_[0];
305}
306
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530307sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200308 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530309
310 my ($val) = git_get_project_config('snapshot');
311
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530314 }
315
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200316 return @fmts;
Luben Tuikovde9272f2006-09-28 16:49:21 -0700317}
318
Petr Baudise7738552007-05-17 04:31:12 +0200319sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
321
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
326 }
327
328 return ($_[0]);
329}
330
Jakub Narebski04f7a942006-09-11 00:29:27 +0200331sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
333
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
338 }
339
340 return ($_[0]);
341}
342
Junio C Hamano2172ce42006-10-03 02:30:47 -0700343# checking HEAD file with -e is fragile if the repository was
344# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345# and then pruned.
346sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
351}
352
353sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
357}
358
Jakub Narebskia7817852007-07-22 23:41:20 +0200359# process alternate names for backward compatibility
360# filter out unsupported (unknown) snapshot formats
361sub filter_snapshot_fmts {
362 my @fmts = @_;
363
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
368
369}
370
Junio C Hamano06c084d2006-08-02 13:50:20 -0700371our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Dennis Stosberg4b5dc982006-08-29 09:19:02 +0200372do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400373
374# version of the core git binary
375our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
376
377$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400378
Jakub Narebski154b4d72006-08-05 12:55:20 +0200379# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200380# input validation and dispatch
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200381our $action = $cgi->param('a');
Kay Sievers09bd7892005-08-07 20:21:23 +0200382if (defined $action) {
Kay Sieversc91da262005-09-03 14:50:33 +0200383 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200384 die_error(undef, "Invalid action parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200385 }
Kay Sieversb87d78d2005-08-07 20:21:04 +0200386}
387
Jakub Narebski24d06932006-09-26 01:57:02 +0200388# parameters which are pathnames
Martin Waitzdd702352006-09-16 23:08:32 +0200389our $project = $cgi->param('p');
Martin Waitz13d02162006-08-17 00:28:40 +0200390if (defined $project) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200391 if (!validate_pathname($project) ||
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200392 !(-d "$projectroot/$project") ||
Junio C Hamano2172ce42006-10-03 02:30:47 -0700393 !check_head_link("$projectroot/$project") ||
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200394 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
395 ($strict_export && !project_in_list($project))) {
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200396 undef $project;
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200397 die_error(undef, "No such project");
Kay Sievers9cd3d982005-08-07 20:17:42 +0200398 }
Kay Sievers2ad93312005-08-07 20:14:48 +0200399}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200400
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200401our $file_name = $cgi->param('f');
Jakub Narebski24d06932006-09-26 01:57:02 +0200402if (defined $file_name) {
403 if (!validate_pathname($file_name)) {
404 die_error(undef, "Invalid file parameter");
405 }
406}
Martin Waitz5c95fab2006-08-17 00:28:38 +0200407
Jakub Narebski24d06932006-09-26 01:57:02 +0200408our $file_parent = $cgi->param('fp');
409if (defined $file_parent) {
410 if (!validate_pathname($file_parent)) {
411 die_error(undef, "Invalid file parent parameter");
412 }
413}
414
415# parameters which are refnames
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200416our $hash = $cgi->param('h');
Kay Sievers4fac5292005-08-07 20:27:38 +0200417if (defined $hash) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200418 if (!validate_refname($hash)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200419 die_error(undef, "Invalid hash parameter");
Kay Sievers4fac5292005-08-07 20:27:38 +0200420 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200421}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200422
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200423our $hash_parent = $cgi->param('hp');
Kay Sieversc91da262005-09-03 14:50:33 +0200424if (defined $hash_parent) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200425 if (!validate_refname($hash_parent)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200426 die_error(undef, "Invalid hash parent parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200427 }
Kay Sievers09bd7892005-08-07 20:21:23 +0200428}
429
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200430our $hash_base = $cgi->param('hb');
Kay Sieversc91da262005-09-03 14:50:33 +0200431if (defined $hash_base) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200432 if (!validate_refname($hash_base)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200433 die_error(undef, "Invalid hash base parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200434 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200435}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200436
Miklos Vajna868bc062007-07-12 20:39:27 +0200437my %allowed_options = (
438 "--no-merges" => [ qw(rss atom log shortlog history) ],
439);
440
441our @extra_options = $cgi->param('opt');
442if (defined @extra_options) {
Jakub Narebski12075102007-07-28 16:27:32 +0200443 foreach my $opt (@extra_options) {
444 if (not exists $allowed_options{$opt}) {
Miklos Vajna868bc062007-07-12 20:39:27 +0200445 die_error(undef, "Invalid option parameter");
446 }
Jakub Narebski12075102007-07-28 16:27:32 +0200447 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
Miklos Vajna868bc062007-07-12 20:39:27 +0200448 die_error(undef, "Invalid option parameter for this action");
449 }
450 }
451}
452
Jakub Narebski420e92f2006-08-24 23:53:54 +0200453our $hash_parent_base = $cgi->param('hpb');
454if (defined $hash_parent_base) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200455 if (!validate_refname($hash_parent_base)) {
Jakub Narebski420e92f2006-08-24 23:53:54 +0200456 die_error(undef, "Invalid hash parent base parameter");
457 }
458}
459
Jakub Narebski24d06932006-09-26 01:57:02 +0200460# other parameters
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200461our $page = $cgi->param('pg');
Kay Sieversea4a6df2005-08-07 20:26:49 +0200462if (defined $page) {
Matthias Lederhoferac8e3f22006-09-17 13:52:45 +0200463 if ($page =~ m/[^0-9]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200464 die_error(undef, "Invalid page parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200465 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200466}
Kay Sievers823d5dc2005-08-07 19:57:58 +0200467
Petr Baudise7738552007-05-17 04:31:12 +0200468our $searchtype = $cgi->param('st');
469if (defined $searchtype) {
470 if ($searchtype =~ m/[^a-z]/) {
471 die_error(undef, "Invalid searchtype parameter");
472 }
473}
474
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200475our $searchtext = $cgi->param('s');
Jakub Narebski7e431ef2007-05-16 01:56:10 +0200476our $search_regexp;
Kay Sievers19806692005-08-07 20:26:27 +0200477if (defined $searchtext) {
Robert Fitzsimons9d032c72006-12-23 03:35:15 +0000478 if (length($searchtext) < 2) {
479 die_error(undef, "At least two characters are required for search parameter");
480 }
Jakub Narebski7e431ef2007-05-16 01:56:10 +0200481 $search_regexp = quotemeta $searchtext;
Kay Sievers19806692005-08-07 20:26:27 +0200482}
483
Martin Waitzdd702352006-09-16 23:08:32 +0200484# now read PATH_INFO and use it as alternative to parameters
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200485sub evaluate_path_info {
486 return if defined $project;
487 my $path_info = $ENV{"PATH_INFO"};
488 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200489 $path_info =~ s,^/+,,;
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200490 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200491 # find which part of PATH_INFO is project
Martin Waitzdd702352006-09-16 23:08:32 +0200492 $project = $path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200493 $project =~ s,/+$,,;
Junio C Hamano2172ce42006-10-03 02:30:47 -0700494 while ($project && !check_head_link("$projectroot/$project")) {
Martin Waitzdd702352006-09-16 23:08:32 +0200495 $project =~ s,/*[^/]*$,,;
496 }
Jakub Narebskicd90e752006-09-20 00:49:51 +0200497 # validate project
Jakub Narebski24d06932006-09-26 01:57:02 +0200498 $project = validate_pathname($project);
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200499 if (!$project ||
500 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
501 ($strict_export && !project_in_list($project))) {
502 undef $project;
503 return;
Martin Waitzdd702352006-09-16 23:08:32 +0200504 }
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200505 # do not change any parameters if an action is given using the query string
506 return if $action;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200507 $path_info =~ s,^$project/*,,;
508 my ($refname, $pathname) = split(/:/, $path_info, 2);
509 if (defined $pathname) {
510 # we got "project.git/branch:filename" or "project.git/branch:dir/"
511 # we could use git_get_type(branch:pathname), but it needs $git_dir
512 $pathname =~ s,^/+,,;
513 if (!$pathname || substr($pathname, -1) eq "/") {
514 $action ||= "tree";
Martin Waitz053d62b2006-09-21 09:48:21 +0200515 $pathname =~ s,/$,,;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200516 } else {
517 $action ||= "blob_plain";
518 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200519 $hash_base ||= validate_refname($refname);
520 $file_name ||= validate_pathname($pathname);
Jakub Narebskicd90e752006-09-20 00:49:51 +0200521 } elsif (defined $refname) {
Martin Waitzdd702352006-09-16 23:08:32 +0200522 # we got "project.git/branch"
523 $action ||= "shortlog";
Jakub Narebski24d06932006-09-26 01:57:02 +0200524 $hash ||= validate_refname($refname);
Martin Waitzdd702352006-09-16 23:08:32 +0200525 }
526}
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200527evaluate_path_info();
Martin Waitzdd702352006-09-16 23:08:32 +0200528
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200529# path to the current git repository
530our $git_dir;
531$git_dir = "$projectroot/$project" if $project;
Martin Waitzdd702352006-09-16 23:08:32 +0200532
Jakub Narebski717b8312006-07-31 21:22:15 +0200533# dispatch
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200534my %actions = (
535 "blame" => \&git_blame2,
536 "blobdiff" => \&git_blobdiff,
537 "blobdiff_plain" => \&git_blobdiff_plain,
538 "blob" => \&git_blob,
539 "blob_plain" => \&git_blob_plain,
540 "commitdiff" => \&git_commitdiff,
541 "commitdiff_plain" => \&git_commitdiff_plain,
542 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200543 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200544 "heads" => \&git_heads,
545 "history" => \&git_history,
546 "log" => \&git_log,
547 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100548 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200549 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200550 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200551 "shortlog" => \&git_shortlog,
552 "summary" => \&git_summary,
553 "tag" => \&git_tag,
554 "tags" => \&git_tags,
555 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530556 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100557 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200558 # those below don't need $project
559 "opml" => \&git_opml,
560 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200561 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200562);
563
Gerrit Pape7f9778b2007-05-10 07:32:07 +0000564if (!defined $action) {
565 if (defined $hash) {
566 $action = git_get_type($hash);
567 } elsif (defined $hash_base && defined $file_name) {
568 $action = git_get_type("$hash_base:$file_name");
569 } elsif (defined $project) {
570 $action = 'summary';
571 } else {
572 $action = 'project_list';
573 }
Jakub Narebski77a153f2006-08-22 16:59:20 +0200574}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200575if (!defined($actions{$action})) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200576 die_error(undef, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200577}
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200578if ($action !~ m/^(opml|project_list|project_index)$/ &&
579 !$project) {
580 die_error(undef, "Project needed");
581}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200582$actions{$action}->();
583exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200584
Jakub Narebski717b8312006-07-31 21:22:15 +0200585## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200586## action links
587
588sub href(%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200589 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +0100590 # default is to use -absolute url() i.e. $my_uri
591 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +0200592
Petr Baudis34c06112006-10-11 22:31:15 +0200593 # XXX: Warning: If you touch this, check the search form for updating,
594 # too.
Jakub Narebski498fe002006-08-22 19:05:25 +0200595
596 my @mapping = (
Martin Waitz06a9d862006-08-16 00:23:50 +0200597 project => "p",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200598 action => "a",
Martin Waitz06a9d862006-08-16 00:23:50 +0200599 file_name => "f",
Martin Waitz5c95fab2006-08-17 00:28:38 +0200600 file_parent => "fp",
Martin Waitz06a9d862006-08-16 00:23:50 +0200601 hash => "h",
602 hash_parent => "hp",
603 hash_base => "hb",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200604 hash_parent_base => "hpb",
Martin Waitz06a9d862006-08-16 00:23:50 +0200605 page => "pg",
Jakub Narebskia1565c42006-09-15 19:30:34 +0200606 order => "o",
Martin Waitz06a9d862006-08-16 00:23:50 +0200607 searchtext => "s",
Petr Baudis88ad7292006-10-24 05:15:46 +0200608 searchtype => "st",
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200609 snapshot_format => "sf",
Jakub Narebski12075102007-07-28 16:27:32 +0200610 extra_options => "opt",
Martin Waitz06a9d862006-08-16 00:23:50 +0200611 );
Jakub Narebski498fe002006-08-22 19:05:25 +0200612 my %mapping = @mapping;
Martin Waitz06a9d862006-08-16 00:23:50 +0200613
Jakub Narebski1cad2832007-11-01 13:06:27 +0100614 if ($params{-replay}) {
615 while (my ($name, $symbol) = each %mapping) {
616 if (!exists $params{$name}) {
617 # to allow for multivalued params we use arrayref form
618 $params{$name} = [ $cgi->param($symbol) ];
619 }
620 }
621 }
622
Jakub Narebskia1565c42006-09-15 19:30:34 +0200623 $params{'project'} = $project unless exists $params{'project'};
Martin Waitz06a9d862006-08-16 00:23:50 +0200624
Martin Waitz9e756902006-10-01 23:57:48 +0200625 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
626 if ($use_pathinfo) {
627 # use PATH_INFO for project name
628 $href .= "/$params{'project'}" if defined $params{'project'};
629 delete $params{'project'};
630
631 # Summary just uses the project path URL
632 if (defined $params{'action'} && $params{'action'} eq 'summary') {
633 delete $params{'action'};
634 }
635 }
636
637 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +0200638 my @result = ();
639 for (my $i = 0; $i < @mapping; $i += 2) {
640 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
641 if (defined $params{$name}) {
Jakub Narebskif22cca42007-07-29 01:04:09 +0200642 if (ref($params{$name}) eq "ARRAY") {
643 foreach my $par (@{$params{$name}}) {
644 push @result, $symbol . "=" . esc_param($par);
645 }
646 } else {
647 push @result, $symbol . "=" . esc_param($params{$name});
648 }
Jakub Narebski498fe002006-08-22 19:05:25 +0200649 }
650 }
Martin Waitz9e756902006-10-01 23:57:48 +0200651 $href .= "?" . join(';', @result) if scalar @result;
652
653 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +0200654}
655
656
657## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200658## validation, quoting/unquoting and escaping
659
Jakub Narebski24d06932006-09-26 01:57:02 +0200660sub validate_pathname {
661 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +0200662
Jakub Narebski24d06932006-09-26 01:57:02 +0200663 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
664 # at the beginning, at the end, and between slashes.
665 # also this catches doubled slashes
666 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
667 return undef;
668 }
669 # no null characters
670 if ($input =~ m!\0!) {
671 return undef;
672 }
673 return $input;
674}
675
676sub validate_refname {
677 my $input = shift || return undef;
678
679 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +0200680 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
681 return $input;
682 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200683 # it must be correct pathname
684 $input = validate_pathname($input)
685 or return undef;
686 # restrictions on ref name according to git-check-ref-format
687 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200688 return undef;
689 }
690 return $input;
691}
692
Martin Koegler00f429a2007-06-03 17:42:44 +0200693# decode sequences of octets in utf8 into Perl's internal form,
694# which is utf-8 with utf8 flag set if needed. gitweb writes out
695# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
696sub to_utf8 {
697 my $str = shift;
698 my $res;
699 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
700 if (defined $res) {
701 return $res;
702 } else {
703 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
704 }
705}
706
Kay Sievers232ff552005-11-24 16:56:55 +0100707# quote unsafe chars, but keep the slash, even when it's not
708# correct, but quoted slashes look too horrible in bookmarks
709sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100710 my $str = shift;
Petr Baudisa2f3db22006-09-24 00:18:41 +0200711 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +0100712 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +0100713 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +0100714 return $str;
715}
716
Jakub Narebskif93bff82006-09-26 01:58:41 +0200717# quote unsafe chars in whole URL, so some charactrs cannot be quoted
718sub esc_url {
719 my $str = shift;
720 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
721 $str =~ s/\+/%2B/g;
722 $str =~ s/ /\+/g;
723 return $str;
724}
725
Kay Sievers232ff552005-11-24 16:56:55 +0100726# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski6255ef02006-11-01 14:33:21 +0100727sub esc_html ($;%) {
Kay Sievers40c13812005-11-19 17:41:29 +0100728 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +0100729 my %opts = @_;
730
Martin Koegler00f429a2007-06-03 17:42:44 +0200731 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +0800732 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +0100733 if ($opts{'-nbsp'}) {
734 $str =~ s/ /&nbsp;/g;
735 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -0800736 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +0100737 return $str;
738}
739
Jakub Narebski391862e2006-11-25 09:43:59 +0100740# quote control characters and escape filename to HTML
741sub esc_path {
742 my $str = shift;
743 my %opts = @_;
744
Martin Koegler00f429a2007-06-03 17:42:44 +0200745 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +0800746 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +0100747 if ($opts{'-nbsp'}) {
748 $str =~ s/ /&nbsp;/g;
749 }
750 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
751 return $str;
752}
753
754# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +0100755sub quot_cec {
756 my $cntrl = shift;
757 my %es = ( # character escape codes, aka escape sequences
758 "\t" => '\t', # tab (HT)
759 "\n" => '\n', # line feed (LF)
760 "\r" => '\r', # carrige return (CR)
761 "\f" => '\f', # form feed (FF)
762 "\b" => '\b', # backspace (BS)
763 "\a" => '\a', # alarm (bell) (BEL)
764 "\e" => '\e', # escape (ESC)
765 "\013" => '\v', # vertical tab (VT)
766 "\000" => '\0', # nul character (NUL)
767 );
768 my $chr = ( (exists $es{$cntrl})
769 ? $es{$cntrl}
770 : sprintf('\%03o', ord($cntrl)) );
771 return "<span class=\"cntrl\">$chr</span>";
772}
773
Jakub Narebski391862e2006-11-25 09:43:59 +0100774# Alternatively use unicode control pictures codepoints,
775# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +0100776sub quot_upr {
777 my $cntrl = shift;
778 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
779 return "<span class=\"cntrl\">$chr</span>";
780}
781
Kay Sievers232ff552005-11-24 16:56:55 +0100782# git may return quoted and escaped filenames
783sub unquote {
784 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +0100785
786 sub unq {
787 my $seq = shift;
788 my %es = ( # character escape codes, aka escape sequences
789 't' => "\t", # tab (HT, TAB)
790 'n' => "\n", # newline (NL)
791 'r' => "\r", # return (CR)
792 'f' => "\f", # form feed (FF)
793 'b' => "\b", # backspace (BS)
794 'a' => "\a", # alarm (bell) (BEL)
795 'e' => "\e", # escape (ESC)
796 'v' => "\013", # vertical tab (VT)
797 );
798
799 if ($seq =~ m/^[0-7]{1,3}$/) {
800 # octal char sequence
801 return chr(oct($seq));
802 } elsif (exists $es{$seq}) {
803 # C escape sequence, aka character escape code
804 return $es{$seq}
805 }
806 # quoted ordinary character
807 return $seq;
808 }
809
Kay Sievers232ff552005-11-24 16:56:55 +0100810 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +0100811 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +0100812 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +0100813 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +0100814 }
815 return $str;
816}
817
Jakub Narebskif16db172006-08-06 02:08:31 +0200818# escape tabs (convert tabs to spaces)
819sub untabify {
820 my $line = shift;
821
822 while ((my $pos = index($line, "\t")) != -1) {
823 if (my $count = (8 - ($pos % 8))) {
824 my $spaces = ' ' x $count;
825 $line =~ s/\t/$spaces/;
826 }
827 }
828
829 return $line;
830}
831
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200832sub project_in_list {
833 my $project = shift;
834 my @list = git_get_projects_list();
835 return @list && scalar(grep { $_->{'path'} eq $project } @list);
836}
837
Jakub Narebski717b8312006-07-31 21:22:15 +0200838## ----------------------------------------------------------------------
839## HTML aware string manipulation
840
841sub chop_str {
842 my $str = shift;
843 my $len = shift;
844 my $add_len = shift || 10;
845
846 # allow only $len chars, but don't cut a word if it would fit in $add_len
847 # if it doesn't fit, cut it if it's still longer than the dots we would add
848 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
849 my $body = $1;
850 my $tail = $2;
851 if (length($tail) > 4) {
852 $tail = " ...";
853 $body =~ s/&[^;]*$//; # remove chopped character entities
854 }
855 return "$body$tail";
856}
857
David Symondsce58ec92007-10-23 11:31:22 +1000858# takes the same arguments as chop_str, but also wraps a <span> around the
859# result with a title attribute if it does get chopped. Additionally, the
860# string is HTML-escaped.
861sub chop_and_escape_str {
862 my $str = shift;
863 my $len = shift;
864 my $add_len = shift || 10;
865
866 my $chopped = chop_str($str, $len, $add_len);
867 if ($chopped eq $str) {
868 return esc_html($chopped);
869 } else {
870 return qq{<span title="} . esc_html($str) . qq{">} .
871 esc_html($chopped) . qq{</span>};
872 }
873}
874
Jakub Narebski717b8312006-07-31 21:22:15 +0200875## ----------------------------------------------------------------------
876## functions returning short strings
877
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000878# CSS class for given age value (in seconds)
879sub age_class {
880 my $age = shift;
881
Jakub Narebski785cdea2007-05-13 12:39:22 +0200882 if (!defined $age) {
883 return "noage";
884 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000885 return "age0";
886 } elsif ($age < 60*60*24*2) {
887 return "age1";
888 } else {
889 return "age2";
890 }
891}
892
Jakub Narebski717b8312006-07-31 21:22:15 +0200893# convert age in seconds to "nn units ago" string
894sub age_string {
895 my $age = shift;
896 my $age_str;
897
898 if ($age > 60*60*24*365*2) {
899 $age_str = (int $age/60/60/24/365);
900 $age_str .= " years ago";
901 } elsif ($age > 60*60*24*(365/12)*2) {
902 $age_str = int $age/60/60/24/(365/12);
903 $age_str .= " months ago";
904 } elsif ($age > 60*60*24*7*2) {
905 $age_str = int $age/60/60/24/7;
906 $age_str .= " weeks ago";
907 } elsif ($age > 60*60*24*2) {
908 $age_str = int $age/60/60/24;
909 $age_str .= " days ago";
910 } elsif ($age > 60*60*2) {
911 $age_str = int $age/60/60;
912 $age_str .= " hours ago";
913 } elsif ($age > 60*2) {
914 $age_str = int $age/60;
915 $age_str .= " min ago";
916 } elsif ($age > 2) {
917 $age_str = int $age;
918 $age_str .= " sec ago";
919 } else {
920 $age_str .= " right now";
921 }
922 return $age_str;
923}
924
Jakub Narebski01ac1e32007-07-28 16:27:31 +0200925use constant {
926 S_IFINVALID => 0030000,
927 S_IFGITLINK => 0160000,
928};
929
930# submodule/subproject, a commit object reference
931sub S_ISGITLINK($) {
932 my $mode = shift;
933
934 return (($mode & S_IFMT) == S_IFGITLINK)
935}
936
Jakub Narebski717b8312006-07-31 21:22:15 +0200937# convert file mode in octal to symbolic file mode string
938sub mode_str {
939 my $mode = oct shift;
940
Jakub Narebski01ac1e32007-07-28 16:27:31 +0200941 if (S_ISGITLINK($mode)) {
942 return 'm---------';
943 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200944 return 'drwxr-xr-x';
945 } elsif (S_ISLNK($mode)) {
946 return 'lrwxrwxrwx';
947 } elsif (S_ISREG($mode)) {
948 # git cares only about the executable bit
949 if ($mode & S_IXUSR) {
950 return '-rwxr-xr-x';
951 } else {
952 return '-rw-r--r--';
953 };
954 } else {
955 return '----------';
956 }
957}
958
959# convert file mode in octal to file type string
960sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +0200961 my $mode = shift;
962
963 if ($mode !~ m/^[0-7]+$/) {
964 return $mode;
965 } else {
966 $mode = oct $mode;
967 }
Jakub Narebski717b8312006-07-31 21:22:15 +0200968
Jakub Narebski01ac1e32007-07-28 16:27:31 +0200969 if (S_ISGITLINK($mode)) {
970 return "submodule";
971 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200972 return "directory";
973 } elsif (S_ISLNK($mode)) {
974 return "symlink";
975 } elsif (S_ISREG($mode)) {
976 return "file";
977 } else {
978 return "unknown";
979 }
980}
981
Jakub Narebski744d0ac2006-11-08 17:59:41 +0100982# convert file mode in octal to file type description string
983sub file_type_long {
984 my $mode = shift;
985
986 if ($mode !~ m/^[0-7]+$/) {
987 return $mode;
988 } else {
989 $mode = oct $mode;
990 }
991
Jakub Narebski01ac1e32007-07-28 16:27:31 +0200992 if (S_ISGITLINK($mode)) {
993 return "submodule";
994 } elsif (S_ISDIR($mode & S_IFMT)) {
Jakub Narebski744d0ac2006-11-08 17:59:41 +0100995 return "directory";
996 } elsif (S_ISLNK($mode)) {
997 return "symlink";
998 } elsif (S_ISREG($mode)) {
999 if ($mode & S_IXUSR) {
1000 return "executable";
1001 } else {
1002 return "file";
1003 };
1004 } else {
1005 return "unknown";
1006 }
1007}
1008
1009
Jakub Narebski717b8312006-07-31 21:22:15 +02001010## ----------------------------------------------------------------------
1011## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -05001012## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +02001013
Junio C Hamano225932e2006-11-09 00:57:13 -08001014# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +02001015sub format_log_line_html {
1016 my $line = shift;
1017
Junio C Hamano225932e2006-11-09 00:57:13 -08001018 $line = esc_html($line, -nbsp=>1);
Jakub Narebskibfe21912006-12-10 13:25:49 +01001019 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001020 my $hash_text = $1;
Jakub Narebskibfe21912006-12-10 13:25:49 +01001021 my $link =
1022 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1023 -class => "text"}, $hash_text);
1024 $line =~ s/$hash_text/$link/;
Jakub Narebski717b8312006-07-31 21:22:15 +02001025 }
1026 return $line;
1027}
1028
1029# format marker of refs pointing to given object
Jakub Narebski847e01f2006-08-14 02:05:47 +02001030sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +02001031 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001032 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +02001033
1034 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001035 foreach my $ref (@{$refs->{$id}}) {
1036 my ($type, $name) = qw();
1037 # e.g. tags/v2.6.11 or heads/next
1038 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1039 $type = $1;
1040 $name = $2;
1041 } else {
1042 $type = "ref";
1043 $name = $ref;
1044 }
1045
Jakub Narebski5fce2782006-12-15 23:49:12 +01001046 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1047 esc_html($name) . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001048 }
1049 }
1050
1051 if ($markers) {
1052 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +02001053 } else {
1054 return "";
1055 }
1056}
1057
Jakub Narebski17d07442006-08-14 02:08:27 +02001058# format, perhaps shortened and with markers, title line
1059sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001060 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02001061 $extra = '' unless defined($extra);
1062
1063 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +02001064 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02001065 -title => to_utf8($long)},
Jakub Narebski17d07442006-08-14 02:08:27 +02001066 esc_html($short) . $extra);
1067 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02001068 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +02001069 esc_html($long) . $extra);
1070 }
1071}
1072
Jakub Narebski90921742007-06-08 13:27:42 +02001073# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1074sub format_git_diff_header_line {
1075 my $line = shift;
1076 my $diffinfo = shift;
1077 my ($from, $to) = @_;
1078
1079 if ($diffinfo->{'nparents'}) {
1080 # combined diff
1081 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1082 if ($to->{'href'}) {
1083 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1084 esc_path($to->{'file'}));
1085 } else { # file was deleted (no href)
1086 $line .= esc_path($to->{'file'});
1087 }
1088 } else {
1089 # "ordinary" diff
1090 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1091 if ($from->{'href'}) {
1092 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1093 'a/' . esc_path($from->{'file'}));
1094 } else { # file was added (no href)
1095 $line .= 'a/' . esc_path($from->{'file'});
1096 }
1097 $line .= ' ';
1098 if ($to->{'href'}) {
1099 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1100 'b/' . esc_path($to->{'file'}));
1101 } else { # file was deleted
1102 $line .= 'b/' . esc_path($to->{'file'});
1103 }
1104 }
1105
1106 return "<div class=\"diff header\">$line</div>\n";
1107}
1108
1109# format extended diff header line, before patch itself
1110sub format_extended_diff_header_line {
1111 my $line = shift;
1112 my $diffinfo = shift;
1113 my ($from, $to) = @_;
1114
1115 # match <path>
1116 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1117 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1118 esc_path($from->{'file'}));
1119 }
1120 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1121 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1122 esc_path($to->{'file'}));
1123 }
1124 # match single <mode>
1125 if ($line =~ m/\s(\d{6})$/) {
1126 $line .= '<span class="info"> (' .
1127 file_type_long($1) .
1128 ')</span>';
1129 }
1130 # match <hash>
1131 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1132 # can match only for combined diff
1133 $line = 'index ';
1134 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1135 if ($from->{'href'}[$i]) {
1136 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1137 -class=>"hash"},
1138 substr($diffinfo->{'from_id'}[$i],0,7));
1139 } else {
1140 $line .= '0' x 7;
1141 }
1142 # separator
1143 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1144 }
1145 $line .= '..';
1146 if ($to->{'href'}) {
1147 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1148 substr($diffinfo->{'to_id'},0,7));
1149 } else {
1150 $line .= '0' x 7;
1151 }
1152
1153 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1154 # can match only for ordinary diff
1155 my ($from_link, $to_link);
1156 if ($from->{'href'}) {
1157 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1158 substr($diffinfo->{'from_id'},0,7));
1159 } else {
1160 $from_link = '0' x 7;
1161 }
1162 if ($to->{'href'}) {
1163 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1164 substr($diffinfo->{'to_id'},0,7));
1165 } else {
1166 $to_link = '0' x 7;
1167 }
1168 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1169 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1170 }
1171
1172 return $line . "<br/>\n";
1173}
1174
1175# format from-file/to-file diff header
1176sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001177 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02001178 my $line;
1179 my $result = '';
1180
1181 $line = $from_line;
1182 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001183 # no extra formatting for "^--- /dev/null"
1184 if (! $diffinfo->{'nparents'}) {
1185 # ordinary (single parent) diff
1186 if ($line =~ m!^--- "?a/!) {
1187 if ($from->{'href'}) {
1188 $line = '--- a/' .
1189 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1190 esc_path($from->{'file'}));
1191 } else {
1192 $line = '--- a/' .
1193 esc_path($from->{'file'});
1194 }
1195 }
1196 $result .= qq!<div class="diff from_file">$line</div>\n!;
1197
1198 } else {
1199 # combined diff (merge commit)
1200 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1201 if ($from->{'href'}[$i]) {
1202 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001203 $cgi->a({-href=>href(action=>"blobdiff",
1204 hash_parent=>$diffinfo->{'from_id'}[$i],
1205 hash_parent_base=>$parents[$i],
1206 file_parent=>$from->{'file'}[$i],
1207 hash=>$diffinfo->{'to_id'},
1208 hash_base=>$hash,
1209 file_name=>$to->{'file'}),
1210 -class=>"path",
1211 -title=>"diff" . ($i+1)},
1212 $i+1) .
1213 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001214 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1215 esc_path($from->{'file'}[$i]));
1216 } else {
1217 $line = '--- /dev/null';
1218 }
1219 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02001220 }
1221 }
Jakub Narebski90921742007-06-08 13:27:42 +02001222
1223 $line = $to_line;
1224 #assert($line =~ m/^\+\+\+/) if DEBUG;
1225 # no extra formatting for "^+++ /dev/null"
1226 if ($line =~ m!^\+\+\+ "?b/!) {
1227 if ($to->{'href'}) {
1228 $line = '+++ b/' .
1229 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1230 esc_path($to->{'file'}));
1231 } else {
1232 $line = '+++ b/' .
1233 esc_path($to->{'file'});
1234 }
1235 }
1236 $result .= qq!<div class="diff to_file">$line</div>\n!;
1237
1238 return $result;
1239}
1240
Jakub Narebskicd030c32007-06-08 13:33:28 +02001241# create note for patch simplified by combined diff
1242sub format_diff_cc_simplified {
1243 my ($diffinfo, @parents) = @_;
1244 my $result = '';
1245
1246 $result .= "<div class=\"diff header\">" .
1247 "diff --cc ";
1248 if (!is_deleted($diffinfo)) {
1249 $result .= $cgi->a({-href => href(action=>"blob",
1250 hash_base=>$hash,
1251 hash=>$diffinfo->{'to_id'},
1252 file_name=>$diffinfo->{'to_file'}),
1253 -class => "path"},
1254 esc_path($diffinfo->{'to_file'}));
1255 } else {
1256 $result .= esc_path($diffinfo->{'to_file'});
1257 }
1258 $result .= "</div>\n" . # class="diff header"
1259 "<div class=\"diff nodifferences\">" .
1260 "Simple merge" .
1261 "</div>\n"; # class="diff nodifferences"
1262
1263 return $result;
1264}
1265
Jakub Narebski90921742007-06-08 13:27:42 +02001266# format patch (diff) line (not to be used for diff headers)
Jakub Narebskieee08902006-08-24 00:15:14 +02001267sub format_diff_line {
1268 my $line = shift;
Jakub Narebski59e3b142006-11-18 23:35:40 +01001269 my ($from, $to) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001270 my $diff_class = "";
1271
1272 chomp $line;
1273
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001274 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1275 # combined diff
1276 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1277 if ($line =~ m/^\@{3}/) {
1278 $diff_class = " chunk_header";
1279 } elsif ($line =~ m/^\\/) {
1280 $diff_class = " incomplete";
1281 } elsif ($prefix =~ tr/+/+/) {
1282 $diff_class = " add";
1283 } elsif ($prefix =~ tr/-/-/) {
1284 $diff_class = " rem";
1285 }
1286 } else {
1287 # assume ordinary diff
1288 my $char = substr($line, 0, 1);
1289 if ($char eq '+') {
1290 $diff_class = " add";
1291 } elsif ($char eq '-') {
1292 $diff_class = " rem";
1293 } elsif ($char eq '@') {
1294 $diff_class = " chunk_header";
1295 } elsif ($char eq "\\") {
1296 $diff_class = " incomplete";
1297 }
Jakub Narebskieee08902006-08-24 00:15:14 +02001298 }
1299 $line = untabify($line);
Jakub Narebski59e3b142006-11-18 23:35:40 +01001300 if ($from && $to && $line =~ m/^\@{2} /) {
1301 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1302 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1303
1304 $from_lines = 0 unless defined $from_lines;
1305 $to_lines = 0 unless defined $to_lines;
1306
1307 if ($from->{'href'}) {
1308 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1309 -class=>"list"}, $from_text);
1310 }
1311 if ($to->{'href'}) {
1312 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1313 -class=>"list"}, $to_text);
1314 }
1315 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1316 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1317 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001318 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1319 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1320 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1321
1322 @from_text = split(' ', $ranges);
1323 for (my $i = 0; $i < @from_text; ++$i) {
1324 ($from_start[$i], $from_nlines[$i]) =
1325 (split(',', substr($from_text[$i], 1)), 0);
1326 }
1327
1328 $to_text = pop @from_text;
1329 $to_start = pop @from_start;
1330 $to_nlines = pop @from_nlines;
1331
1332 $line = "<span class=\"chunk_info\">$prefix ";
1333 for (my $i = 0; $i < @from_text; ++$i) {
1334 if ($from->{'href'}[$i]) {
1335 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1336 -class=>"list"}, $from_text[$i]);
1337 } else {
1338 $line .= $from_text[$i];
1339 }
1340 $line .= " ";
1341 }
1342 if ($to->{'href'}) {
1343 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1344 -class=>"list"}, $to_text);
1345 } else {
1346 $line .= $to_text;
1347 }
1348 $line .= " $prefix</span>" .
1349 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1350 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebski59e3b142006-11-18 23:35:40 +01001351 }
Jakub Narebski6255ef02006-11-01 14:33:21 +01001352 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001353}
1354
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001355# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1356# linked. Pass the hash of the tree/commit to snapshot.
1357sub format_snapshot_links {
1358 my ($hash) = @_;
1359 my @snapshot_fmts = gitweb_check_feature('snapshot');
Jakub Narebskia7817852007-07-22 23:41:20 +02001360 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001361 my $num_fmts = @snapshot_fmts;
1362 if ($num_fmts > 1) {
1363 # A parenthesized list of links bearing format names.
Jakub Narebskia7817852007-07-22 23:41:20 +02001364 # e.g. "snapshot (_tar.gz_ _zip_)"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001365 return "snapshot (" . join(' ', map
1366 $cgi->a({
1367 -href => href(
1368 action=>"snapshot",
1369 hash=>$hash,
1370 snapshot_format=>$_
1371 )
1372 }, $known_snapshot_formats{$_}{'display'})
1373 , @snapshot_fmts) . ")";
1374 } elsif ($num_fmts == 1) {
1375 # A single "snapshot" link whose tooltip bears the format name.
Jakub Narebskia7817852007-07-22 23:41:20 +02001376 # i.e. "_snapshot_"
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001377 my ($fmt) = @snapshot_fmts;
Jakub Narebskia7817852007-07-22 23:41:20 +02001378 return
1379 $cgi->a({
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001380 -href => href(
1381 action=>"snapshot",
1382 hash=>$hash,
1383 snapshot_format=>$fmt
1384 ),
1385 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1386 }, "snapshot");
1387 } else { # $num_fmts == 0
1388 return undef;
1389 }
1390}
1391
Jakub Narebski717b8312006-07-31 21:22:15 +02001392## ----------------------------------------------------------------------
1393## git utility subroutines, invoking git commands
1394
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001395# returns path to the core git executable and the --git-dir parameter as list
1396sub git_cmd {
1397 return $GIT, '--git-dir='.$git_dir;
1398}
1399
1400# returns path to the core git executable and the --git-dir parameter as string
1401sub git_cmd_str {
1402 return join(' ', git_cmd());
1403}
1404
Jakub Narebski717b8312006-07-31 21:22:15 +02001405# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02001406sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +02001407 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001408 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001409 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001410 $git_dir = "$projectroot/$project";
1411 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +02001412 my $head = <$fd>;
1413 close $fd;
1414 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1415 $retval = $1;
1416 }
1417 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001418 if (defined $o_git_dir) {
1419 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001420 }
1421 return $retval;
1422}
1423
1424# get type of given object
1425sub git_get_type {
1426 my $hash = shift;
1427
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001428 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001429 my $type = <$fd>;
1430 close $fd or return;
1431 chomp $type;
1432 return $type;
1433}
1434
1435sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301436 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001437
1438 return unless ($key);
1439 $key =~ s/^gitweb\.//;
1440 return if ($key =~ m/\W/);
1441
Tom Princee0d10e12007-01-28 16:16:53 -08001442 my @x = (git_cmd(), 'config');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301443 if (defined $type) { push @x, $type; }
1444 push @x, "--get";
1445 push @x, "gitweb.$key";
1446 my $val = qx(@x);
1447 chomp $val;
Jakub Narebski717b8312006-07-31 21:22:15 +02001448 return ($val);
1449}
1450
Jakub Narebski717b8312006-07-31 21:22:15 +02001451# get hash of given path at given ref
1452sub git_get_hash_by_path {
1453 my $base = shift;
1454 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001455 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02001456
Jakub Narebski4b02f482006-09-26 01:54:24 +02001457 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02001458
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001459 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Jakub Narebskicac4bd92006-08-05 13:13:53 +02001460 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02001461 my $line = <$fd>;
1462 close $fd or return undef;
1463
Jakub Narebski198a2a82007-05-12 21:16:34 +02001464 if (!defined $line) {
1465 # there is no tree or hash given by $path at $base
1466 return undef;
1467 }
1468
Jakub Narebski717b8312006-07-31 21:22:15 +02001469 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01001470 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001471 if (defined $type && $type ne $2) {
1472 # type doesn't match
1473 return undef;
1474 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001475 return $3;
1476}
1477
Jakub Narebskied224de2007-05-07 01:10:04 +02001478# get path of entry with given hash at given tree-ish (ref)
1479# used to get 'from' filename for combined diff (merge commit) for renames
1480sub git_get_path_by_hash {
1481 my $base = shift || return;
1482 my $hash = shift || return;
1483
1484 local $/ = "\0";
1485
1486 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1487 or return undef;
1488 while (my $line = <$fd>) {
1489 chomp $line;
1490
1491 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1492 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1493 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1494 close $fd;
1495 return $1;
1496 }
1497 }
1498 close $fd;
1499 return undef;
1500}
1501
Jakub Narebski717b8312006-07-31 21:22:15 +02001502## ......................................................................
1503## git utility functions, directly accessing git repository
1504
Jakub Narebski847e01f2006-08-14 02:05:47 +02001505sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +02001506 my $path = shift;
1507
1508 open my $fd, "$projectroot/$path/description" or return undef;
1509 my $descr = <$fd>;
1510 close $fd;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07001511 if (defined $descr) {
1512 chomp $descr;
1513 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001514 return $descr;
1515}
1516
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02001517sub git_get_project_url_list {
1518 my $path = shift;
1519
Petr Baudis74d61662006-09-23 01:15:18 +02001520 open my $fd, "$projectroot/$path/cloneurl" or return;
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02001521 my @git_project_url_list = map { chomp; $_ } <$fd>;
1522 close $fd;
1523
1524 return wantarray ? @git_project_url_list : \@git_project_url_list;
1525}
1526
Jakub Narebski847e01f2006-08-14 02:05:47 +02001527sub git_get_projects_list {
Petr Baudise30496d2006-10-24 05:33:17 +02001528 my ($filter) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001529 my @list;
1530
Petr Baudise30496d2006-10-24 05:33:17 +02001531 $filter ||= '';
1532 $filter =~ s/\.git$//;
1533
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001534 my ($check_forks) = gitweb_check_feature('forks');
1535
Jakub Narebski717b8312006-07-31 21:22:15 +02001536 if (-d $projects_list) {
1537 # search in directory
Petr Baudise30496d2006-10-24 05:33:17 +02001538 my $dir = $projects_list . ($filter ? "/$filter" : '');
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05301539 # remove the trailing "/"
1540 $dir =~ s!/+$!!;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001541 my $pfxlen = length("$dir");
Luke Luca5e9492007-10-16 20:45:25 -07001542 my $pfxdepth = ($dir =~ tr!/!!);
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001543
1544 File::Find::find({
1545 follow_fast => 1, # follow symbolic links
Junio C Hamanod20602e2007-07-27 01:23:03 -07001546 follow_skip => 2, # ignore duplicates
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001547 dangling_symlinks => 0, # ignore dangling symlinks, silently
1548 wanted => sub {
1549 # skip project-list toplevel, if we get it.
1550 return if (m!^[/.]$!);
1551 # only directories can be git repositories
1552 return unless (-d $_);
Luke Luca5e9492007-10-16 20:45:25 -07001553 # don't traverse too deep (Find is super slow on os x)
1554 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1555 $File::Find::prune = 1;
1556 return;
1557 }
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001558
1559 my $subdir = substr($File::Find::name, $pfxlen + 1);
1560 # we check related file in $projectroot
Petr Baudise30496d2006-10-24 05:33:17 +02001561 if ($check_forks and $subdir =~ m#/.#) {
1562 $File::Find::prune = 1;
1563 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1564 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001565 $File::Find::prune = 1;
1566 }
1567 },
1568 }, "$dir");
1569
Jakub Narebski717b8312006-07-31 21:22:15 +02001570 } elsif (-f $projects_list) {
1571 # read from file(url-encoded):
1572 # 'git%2Fgit.git Linus+Torvalds'
1573 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1574 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001575 my %paths;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +02001576 open my ($fd), $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001577 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02001578 while (my $line = <$fd>) {
1579 chomp $line;
1580 my ($path, $owner) = split ' ', $line;
1581 $path = unescape($path);
1582 $owner = unescape($owner);
1583 if (!defined $path) {
1584 next;
1585 }
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001586 if ($filter ne '') {
1587 # looking for forks;
1588 my $pfx = substr($path, 0, length($filter));
1589 if ($pfx ne $filter) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001590 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001591 }
1592 my $sfx = substr($path, length($filter));
1593 if ($sfx !~ /^\/.*\.git$/) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001594 next PROJECT;
1595 }
1596 } elsif ($check_forks) {
1597 PATH:
1598 foreach my $filter (keys %paths) {
1599 # looking for forks;
1600 my $pfx = substr($path, 0, length($filter));
1601 if ($pfx ne $filter) {
1602 next PATH;
1603 }
1604 my $sfx = substr($path, length($filter));
1605 if ($sfx !~ /^\/.*\.git$/) {
1606 next PATH;
1607 }
1608 # is a fork, don't include it in
1609 # the list
1610 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001611 }
1612 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07001613 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001614 my $pr = {
1615 path => $path,
Martin Koegler00f429a2007-06-03 17:42:44 +02001616 owner => to_utf8($owner),
Jakub Narebski717b8312006-07-31 21:22:15 +02001617 };
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001618 push @list, $pr;
1619 (my $forks_path = $path) =~ s/\.git$//;
1620 $paths{$forks_path}++;
Jakub Narebski717b8312006-07-31 21:22:15 +02001621 }
1622 }
1623 close $fd;
1624 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001625 return @list;
1626}
1627
Junio C Hamano47852452007-07-03 22:10:42 -07001628our $gitweb_project_owner = undef;
1629sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001630
Junio C Hamano47852452007-07-03 22:10:42 -07001631 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001632
Junio C Hamano47852452007-07-03 22:10:42 -07001633 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001634 # read from file (url-encoded):
1635 # 'git%2Fgit.git Linus+Torvalds'
1636 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1637 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1638 if (-f $projects_list) {
1639 open (my $fd , $projects_list);
1640 while (my $line = <$fd>) {
1641 chomp $line;
1642 my ($pr, $ow) = split ' ', $line;
1643 $pr = unescape($pr);
1644 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07001645 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001646 }
1647 close $fd;
1648 }
Junio C Hamano47852452007-07-03 22:10:42 -07001649}
1650
1651sub git_get_project_owner {
1652 my $project = shift;
1653 my $owner;
1654
1655 return undef unless $project;
1656
1657 if (!defined $gitweb_project_owner) {
1658 git_get_project_list_from_file();
1659 }
1660
1661 if (exists $gitweb_project_owner->{$project}) {
1662 $owner = $gitweb_project_owner->{$project};
1663 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001664 if (!defined $owner) {
1665 $owner = get_file_owner("$projectroot/$project");
1666 }
1667
1668 return $owner;
1669}
1670
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001671sub git_get_last_activity {
1672 my ($path) = @_;
1673 my $fd;
1674
1675 $git_dir = "$projectroot/$path";
1676 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00001677 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001678 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00001679 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001680 'refs/heads') or return;
1681 my $most_recent = <$fd>;
1682 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02001683 if (defined $most_recent &&
1684 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001685 my $timestamp = $1;
1686 my $age = time - $timestamp;
1687 return ($age, age_string($age));
1688 }
Matt McCutchenc9563952007-06-28 18:15:22 -04001689 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001690}
1691
Jakub Narebski847e01f2006-08-14 02:05:47 +02001692sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02001693 my $type = shift || "";
1694 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01001695 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1696 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1697 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1698 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02001699 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001700
Jakub Narebski717b8312006-07-31 21:22:15 +02001701 while (my $line = <$fd>) {
1702 chomp $line;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01001703 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001704 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001705 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02001706 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001707 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02001708 }
1709 }
1710 }
1711 close $fd or return;
1712 return \%refs;
1713}
1714
Jakub Narebski56a322f2006-08-24 19:41:23 +02001715sub git_get_rev_name_tags {
1716 my $hash = shift || return undef;
1717
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001718 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02001719 or return;
1720 my $name_rev = <$fd>;
1721 close $fd;
1722
1723 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1724 return $1;
1725 } else {
1726 # catches also '$hash undefined' output
1727 return undef;
1728 }
1729}
1730
Jakub Narebski717b8312006-07-31 21:22:15 +02001731## ----------------------------------------------------------------------
1732## parse to hash functions
1733
Jakub Narebski847e01f2006-08-14 02:05:47 +02001734sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02001735 my $epoch = shift;
1736 my $tz = shift || "-0000";
1737
1738 my %date;
1739 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1740 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1741 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1742 $date{'hour'} = $hour;
1743 $date{'minute'} = $min;
1744 $date{'mday'} = $mday;
1745 $date{'day'} = $days[$wday];
1746 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001747 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1748 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02001749 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1750 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001751 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1752 1900+$year, $mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02001753
1754 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1755 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1756 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1757 $date{'hour_local'} = $hour;
1758 $date{'minute_local'} = $min;
1759 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001760 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1761 1900+$year, $mon+1, $mday,
1762 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02001763 return %date;
1764}
1765
Jakub Narebski847e01f2006-08-14 02:05:47 +02001766sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02001767 my $tag_id = shift;
1768 my %tag;
1769 my @comment;
1770
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001771 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001772 $tag{'id'} = $tag_id;
1773 while (my $line = <$fd>) {
1774 chomp $line;
1775 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1776 $tag{'object'} = $1;
1777 } elsif ($line =~ m/^type (.+)$/) {
1778 $tag{'type'} = $1;
1779 } elsif ($line =~ m/^tag (.+)$/) {
1780 $tag{'name'} = $1;
1781 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1782 $tag{'author'} = $1;
1783 $tag{'epoch'} = $2;
1784 $tag{'tz'} = $3;
1785 } elsif ($line =~ m/--BEGIN/) {
1786 push @comment, $line;
1787 last;
1788 } elsif ($line eq "") {
1789 last;
1790 }
1791 }
1792 push @comment, <$fd>;
1793 $tag{'comment'} = \@comment;
1794 close $fd or return;
1795 if (!defined $tag{'name'}) {
1796 return
1797 };
1798 return %tag
1799}
1800
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001801sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001802 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001803 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02001804 my %co;
1805
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001806 pop @commit_lines; # Remove '\0'
1807
Jakub Narebski198a2a82007-05-12 21:16:34 +02001808 if (! @commit_lines) {
1809 return;
1810 }
1811
Jakub Narebski717b8312006-07-31 21:22:15 +02001812 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02001813 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001814 return;
1815 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001816 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02001817 while (my $line = shift @commit_lines) {
1818 last if $line eq "\n";
1819 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1820 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001821 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00001822 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02001823 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1824 $co{'author'} = $1;
1825 $co{'author_epoch'} = $2;
1826 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01001827 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1828 $co{'author_name'} = $1;
1829 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02001830 } else {
1831 $co{'author_name'} = $co{'author'};
1832 }
1833 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1834 $co{'committer'} = $1;
1835 $co{'committer_epoch'} = $2;
1836 $co{'committer_tz'} = $3;
1837 $co{'committer_name'} = $co{'committer'};
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01001838 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1839 $co{'committer_name'} = $1;
1840 $co{'committer_email'} = $2;
1841 } else {
1842 $co{'committer_name'} = $co{'committer'};
1843 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001844 }
1845 }
1846 if (!defined $co{'tree'}) {
1847 return;
1848 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00001849 $co{'parents'} = \@parents;
1850 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02001851
1852 foreach my $title (@commit_lines) {
1853 $title =~ s/^ //;
1854 if ($title ne "") {
1855 $co{'title'} = chop_str($title, 80, 5);
1856 # remove leading stuff of merges to make the interesting part visible
1857 if (length($title) > 50) {
1858 $title =~ s/^Automatic //;
1859 $title =~ s/^merge (of|with) /Merge ... /i;
1860 if (length($title) > 50) {
1861 $title =~ s/(http|rsync):\/\///;
1862 }
1863 if (length($title) > 50) {
1864 $title =~ s/(master|www|rsync)\.//;
1865 }
1866 if (length($title) > 50) {
1867 $title =~ s/kernel.org:?//;
1868 }
1869 if (length($title) > 50) {
1870 $title =~ s/\/pub\/scm//;
1871 }
1872 }
1873 $co{'title_short'} = chop_str($title, 50, 5);
1874 last;
1875 }
1876 }
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02001877 if ($co{'title'} eq "") {
1878 $co{'title'} = $co{'title_short'} = '(no commit message)';
1879 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001880 # remove added spaces
1881 foreach my $line (@commit_lines) {
1882 $line =~ s/^ //;
1883 }
1884 $co{'comment'} = \@commit_lines;
1885
1886 my $age = time - $co{'committer_epoch'};
1887 $co{'age'} = $age;
1888 $co{'age_string'} = age_string($age);
1889 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1890 if ($age > 60*60*24*7*2) {
1891 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1892 $co{'age_string_age'} = $co{'age_string'};
1893 } else {
1894 $co{'age_string_date'} = $co{'age_string'};
1895 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1896 }
1897 return %co;
1898}
1899
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001900sub parse_commit {
1901 my ($commit_id) = @_;
1902 my %co;
1903
1904 local $/ = "\0";
1905
1906 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001907 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001908 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001909 "--max-count=1",
1910 $commit_id,
1911 "--",
1912 or die_error(undef, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001913 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001914 close $fd;
1915
1916 return %co;
1917}
1918
1919sub parse_commits {
1920 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1921 my @cos;
1922
1923 $maxcount ||= 1;
1924 $skip ||= 0;
1925
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001926 local $/ = "\0";
1927
1928 open my $fd, "-|", git_cmd(), "rev-list",
1929 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001930 ($arg ? ($arg) : ()),
1931 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00001932 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02001933 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001934 $commit_id,
1935 "--",
1936 ($filename ? ($filename) : ())
1937 or die_error(undef, "Open git-rev-list failed");
1938 while (my $line = <$fd>) {
1939 my %co = parse_commit_text($line);
1940 push @cos, \%co;
1941 }
1942 close $fd;
1943
1944 return wantarray ? @cos : \@cos;
1945}
1946
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001947# parse ref from ref_file, given by ref_id, with given type
1948sub parse_ref {
1949 my $ref_file = shift;
1950 my $ref_id = shift;
1951 my $type = shift || git_get_type($ref_id);
1952 my %ref_item;
1953
1954 $ref_item{'type'} = $type;
1955 $ref_item{'id'} = $ref_id;
1956 $ref_item{'epoch'} = 0;
1957 $ref_item{'age'} = "unknown";
1958 if ($type eq "tag") {
1959 my %tag = parse_tag($ref_id);
1960 $ref_item{'comment'} = $tag{'comment'};
1961 if ($tag{'type'} eq "commit") {
1962 my %co = parse_commit($tag{'object'});
1963 $ref_item{'epoch'} = $co{'committer_epoch'};
1964 $ref_item{'age'} = $co{'age_string'};
1965 } elsif (defined($tag{'epoch'})) {
1966 my $age = time - $tag{'epoch'};
1967 $ref_item{'epoch'} = $tag{'epoch'};
1968 $ref_item{'age'} = age_string($age);
1969 }
1970 $ref_item{'reftype'} = $tag{'type'};
1971 $ref_item{'name'} = $tag{'name'};
1972 $ref_item{'refid'} = $tag{'object'};
1973 } elsif ($type eq "commit"){
1974 my %co = parse_commit($ref_id);
1975 $ref_item{'reftype'} = "commit";
1976 $ref_item{'name'} = $ref_file;
1977 $ref_item{'title'} = $co{'title'};
1978 $ref_item{'refid'} = $ref_id;
1979 $ref_item{'epoch'} = $co{'committer_epoch'};
1980 $ref_item{'age'} = $co{'age_string'};
1981 } else {
1982 $ref_item{'reftype'} = $type;
1983 $ref_item{'name'} = $ref_file;
1984 $ref_item{'refid'} = $ref_id;
1985 }
1986
1987 return %ref_item;
1988}
1989
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001990# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02001991sub parse_difftree_raw_line {
1992 my $line = shift;
1993 my %res;
1994
1995 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1996 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1997 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1998 $res{'from_mode'} = $1;
1999 $res{'to_mode'} = $2;
2000 $res{'from_id'} = $3;
2001 $res{'to_id'} = $4;
Jakub Narebski6aa6f922007-11-01 12:38:09 +01002002 $res{'status'} = $res{'status_str'} = $5;
Jakub Narebski740e67f2006-08-21 23:07:00 +02002003 $res{'similarity'} = $6;
2004 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002005 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002006 } else {
Jakub Narebski9d301452007-11-01 12:38:08 +01002007 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02002008 }
2009 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02002010 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2011 # combined diff (for merge commit)
2012 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2013 $res{'nparents'} = length($1);
2014 $res{'from_mode'} = [ split(' ', $2) ];
2015 $res{'to_mode'} = pop @{$res{'from_mode'}};
2016 $res{'from_id'} = [ split(' ', $3) ];
2017 $res{'to_id'} = pop @{$res{'from_id'}};
Jakub Narebski6aa6f922007-11-01 12:38:09 +01002018 $res{'status_str'} = $4;
Jakub Narebski78bc4032007-05-07 01:10:03 +02002019 $res{'status'} = [ split('', $4) ];
2020 $res{'to_file'} = unquote($5);
2021 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002022 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02002023 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2024 $res{'commit'} = $1;
2025 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02002026
2027 return wantarray ? %res : \%res;
2028}
2029
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002030# wrapper: return parsed line of git-diff-tree "raw" output
2031# (the argument might be raw line, or parsed info)
2032sub parsed_difftree_line {
2033 my $line_or_ref = shift;
2034
2035 if (ref($line_or_ref) eq "HASH") {
2036 # pre-parsed (or generated by hand)
2037 return $line_or_ref;
2038 } else {
2039 return parse_difftree_raw_line($line_or_ref);
2040 }
2041}
2042
Jakub Narebskicb849b42006-08-31 00:32:15 +02002043# parse line of git-ls-tree output
2044sub parse_ls_tree_line ($;%) {
2045 my $line = shift;
2046 my %opts = @_;
2047 my %res;
2048
2049 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01002050 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002051
2052 $res{'mode'} = $1;
2053 $res{'type'} = $2;
2054 $res{'hash'} = $3;
2055 if ($opts{'-z'}) {
2056 $res{'name'} = $4;
2057 } else {
2058 $res{'name'} = unquote($4);
2059 }
2060
2061 return wantarray ? %res : \%res;
2062}
2063
Jakub Narebski90921742007-06-08 13:27:42 +02002064# generates _two_ hashes, references to which are passed as 2 and 3 argument
2065sub parse_from_to_diffinfo {
2066 my ($diffinfo, $from, $to, @parents) = @_;
2067
2068 if ($diffinfo->{'nparents'}) {
2069 # combined diff
2070 $from->{'file'} = [];
2071 $from->{'href'} = [];
2072 fill_from_file_info($diffinfo, @parents)
2073 unless exists $diffinfo->{'from_file'};
2074 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
Jakub Narebski9d301452007-11-01 12:38:08 +01002075 $from->{'file'}[$i] =
2076 defined $diffinfo->{'from_file'}[$i] ?
2077 $diffinfo->{'from_file'}[$i] :
2078 $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002079 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2080 $from->{'href'}[$i] = href(action=>"blob",
2081 hash_base=>$parents[$i],
2082 hash=>$diffinfo->{'from_id'}[$i],
2083 file_name=>$from->{'file'}[$i]);
2084 } else {
2085 $from->{'href'}[$i] = undef;
2086 }
2087 }
2088 } else {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002089 # ordinary (not combined) diff
Jakub Narebski9d301452007-11-01 12:38:08 +01002090 $from->{'file'} = $diffinfo->{'from_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002091 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2092 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2093 hash=>$diffinfo->{'from_id'},
2094 file_name=>$from->{'file'});
2095 } else {
2096 delete $from->{'href'};
2097 }
2098 }
2099
Jakub Narebski9d301452007-11-01 12:38:08 +01002100 $to->{'file'} = $diffinfo->{'to_file'};
Jakub Narebski90921742007-06-08 13:27:42 +02002101 if (!is_deleted($diffinfo)) { # file exists in result
2102 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2103 hash=>$diffinfo->{'to_id'},
2104 file_name=>$to->{'file'});
2105 } else {
2106 delete $to->{'href'};
2107 }
2108}
2109
Jakub Narebski717b8312006-07-31 21:22:15 +02002110## ......................................................................
2111## parse to array of hashes functions
2112
Jakub Narebskicd146402006-11-02 20:23:11 +01002113sub git_get_heads_list {
2114 my $limit = shift;
2115 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002116
Jakub Narebskicd146402006-11-02 20:23:11 +01002117 open my $fd, '-|', git_cmd(), 'for-each-ref',
2118 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2119 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2120 'refs/heads'
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002121 or return;
2122 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01002123 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02002124
Jakub Narebskicd146402006-11-02 20:23:11 +01002125 chomp $line;
2126 my ($refinfo, $committerinfo) = split(/\0/, $line);
2127 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2128 my ($committer, $epoch, $tz) =
2129 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2130 $name =~ s!^refs/heads/!!;
2131
2132 $ref_item{'name'} = $name;
2133 $ref_item{'id'} = $hash;
2134 $ref_item{'title'} = $title || '(no commit message)';
2135 $ref_item{'epoch'} = $epoch;
2136 if ($epoch) {
2137 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2138 } else {
2139 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02002140 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002141
2142 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002143 }
2144 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07002145
Jakub Narebskicd146402006-11-02 20:23:11 +01002146 return wantarray ? @headslist : \@headslist;
2147}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002148
Jakub Narebskicd146402006-11-02 20:23:11 +01002149sub git_get_tags_list {
2150 my $limit = shift;
2151 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002152
Jakub Narebskicd146402006-11-02 20:23:11 +01002153 open my $fd, '-|', git_cmd(), 'for-each-ref',
2154 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2155 '--format=%(objectname) %(objecttype) %(refname) '.
2156 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2157 'refs/tags'
2158 or return;
2159 while (my $line = <$fd>) {
2160 my %ref_item;
2161
2162 chomp $line;
2163 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2164 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2165 my ($creator, $epoch, $tz) =
2166 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2167 $name =~ s!^refs/tags/!!;
2168
2169 $ref_item{'type'} = $type;
2170 $ref_item{'id'} = $id;
2171 $ref_item{'name'} = $name;
2172 if ($type eq "tag") {
2173 $ref_item{'subject'} = $title;
2174 $ref_item{'reftype'} = $reftype;
2175 $ref_item{'refid'} = $refid;
2176 } else {
2177 $ref_item{'reftype'} = $type;
2178 $ref_item{'refid'} = $id;
2179 }
2180
2181 if ($type eq "tag" || $type eq "commit") {
2182 $ref_item{'epoch'} = $epoch;
2183 if ($epoch) {
2184 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2185 } else {
2186 $ref_item{'age'} = "unknown";
2187 }
2188 }
2189
2190 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02002191 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002192 close $fd;
2193
2194 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002195}
2196
2197## ----------------------------------------------------------------------
2198## filesystem-related functions
2199
2200sub get_file_owner {
2201 my $path = shift;
2202
2203 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2204 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2205 if (!defined $gcos) {
2206 return undef;
2207 }
2208 my $owner = $gcos;
2209 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02002210 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02002211}
2212
2213## ......................................................................
2214## mimetype related functions
2215
2216sub mimetype_guess_file {
2217 my $filename = shift;
2218 my $mimemap = shift;
2219 -r $mimemap or return undef;
2220
2221 my %mimemap;
2222 open(MIME, $mimemap) or return undef;
2223 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02002224 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02002225 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07002226 if (defined $exts) {
2227 my @exts = split(/\s+/, $exts);
2228 foreach my $ext (@exts) {
2229 $mimemap{$ext} = $mime;
2230 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002231 }
2232 }
2233 close(MIME);
2234
Jakub Narebski80593192006-09-19 13:57:03 +02002235 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02002236 return $mimemap{$1};
2237}
2238
2239sub mimetype_guess {
2240 my $filename = shift;
2241 my $mime;
2242 $filename =~ /\./ or return undef;
2243
2244 if ($mimetypes_file) {
2245 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02002246 if ($file !~ m!^/!) { # if it is relative path
2247 # it is relative to project
2248 $file = "$projectroot/$project/$file";
2249 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002250 $mime = mimetype_guess_file($filename, $file);
2251 }
2252 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2253 return $mime;
2254}
2255
Jakub Narebski847e01f2006-08-14 02:05:47 +02002256sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02002257 my $fd = shift;
2258 my $filename = shift;
2259
2260 if ($filename) {
2261 my $mime = mimetype_guess($filename);
2262 $mime and return $mime;
2263 }
2264
2265 # just in case
2266 return $default_blob_plain_mimetype unless $fd;
2267
2268 if (-T $fd) {
2269 return 'text/plain' .
2270 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2271 } elsif (! $filename) {
2272 return 'application/octet-stream';
2273 } elsif ($filename =~ m/\.png$/i) {
2274 return 'image/png';
2275 } elsif ($filename =~ m/\.gif$/i) {
2276 return 'image/gif';
2277 } elsif ($filename =~ m/\.jpe?g$/i) {
2278 return 'image/jpeg';
2279 } else {
2280 return 'application/octet-stream';
2281 }
2282}
2283
2284## ======================================================================
2285## functions printing HTML: header, footer, error page
2286
Kay Sievers12a88f22005-08-07 20:02:47 +02002287sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02002288 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02002289 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002290
Petr Baudis8be28902006-10-24 05:18:39 +02002291 my $title = "$site_name";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002292 if (defined $project) {
Martin Koegler00f429a2007-06-03 17:42:44 +02002293 $title .= " - " . to_utf8($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002294 if (defined $action) {
2295 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002296 if (defined $file_name) {
Jakub Narebski403d0902006-11-08 11:48:56 +01002297 $title .= " - " . esc_path($file_name);
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002298 if ($action eq "tree" && $file_name !~ m|/$|) {
2299 $title .= "/";
2300 }
2301 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02002302 }
2303 }
Alp Tokerf6801d62006-07-11 11:19:34 +01002304 my $content_type;
2305 # require explicit support from the UA if we are to send the page as
2306 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2307 # we have to do this because MSIE sometimes globs '*/*', pretending to
2308 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02002309 if (defined $cgi->http('HTTP_ACCEPT') &&
2310 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2311 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01002312 $content_type = 'application/xhtml+xml';
2313 } else {
2314 $content_type = 'text/html';
2315 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002316 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2317 -status=> $status, -expires => $expires);
Jakub Narebski45c9a752006-12-27 23:59:51 +01002318 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02002319 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02002320<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02002321<!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 +02002322<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02002323<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02002324<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02002325<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01002326<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01002327<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02002328<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02002329<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02002330EOF
Alan Chandlerb2d34762006-10-03 13:49:03 +01002331# print out each stylesheet that exist
2332 if (defined $stylesheet) {
2333#provides backwards capability for those people who define style sheet in a config file
2334 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2335 } else {
2336 foreach my $stylesheet (@stylesheets) {
2337 next unless $stylesheet;
2338 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2339 }
2340 }
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002341 if (defined $project) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002342 printf('<link rel="alternate" title="%s log RSS feed" '.
2343 'href="%s" type="application/rss+xml" />'."\n",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002344 esc_param($project), href(action=>"rss"));
Jakub Narebski12075102007-07-28 16:27:32 +02002345 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2346 'href="%s" type="application/rss+xml" />'."\n",
2347 esc_param($project), href(action=>"rss",
2348 extra_options=>"--no-merges"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002349 printf('<link rel="alternate" title="%s log Atom feed" '.
2350 'href="%s" type="application/atom+xml" />'."\n",
2351 esc_param($project), href(action=>"atom"));
Jakub Narebski12075102007-07-28 16:27:32 +02002352 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2353 'href="%s" type="application/atom+xml" />'."\n",
2354 esc_param($project), href(action=>"atom",
2355 extra_options=>"--no-merges"));
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002356 } else {
2357 printf('<link rel="alternate" title="%s projects list" '.
2358 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2359 $site_name, href(project=>undef, action=>"project_index"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002360 printf('<link rel="alternate" title="%s projects feeds" '.
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002361 'href="%s" type="text/x-opml"/>'."\n",
2362 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002363 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002364 if (defined $favicon) {
2365 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2366 }
Jakub Narebski10161352006-08-05 13:18:58 +02002367
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002368 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01002369 "<body>\n";
2370
2371 if (-f $site_header) {
2372 open (my $fd, $site_header);
2373 print <$fd>;
2374 close $fd;
2375 }
2376
2377 print "<div class=\"page_header\">\n" .
Jakub Narebski9a7a62f2006-10-06 12:31:05 +02002378 $cgi->a({-href => esc_url($logo_url),
2379 -title => $logo_label},
2380 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
Jakub Narebskif93bff82006-09-26 01:58:41 +02002381 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002382 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002383 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02002384 if (defined $action) {
2385 print " / $action";
2386 }
Kay Sievers19806692005-08-07 20:26:27 +02002387 print "\n";
Robert Fitzsimons6be93512006-12-23 03:35:16 +00002388 }
Petr Baudisd77b5672007-05-17 04:24:19 +02002389 print "</div>\n";
2390
Robert Fitzsimons6be93512006-12-23 03:35:16 +00002391 my ($have_search) = gitweb_check_feature('search');
2392 if ((defined $project) && ($have_search)) {
Kay Sievers19806692005-08-07 20:26:27 +02002393 if (!defined $searchtext) {
2394 $searchtext = "";
2395 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02002396 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03002397 if (defined $hash_base) {
2398 $search_hash = $hash_base;
2399 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02002400 $search_hash = $hash;
2401 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002402 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02002403 }
Matt McCutchen40375a82007-06-28 14:57:07 -04002404 my $action = $my_uri;
2405 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2406 if ($use_pathinfo) {
2407 $action .= "/$project";
2408 } else {
2409 $cgi->param("p", $project);
2410 }
Kay Sievers19806692005-08-07 20:26:27 +02002411 $cgi->param("a", "search");
Kay Sieversc39e47d2005-09-17 03:00:21 +02002412 $cgi->param("h", $search_hash);
Matt McCutchen40375a82007-06-28 14:57:07 -04002413 print $cgi->startform(-method => "get", -action => $action) .
Kay Sieversc994d622005-08-07 20:27:18 +02002414 "<div class=\"search\">\n" .
Matt McCutchen40375a82007-06-28 14:57:07 -04002415 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
Kay Sieversc994d622005-08-07 20:27:18 +02002416 $cgi->hidden(-name => "a") . "\n" .
Kay Sieversc39e47d2005-09-17 03:00:21 +02002417 $cgi->hidden(-name => "h") . "\n" .
Petr Baudis88ad7292006-10-24 05:15:46 +02002418 $cgi->popup_menu(-name => 'st', -default => 'commit',
Petr Baudise7738552007-05-17 04:31:12 +02002419 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Petr Baudis88ad7292006-10-24 05:15:46 +02002420 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2421 " search:\n",
Kay Sieversc994d622005-08-07 20:27:18 +02002422 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2423 "</div>" .
2424 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02002425 }
Kay Sievers161332a2005-08-07 19:49:46 +02002426}
2427
Kay Sievers12a88f22005-08-07 20:02:47 +02002428sub git_footer_html {
Kay Sievers6191f8e2005-08-07 20:19:56 +02002429 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002430 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002431 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002432 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01002433 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02002434 }
Jakub Narebskia1565c42006-09-15 19:30:34 +02002435 print $cgi->a({-href => href(action=>"rss"),
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002436 -class => "rss_logo"}, "RSS") . " ";
2437 print $cgi->a({-href => href(action=>"atom"),
2438 -class => "rss_logo"}, "Atom") . "\n";
Kay Sieversc994d622005-08-07 20:27:18 +02002439 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02002440 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002441 -class => "rss_logo"}, "OPML") . " ";
2442 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2443 -class => "rss_logo"}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02002444 }
Alan Chandlerb2d34762006-10-03 13:49:03 +01002445 print "</div>\n" ;
2446
2447 if (-f $site_footer) {
2448 open (my $fd, $site_footer);
2449 print <$fd>;
2450 close $fd;
2451 }
2452
2453 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02002454 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02002455}
2456
Kay Sievers061cc7c2005-08-07 20:15:57 +02002457sub die_error {
2458 my $status = shift || "403 Forbidden";
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02002459 my $error = shift || "Malformed query, file missing or permission denied";
Kay Sievers664f4cc2005-08-07 20:17:19 +02002460
Kay Sieversa59d4af2005-08-07 20:15:44 +02002461 git_header_html($status);
Jakub Narebski59b9f612006-08-22 23:42:53 +02002462 print <<EOF;
2463<div class="page_body">
2464<br /><br />
2465$status - $error
2466<br />
2467</div>
2468EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02002469 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002470 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002471}
2472
Jakub Narebski717b8312006-07-31 21:22:15 +02002473## ----------------------------------------------------------------------
2474## functions printing or outputting HTML: navigation
2475
Jakub Narebski847e01f2006-08-14 02:05:47 +02002476sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002477 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2478 $extra = '' if !defined $extra; # pager or formats
2479
2480 my @navs = qw(summary shortlog log commit commitdiff tree);
2481 if ($suppress) {
2482 @navs = grep { $_ ne $suppress } @navs;
2483 }
2484
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002485 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002486 if (defined $head) {
2487 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02002488 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002489 }
2490 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2491 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02002492 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002493 }
2494 }
2495 }
Jakub Narebski3be8e722007-04-01 22:22:21 +02002496 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2497 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002498
2499 print "<div class=\"page_nav\">\n" .
2500 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002501 map { $_ eq $current ?
2502 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2503 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02002504 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002505 "</div>\n";
2506}
2507
Jakub Narebski847e01f2006-08-14 02:05:47 +02002508sub format_paging_nav {
Jakub Narebski6855f422006-07-30 20:31:00 +02002509 my ($action, $hash, $head, $page, $nrevs) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02002510 my $paging_nav;
2511
2512
2513 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002514 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002515 } else {
2516 $paging_nav .= "HEAD";
2517 }
2518
2519 if ($page > 0) {
2520 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01002521 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02002522 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002523 } else {
2524 $paging_nav .= " &sdot; prev";
2525 }
2526
2527 if ($nrevs >= (100 * ($page+1)-1)) {
2528 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01002529 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02002530 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002531 } else {
2532 $paging_nav .= " &sdot; next";
2533 }
2534
2535 return $paging_nav;
2536}
2537
Jakub Narebski717b8312006-07-31 21:22:15 +02002538## ......................................................................
2539## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02002540
Jakub Narebski847e01f2006-08-14 02:05:47 +02002541sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02002542 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002543 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02002544
Jakub Narebski3be8e722007-04-01 22:22:21 +02002545 $args{'action'} = $action;
2546 $args{'hash'} = $hash if $hash;
2547 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02002548
2549 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002550 $cgi->a({-href => href(%args), -class => "title"},
2551 $title ? $title : $action) .
2552 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02002553}
2554
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002555#sub git_print_authorship (\%) {
2556sub git_print_authorship {
2557 my $co = shift;
2558
Jakub Narebskia44465c2006-08-28 23:17:31 +02002559 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002560 print "<div class=\"author_date\">" .
2561 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02002562 " [$ad{'rfc2822'}";
2563 if ($ad{'hour_local'} < 6) {
2564 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2565 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2566 } else {
2567 printf(" (%02d:%02d %s)",
2568 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2569 }
2570 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002571}
2572
Jakub Narebski717b8312006-07-31 21:22:15 +02002573sub git_print_page_path {
2574 my $name = shift;
2575 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002576 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002577
Jakub Narebski4df118e2006-10-21 17:53:55 +02002578
2579 print "<div class=\"page_path\">";
2580 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02002581 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02002582 print " / ";
2583 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02002584 my @dirname = split '/', $name;
2585 my $basename = pop @dirname;
2586 my $fullname = '';
2587
Jakub Narebski762c7202006-09-04 18:17:58 +02002588 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02002589 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02002590 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2591 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002592 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02002593 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02002594 }
2595 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002596 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02002597 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002598 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02002599 } elsif (defined $type && $type eq 'tree') {
2600 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2601 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002602 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02002603 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002604 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01002605 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002606 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02002607 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02002608 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02002609}
2610
Jakub Narebskib7f92532006-08-28 14:48:10 +02002611# sub git_print_log (\@;%) {
2612sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02002613 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02002614 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02002615
Jakub Narebskib7f92532006-08-28 14:48:10 +02002616 if ($opts{'-remove_title'}) {
2617 # remove title, i.e. first line of log
2618 shift @$log;
2619 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02002620 # remove leading empty lines
2621 while (defined $log->[0] && $log->[0] eq "") {
2622 shift @$log;
2623 }
2624
2625 # print log
2626 my $signoff = 0;
2627 my $empty = 0;
2628 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02002629 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2630 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02002631 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02002632 if (! $opts{'-remove_signoff'}) {
2633 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2634 next;
2635 } else {
2636 # remove signoff lines
2637 next;
2638 }
2639 } else {
2640 $signoff = 0;
2641 }
2642
Jakub Narebskid16d0932006-08-17 11:21:23 +02002643 # print only one empty line
2644 # do not print empty line after signoff
2645 if ($line eq "") {
2646 next if ($empty || $signoff);
2647 $empty = 1;
2648 } else {
2649 $empty = 0;
2650 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02002651
2652 print format_log_line_html($line) . "<br/>\n";
2653 }
2654
2655 if ($opts{'-final_empty_line'}) {
2656 # end with single empty line
2657 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02002658 }
2659}
2660
Jakub Narebskie33fba42006-12-10 13:25:46 +01002661# return link target (what link points to)
2662sub git_get_link_target {
2663 my $hash = shift;
2664 my $link_target;
2665
2666 # read link
2667 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2668 or return;
2669 {
2670 local $/;
2671 $link_target = <$fd>;
2672 }
2673 close $fd
2674 or return;
2675
2676 return $link_target;
2677}
2678
Jakub Narebski3bf9d572006-12-10 13:25:48 +01002679# given link target, and the directory (basedir) the link is in,
2680# return target of link relative to top directory (top tree);
2681# return undef if it is not possible (including absolute links).
2682sub normalize_link_target {
2683 my ($link_target, $basedir, $hash_base) = @_;
2684
2685 # we can normalize symlink target only if $hash_base is provided
2686 return unless $hash_base;
2687
2688 # absolute symlinks (beginning with '/') cannot be normalized
2689 return if (substr($link_target, 0, 1) eq '/');
2690
2691 # normalize link target to path from top (root) tree (dir)
2692 my $path;
2693 if ($basedir) {
2694 $path = $basedir . '/' . $link_target;
2695 } else {
2696 # we are in top (root) tree (dir)
2697 $path = $link_target;
2698 }
2699
2700 # remove //, /./, and /../
2701 my @path_parts;
2702 foreach my $part (split('/', $path)) {
2703 # discard '.' and ''
2704 next if (!$part || $part eq '.');
2705 # handle '..'
2706 if ($part eq '..') {
2707 if (@path_parts) {
2708 pop @path_parts;
2709 } else {
2710 # link leads outside repository (outside top dir)
2711 return;
2712 }
2713 } else {
2714 push @path_parts, $part;
2715 }
2716 }
2717 $path = join('/', @path_parts);
2718
2719 return $path;
2720}
Jakub Narebskie33fba42006-12-10 13:25:46 +01002721
Jakub Narebskifa702002006-08-31 00:35:07 +02002722# print tree entry (row of git_tree), but without encompassing <tr> element
2723sub git_print_tree_entry {
2724 my ($t, $basedir, $hash_base, $have_blame) = @_;
2725
2726 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01002727 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02002728
Luben Tuikov4de741b2006-09-25 22:38:16 -07002729 # The format of a table row is: mode list link. Where mode is
2730 # the mode of the entry, list is the name of the entry, an href,
2731 # and link is the action links of the entry.
2732
Jakub Narebskifa702002006-08-31 00:35:07 +02002733 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2734 if ($t->{'type'} eq "blob") {
2735 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07002736 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002737 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01002738 -class => "list"}, esc_path($t->{'name'}));
2739 if (S_ISLNK(oct $t->{'mode'})) {
2740 my $link_target = git_get_link_target($t->{'hash'});
2741 if ($link_target) {
Jakub Narebski3bf9d572006-12-10 13:25:48 +01002742 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2743 if (defined $norm_target) {
2744 print " -> " .
2745 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2746 file_name=>$norm_target),
2747 -title => $norm_target}, esc_path($link_target));
2748 } else {
2749 print " -> " . esc_path($link_target);
2750 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01002751 }
2752 }
2753 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07002754 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02002755 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002756 file_name=>"$basedir$t->{'name'}", %base_key)},
2757 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02002758 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02002759 print " | " .
2760 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002761 file_name=>"$basedir$t->{'name'}", %base_key)},
2762 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02002763 }
2764 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02002765 print " | " .
2766 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02002767 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2768 "history");
2769 }
2770 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07002771 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002772 file_name=>"$basedir$t->{'name'}")},
2773 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07002774 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02002775
2776 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07002777 print "<td class=\"list\">";
2778 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskifa702002006-08-31 00:35:07 +02002779 file_name=>"$basedir$t->{'name'}", %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01002780 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07002781 print "</td>\n";
2782 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02002783 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002784 file_name=>"$basedir$t->{'name'}", %base_key)},
2785 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02002786 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02002787 print " | " .
2788 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02002789 file_name=>"$basedir$t->{'name'}")},
2790 "history");
2791 }
2792 print "</td>\n";
Jakub Narebski01ac1e32007-07-28 16:27:31 +02002793 } else {
2794 # unknown object: we can only present history for it
2795 # (this includes 'commit' object, i.e. submodule support)
2796 print "<td class=\"list\">" .
2797 esc_path($t->{'name'}) .
2798 "</td>\n";
2799 print "<td class=\"link\">";
2800 if (defined $hash_base) {
2801 print $cgi->a({-href => href(action=>"history",
2802 hash_base=>$hash_base,
2803 file_name=>"$basedir$t->{'name'}")},
2804 "history");
2805 }
2806 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02002807 }
2808}
2809
Jakub Narebski717b8312006-07-31 21:22:15 +02002810## ......................................................................
2811## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02002812
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002813# get pre-image filenames for merge (combined) diff
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002814sub fill_from_file_info {
2815 my ($diff, @parents) = @_;
2816
2817 $diff->{'from_file'} = [ ];
2818 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2819 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2820 if ($diff->{'status'}[$i] eq 'R' ||
2821 $diff->{'status'}[$i] eq 'C') {
2822 $diff->{'from_file'}[$i] =
2823 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2824 }
2825 }
2826
2827 return $diff;
2828}
2829
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002830# is current raw difftree line of file deletion
Jakub Narebski90921742007-06-08 13:27:42 +02002831sub is_deleted {
2832 my $diffinfo = shift;
2833
Jakub Narebski6aa6f922007-11-01 12:38:09 +01002834 return $diffinfo->{'status_str'} =~ /D/;
Jakub Narebski90921742007-06-08 13:27:42 +02002835}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002836
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002837# does patch correspond to [previous] difftree raw line
2838# $diffinfo - hashref of parsed raw diff format
2839# $patchinfo - hashref of parsed patch diff format
2840# (the same keys as in $diffinfo)
2841sub is_patch_split {
2842 my ($diffinfo, $patchinfo) = @_;
2843
2844 return defined $diffinfo && defined $patchinfo
Jakub Narebski9d301452007-11-01 12:38:08 +01002845 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002846}
2847
2848
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002849sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02002850 my ($difftree, $hash, @parents) = @_;
2851 my ($parent) = $parents[0];
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002852 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002853 print "<div class=\"list_head\">\n";
2854 if ($#{$difftree} > 10) {
2855 print(($#{$difftree} + 1) . " files changed:\n");
2856 }
2857 print "</div>\n";
2858
Jakub Narebskied224de2007-05-07 01:10:04 +02002859 print "<table class=\"" .
2860 (@parents > 1 ? "combined " : "") .
2861 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02002862
2863 # header only for combined diff in 'commitdiff' view
Jakub Narebski3ef408a2007-09-08 21:54:28 +02002864 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
Jakub Narebski47598d72007-06-08 13:24:56 +02002865 if ($has_header) {
2866 # table header
2867 print "<thead><tr>\n" .
2868 "<th></th><th></th>\n"; # filename, patchN link
2869 for (my $i = 0; $i < @parents; $i++) {
2870 my $par = $parents[$i];
2871 print "<th>" .
2872 $cgi->a({-href => href(action=>"commitdiff",
2873 hash=>$hash, hash_parent=>$par),
2874 -title => 'commitdiff to parent number ' .
2875 ($i+1) . ': ' . substr($par,0,7)},
2876 $i+1) .
2877 "&nbsp;</th>\n";
2878 }
2879 print "</tr></thead>\n<tbody>\n";
2880 }
2881
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07002882 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02002883 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002884 foreach my $line (@{$difftree}) {
Jakub Narebski0cec6db2007-10-30 01:35:05 +01002885 my $diff = parsed_difftree_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002886
2887 if ($alternate) {
2888 print "<tr class=\"dark\">\n";
2889 } else {
2890 print "<tr class=\"light\">\n";
2891 }
2892 $alternate ^= 1;
2893
Jakub Narebski493e01d2007-05-07 01:10:06 +02002894 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02002895
Jakub Narebski493e01d2007-05-07 01:10:06 +02002896 fill_from_file_info($diff, @parents)
2897 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002898
Jakub Narebski90921742007-06-08 13:27:42 +02002899 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002900 # file exists in the result (child) commit
2901 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02002902 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2903 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002904 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02002905 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02002906 "</td>\n";
2907 } else {
2908 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02002909 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02002910 "</td>\n";
2911 }
2912
2913 if ($action eq 'commitdiff') {
2914 # link to patch
2915 $patchno++;
2916 print "<td class=\"link\">" .
2917 $cgi->a({-href => "#patch$patchno"}, "patch") .
2918 " | " .
2919 "</td>\n";
2920 }
2921
2922 my $has_history = 0;
2923 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02002924 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002925 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02002926 my $from_hash = $diff->{'from_id'}[$i];
2927 my $from_path = $diff->{'from_file'}[$i];
2928 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02002929
2930 $has_history ||= ($status ne 'A');
2931 $not_deleted ||= ($status ne 'D');
2932
Jakub Narebskied224de2007-05-07 01:10:04 +02002933 if ($status eq 'A') {
2934 print "<td class=\"link\" align=\"right\"> | </td>\n";
2935 } elsif ($status eq 'D') {
2936 print "<td class=\"link\">" .
2937 $cgi->a({-href => href(action=>"blob",
2938 hash_base=>$hash,
2939 hash=>$from_hash,
2940 file_name=>$from_path)},
2941 "blob" . ($i+1)) .
2942 " | </td>\n";
2943 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02002944 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002945 print "<td class=\"link nochange\">";
2946 } else {
2947 print "<td class=\"link\">";
2948 }
2949 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002950 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002951 hash_parent=>$from_hash,
2952 hash_base=>$hash,
2953 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002954 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002955 file_parent=>$from_path)},
2956 "diff" . ($i+1)) .
2957 " | </td>\n";
2958 }
2959 }
2960
2961 print "<td class=\"link\">";
2962 if ($not_deleted) {
2963 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002964 hash=>$diff->{'to_id'},
2965 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002966 hash_base=>$hash)},
2967 "blob");
2968 print " | " if ($has_history);
2969 }
2970 if ($has_history) {
2971 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002972 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002973 hash_base=>$hash)},
2974 "history");
2975 }
2976 print "</td>\n";
2977
2978 print "</tr>\n";
2979 next; # instead of 'else' clause, to avoid extra indent
2980 }
2981 # else ordinary diff
2982
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002983 my ($to_mode_oct, $to_mode_str, $to_file_type);
2984 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02002985 if ($diff->{'to_mode'} ne ('0' x 6)) {
2986 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002987 if (S_ISREG($to_mode_oct)) { # only for regular file
2988 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002989 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002990 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002991 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002992 if ($diff->{'from_mode'} ne ('0' x 6)) {
2993 $from_mode_oct = oct $diff->{'from_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002994 if (S_ISREG($to_mode_oct)) { # only for regular file
2995 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2996 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002997 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002998 }
2999
Jakub Narebski493e01d2007-05-07 01:10:06 +02003000 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003001 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3002 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3003 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003004 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003005 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3006 hash_base=>$hash, file_name=>$diff->{'file'}),
3007 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003008 print "</td>\n";
3009 print "<td>$mode_chng</td>\n";
3010 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003011 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003012 # link to patch
3013 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003014 print $cgi->a({-href => "#patch$patchno"}, "patch");
Jakub Narebski897d1d22006-11-19 22:51:39 +01003015 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003016 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003017 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3018 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01003019 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02003020 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003021
Jakub Narebski493e01d2007-05-07 01:10:06 +02003022 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003023 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07003024 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003025 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3026 hash_base=>$parent, file_name=>$diff->{'file'}),
3027 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003028 print "</td>\n";
3029 print "<td>$mode_chng</td>\n";
3030 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02003031 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003032 # link to patch
3033 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07003034 print $cgi->a({-href => "#patch$patchno"}, "patch");
3035 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02003036 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003037 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3038 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003039 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003040 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003041 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003042 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003043 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003044 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02003045 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003046 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003047 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07003048 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003049
Jakub Narebski493e01d2007-05-07 01:10:06 +02003050 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003051 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003052 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003053 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01003054 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003055 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003056 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003057 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3058 if ($from_mode_str && $to_mode_str) {
3059 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3060 } elsif ($to_mode_str) {
3061 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003062 }
3063 }
3064 $mode_chnge .= "]</span>\n";
3065 }
3066 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003067 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3068 hash_base=>$hash, file_name=>$diff->{'file'}),
3069 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07003070 print "</td>\n";
3071 print "<td>$mode_chnge</td>\n";
3072 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003073 if ($action eq 'commitdiff') {
3074 # link to patch
3075 $patchno++;
3076 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3077 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003078 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003079 # "commit" view and modified file (not onlu mode changed)
3080 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003081 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003082 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003083 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003084 "diff") .
3085 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003086 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003087 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3088 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003089 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003090 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003091 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003092 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003093 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003094 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07003095 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003096 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003097 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003098 print "</td>\n";
3099
Jakub Narebski493e01d2007-05-07 01:10:06 +02003100 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003101 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02003102 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003103 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003104 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003105 # mode also for directories, so we cannot use $to_mode_str
3106 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003107 }
3108 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003109 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003110 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3111 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003112 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3113 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003114 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3115 -class => "list"}, esc_path($diff->{'from_file'})) .
3116 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07003117 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003118 if ($action eq 'commitdiff') {
3119 # link to patch
3120 $patchno++;
3121 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3122 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003123 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003124 # "commit" view and modified file (not only pure rename or copy)
3125 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003126 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003127 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003128 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003129 "diff") .
3130 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003131 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003132 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3133 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003134 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003135 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003136 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003137 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003138 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003139 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01003140 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003141 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003142 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003143 print "</td>\n";
3144
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003145 } # we should not encounter Unmerged (U) or Unknown (X) status
3146 print "</tr>\n";
3147 }
Jakub Narebski47598d72007-06-08 13:24:56 +02003148 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003149 print "</table>\n";
3150}
3151
Jakub Narebskieee08902006-08-24 00:15:14 +02003152sub git_patchset_body {
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003153 my ($fd, $difftree, $hash, @hash_parents) = @_;
3154 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02003155
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003156 my $is_combined = (@hash_parents > 1);
Jakub Narebskieee08902006-08-24 00:15:14 +02003157 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07003158 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003159 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02003160 my $diffinfo;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003161 my $to_name;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003162 my (%from, %to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003163
3164 print "<div class=\"patchset\">\n";
3165
Jakub Narebski6d55f052006-11-18 23:35:39 +01003166 # skip to first patch
3167 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02003168 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02003169
Jakub Narebski6d55f052006-11-18 23:35:39 +01003170 last if ($patch_line =~ m/^diff /);
3171 }
3172
3173 PATCH:
3174 while ($patch_line) {
Jakub Narebski6d55f052006-11-18 23:35:39 +01003175
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003176 # parse "git diff" header line
3177 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3178 # $1 is from_name, which we do not use
3179 $to_name = unquote($2);
3180 $to_name =~ s!^b/!!;
3181 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3182 # $1 is 'cc' or 'combined', which we do not use
3183 $to_name = unquote($2);
3184 } else {
3185 $to_name = undef;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003186 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003187
3188 # check if current patch belong to current raw line
3189 # and parse raw git-diff line if needed
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003190 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
Jakub Narebski22065372007-05-12 12:42:32 +02003191 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01003192 print "<div class=\"patch cont\">\n";
3193 } else {
3194 # advance raw git-diff output if needed
3195 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02003196
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003197 # read and prepare patch information
3198 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3199
Jakub Narebskicd030c32007-06-08 13:33:28 +02003200 # compact combined diff output can have some patches skipped
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003201 # find which patch (using pathname of result) we are at now;
3202 if ($is_combined) {
3203 while ($to_name ne $diffinfo->{'to_file'}) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003204 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3205 format_diff_cc_simplified($diffinfo, @hash_parents) .
3206 "</div>\n"; # class="patch"
3207
3208 $patch_idx++;
3209 $patch_number++;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003210
3211 last if $patch_idx > $#$difftree;
3212 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003213 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003214 }
Jakub Narebski711fa742007-09-08 21:49:11 +02003215
Jakub Narebski90921742007-06-08 13:27:42 +02003216 # modifies %from, %to hashes
3217 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebski5f855052007-05-17 22:54:28 +02003218
Jakub Narebski6d55f052006-11-18 23:35:39 +01003219 # this is first patch for raw difftree line with $patch_idx index
3220 # we index @$difftree array from 0, but number patches from 1
3221 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003222 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003223
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003224 # git diff header
3225 #assert($patch_line =~ m/^diff /) if DEBUG;
3226 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3227 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003228 # print "git diff" header
Jakub Narebski90921742007-06-08 13:27:42 +02003229 print format_git_diff_header_line($patch_line, $diffinfo,
3230 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003231
Jakub Narebski6d55f052006-11-18 23:35:39 +01003232 # print extended diff header
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003233 print "<div class=\"diff extended_header\">\n";
Jakub Narebski6d55f052006-11-18 23:35:39 +01003234 EXTENDED_HEADER:
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003235 while ($patch_line = <$fd>) {
3236 chomp $patch_line;
3237
3238 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3239
Jakub Narebski90921742007-06-08 13:27:42 +02003240 print format_extended_diff_header_line($patch_line, $diffinfo,
3241 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003242 }
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003243 print "</div>\n"; # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02003244
Jakub Narebski6d55f052006-11-18 23:35:39 +01003245 # from-file/to-file diff header
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01003246 if (! $patch_line) {
3247 print "</div>\n"; # class="patch"
3248 last PATCH;
3249 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01003250 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003251 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003252
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003253 my $last_patch_line = $patch_line;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003254 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003255 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02003256 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003257
Jakub Narebski90921742007-06-08 13:27:42 +02003258 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02003259 $diffinfo, \%from, \%to,
3260 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003261
3262 # the patch itself
3263 LINE:
3264 while ($patch_line = <$fd>) {
3265 chomp $patch_line;
3266
3267 next PATCH if ($patch_line =~ m/^diff /);
3268
Jakub Narebski59e3b142006-11-18 23:35:40 +01003269 print format_diff_line($patch_line, \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003270 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003271
Jakub Narebski6d55f052006-11-18 23:35:39 +01003272 } continue {
3273 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02003274 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02003275
Jakub Narebskicd030c32007-06-08 13:33:28 +02003276 # for compact combined (--cc) format, with chunk and patch simpliciaction
3277 # patchset might be empty, but there might be unprocessed raw lines
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003278 for (++$patch_idx if $patch_number > 0;
Jakub Narebskicd030c32007-06-08 13:33:28 +02003279 $patch_idx < @$difftree;
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003280 ++$patch_idx) {
Jakub Narebskicd030c32007-06-08 13:33:28 +02003281 # read and prepare patch information
Jakub Narebski0cec6db2007-10-30 01:35:05 +01003282 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
Jakub Narebskicd030c32007-06-08 13:33:28 +02003283
3284 # generate anchor for "patch" links in difftree / whatchanged part
3285 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3286 format_diff_cc_simplified($diffinfo, @hash_parents) .
3287 "</div>\n"; # class="patch"
3288
3289 $patch_number++;
3290 }
3291
Jakub Narebskid26c4262007-05-17 00:05:55 +02003292 if ($patch_number == 0) {
3293 if (@hash_parents > 1) {
3294 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3295 } else {
3296 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3297 }
3298 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003299
3300 print "</div>\n"; # class="patchset"
3301}
3302
3303# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3304
Petr Baudise30496d2006-10-24 05:33:17 +02003305sub git_project_list_body {
3306 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3307
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08003308 my ($check_forks) = gitweb_check_feature('forks');
Petr Baudise30496d2006-10-24 05:33:17 +02003309
3310 my @projects;
3311 foreach my $pr (@$projlist) {
3312 my (@aa) = git_get_last_activity($pr->{'path'});
3313 unless (@aa) {
3314 next;
3315 }
3316 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3317 if (!defined $pr->{'descr'}) {
3318 my $descr = git_get_project_description($pr->{'path'}) || "";
Martin Koegler00f429a2007-06-03 17:42:44 +02003319 $pr->{'descr_long'} = to_utf8($descr);
Michael Hendricks55feb122007-07-04 18:36:48 -06003320 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02003321 }
3322 if (!defined $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003323 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02003324 }
3325 if ($check_forks) {
3326 my $pname = $pr->{'path'};
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003327 if (($pname =~ s/\.git$//) &&
3328 ($pname !~ /\/$/) &&
3329 (-d "$projectroot/$pname")) {
3330 $pr->{'forks'} = "-d $projectroot/$pname";
3331 }
3332 else {
3333 $pr->{'forks'} = 0;
3334 }
Petr Baudise30496d2006-10-24 05:33:17 +02003335 }
3336 push @projects, $pr;
3337 }
3338
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003339 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02003340 $from = 0 unless defined $from;
3341 $to = $#projects if (!defined $to || $#projects < $to);
3342
3343 print "<table class=\"project_list\">\n";
3344 unless ($no_header) {
3345 print "<tr>\n";
3346 if ($check_forks) {
3347 print "<th></th>\n";
3348 }
3349 if ($order eq "project") {
3350 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3351 print "<th>Project</th>\n";
3352 } else {
3353 print "<th>" .
3354 $cgi->a({-href => href(project=>undef, order=>'project'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003355 -class => "header"}, "Project") .
Petr Baudise30496d2006-10-24 05:33:17 +02003356 "</th>\n";
3357 }
3358 if ($order eq "descr") {
3359 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3360 print "<th>Description</th>\n";
3361 } else {
3362 print "<th>" .
3363 $cgi->a({-href => href(project=>undef, order=>'descr'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003364 -class => "header"}, "Description") .
Petr Baudise30496d2006-10-24 05:33:17 +02003365 "</th>\n";
3366 }
3367 if ($order eq "owner") {
3368 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3369 print "<th>Owner</th>\n";
3370 } else {
3371 print "<th>" .
3372 $cgi->a({-href => href(project=>undef, order=>'owner'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003373 -class => "header"}, "Owner") .
Petr Baudise30496d2006-10-24 05:33:17 +02003374 "</th>\n";
3375 }
3376 if ($order eq "age") {
3377 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3378 print "<th>Last Change</th>\n";
3379 } else {
3380 print "<th>" .
3381 $cgi->a({-href => href(project=>undef, order=>'age'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003382 -class => "header"}, "Last Change") .
Petr Baudise30496d2006-10-24 05:33:17 +02003383 "</th>\n";
3384 }
3385 print "<th></th>\n" .
3386 "</tr>\n";
3387 }
3388 my $alternate = 1;
3389 for (my $i = $from; $i <= $to; $i++) {
3390 my $pr = $projects[$i];
3391 if ($alternate) {
3392 print "<tr class=\"dark\">\n";
3393 } else {
3394 print "<tr class=\"light\">\n";
3395 }
3396 $alternate ^= 1;
3397 if ($check_forks) {
3398 print "<td>";
3399 if ($pr->{'forks'}) {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003400 print "<!-- $pr->{'forks'} -->\n";
Petr Baudise30496d2006-10-24 05:33:17 +02003401 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3402 }
3403 print "</td>\n";
3404 }
3405 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3406 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003407 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3408 -class => "list", -title => $pr->{'descr_long'}},
3409 esc_html($pr->{'descr'})) . "</td>\n" .
David Symondsd3cd2492007-10-23 11:31:23 +10003410 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
Petr Baudise30496d2006-10-24 05:33:17 +02003411 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
Jakub Narebski785cdea2007-05-13 12:39:22 +02003412 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
Petr Baudise30496d2006-10-24 05:33:17 +02003413 "<td class=\"link\">" .
3414 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
Alexandre Julliardfaa1bbf2006-11-15 21:37:50 +01003415 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
Petr Baudise30496d2006-10-24 05:33:17 +02003416 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3417 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3418 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3419 "</td>\n" .
3420 "</tr>\n";
3421 }
3422 if (defined $extra) {
3423 print "<tr>\n";
3424 if ($check_forks) {
3425 print "<td></td>\n";
3426 }
3427 print "<td colspan=\"5\">$extra</td>\n" .
3428 "</tr>\n";
3429 }
3430 print "</table>\n";
3431}
3432
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003433sub git_shortlog_body {
3434 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003435 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303436
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003437 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003438 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003439
3440 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003441 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003442 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003443 my %co = %{$commitlist->[$i]};
3444 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02003445 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003446 if ($alternate) {
3447 print "<tr class=\"dark\">\n";
3448 } else {
3449 print "<tr class=\"light\">\n";
3450 }
3451 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10003452 my $author = chop_and_escape_str($co{'author_name'}, 10);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003453 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3454 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10003455 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003456 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003457 print format_subject_html($co{'title'}, $co{'title_short'},
3458 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003459 print "</td>\n" .
3460 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02003461 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02003462 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02003463 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02003464 my $snapshot_links = format_snapshot_links($commit);
3465 if (defined $snapshot_links) {
3466 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02003467 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05303468 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003469 "</tr>\n";
3470 }
3471 if (defined $extra) {
3472 print "<tr>\n" .
3473 "<td colspan=\"4\">$extra</td>\n" .
3474 "</tr>\n";
3475 }
3476 print "</table>\n";
3477}
3478
Jakub Narebski581860e2006-08-14 02:09:08 +02003479sub git_history_body {
3480 # Warning: assumes constant type (blob or tree) during history
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003481 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02003482
3483 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003484 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02003485
3486 print "<table class=\"history\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003487 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02003488 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003489 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02003490 if (!%co) {
3491 next;
3492 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003493 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02003494
3495 my $ref = format_ref_marker($refs, $commit);
3496
3497 if ($alternate) {
3498 print "<tr class=\"dark\">\n";
3499 } else {
3500 print "<tr class=\"light\">\n";
3501 }
3502 $alternate ^= 1;
David Symondse076a0e2007-10-22 10:28:03 +10003503 # shortlog uses chop_str($co{'author_name'}, 10)
David Symondsce58ec92007-10-23 11:31:22 +10003504 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
Jakub Narebski581860e2006-08-14 02:09:08 +02003505 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10003506 "<td><i>" . $author . "</i></td>\n" .
Jakub Narebski581860e2006-08-14 02:09:08 +02003507 "<td>";
3508 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02003509 print format_subject_html($co{'title'}, $co{'title_short'},
3510 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02003511 print "</td>\n" .
3512 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07003513 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3514 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02003515
3516 if ($ftype eq 'blob') {
3517 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3518 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3519 if (defined $blob_current && defined $blob_parent &&
3520 $blob_current ne $blob_parent) {
3521 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02003522 $cgi->a({-href => href(action=>"blobdiff",
3523 hash=>$blob_current, hash_parent=>$blob_parent,
3524 hash_base=>$hash_base, hash_parent_base=>$commit,
3525 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02003526 "diff to current");
3527 }
3528 }
3529 print "</td>\n" .
3530 "</tr>\n";
3531 }
3532 if (defined $extra) {
3533 print "<tr>\n" .
3534 "<td colspan=\"4\">$extra</td>\n" .
3535 "</tr>\n";
3536 }
3537 print "</table>\n";
3538}
3539
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003540sub git_tags_body {
3541 # uses global variable $project
3542 my ($taglist, $from, $to, $extra) = @_;
3543 $from = 0 unless defined $from;
3544 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3545
3546 print "<table class=\"tags\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003547 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003548 for (my $i = $from; $i <= $to; $i++) {
3549 my $entry = $taglist->[$i];
3550 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01003551 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003552 my $comment_short;
3553 if (defined $comment) {
3554 $comment_short = chop_str($comment, 30, 5);
3555 }
3556 if ($alternate) {
3557 print "<tr class=\"dark\">\n";
3558 } else {
3559 print "<tr class=\"light\">\n";
3560 }
3561 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01003562 if (defined $tag{'age'}) {
3563 print "<td><i>$tag{'age'}</i></td>\n";
3564 } else {
3565 print "<td></td>\n";
3566 }
3567 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003568 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02003569 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003570 "</td>\n" .
3571 "<td>";
3572 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02003573 print format_subject_html($comment, $comment_short,
3574 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003575 }
3576 print "</td>\n" .
3577 "<td class=\"selflink\">";
3578 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003579 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003580 } else {
3581 print "&nbsp;";
3582 }
3583 print "</td>\n" .
3584 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003585 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003586 if ($tag{'reftype'} eq "commit") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003587 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
Jakub Narebskicd146402006-11-02 20:23:11 +01003588 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003589 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003590 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003591 }
3592 print "</td>\n" .
3593 "</tr>";
3594 }
3595 if (defined $extra) {
3596 print "<tr>\n" .
3597 "<td colspan=\"5\">$extra</td>\n" .
3598 "</tr>\n";
3599 }
3600 print "</table>\n";
3601}
3602
3603sub git_heads_body {
3604 # uses global variable $project
Jakub Narebski120ddde2006-09-19 14:33:22 +02003605 my ($headlist, $head, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003606 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003607 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003608
3609 print "<table class=\"heads\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003610 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003611 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02003612 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01003613 my %ref = %$entry;
3614 my $curr = $ref{'id'} eq $head;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003615 if ($alternate) {
3616 print "<tr class=\"dark\">\n";
3617 } else {
3618 print "<tr class=\"light\">\n";
3619 }
3620 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01003621 print "<td><i>$ref{'age'}</i></td>\n" .
3622 ($curr ? "<td class=\"current_head\">" : "<td>") .
3623 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3624 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003625 "</td>\n" .
3626 "<td class=\"link\">" .
Jakub Narebskicd146402006-11-02 20:23:11 +01003627 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3628 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3629 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003630 "</td>\n" .
3631 "</tr>";
3632 }
3633 if (defined $extra) {
3634 print "<tr>\n" .
3635 "<td colspan=\"3\">$extra</td>\n" .
3636 "</tr>\n";
3637 }
3638 print "</table>\n";
3639}
3640
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003641sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003642 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003643 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003644 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003645
3646 print "<table class=\"grep\" cellspacing=\"0\">\n";
3647 my $alternate = 1;
3648 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003649 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003650 if (!%co) {
3651 next;
3652 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003653 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003654 if ($alternate) {
3655 print "<tr class=\"dark\">\n";
3656 } else {
3657 print "<tr class=\"light\">\n";
3658 }
3659 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10003660 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003661 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10003662 "<td><i>" . $author . "</i></td>\n" .
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003663 "<td>" .
3664 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
David Symondsd3cd2492007-10-23 11:31:23 +10003665 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003666 my $comment = $co{'comment'};
3667 foreach my $line (@$comment) {
Jakub Narebski7e431ef2007-05-16 01:56:10 +02003668 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003669 my $lead = esc_html($1) || "";
3670 $lead = chop_str($lead, 30, 10);
3671 my $match = esc_html($2) || "";
3672 my $trail = esc_html($3) || "";
3673 $trail = chop_str($trail, 30, 10);
3674 my $text = "$lead<span class=\"match\">$match</span>$trail";
3675 print chop_str($text, 80, 5) . "<br/>\n";
3676 }
3677 }
3678 print "</td>\n" .
3679 "<td class=\"link\">" .
3680 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3681 " | " .
3682 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3683 print "</td>\n" .
3684 "</tr>\n";
3685 }
3686 if (defined $extra) {
3687 print "<tr>\n" .
3688 "<td colspan=\"3\">$extra</td>\n" .
3689 "</tr>\n";
3690 }
3691 print "</table>\n";
3692}
3693
Jakub Narebski717b8312006-07-31 21:22:15 +02003694## ======================================================================
3695## ======================================================================
3696## actions
3697
Jakub Narebski717b8312006-07-31 21:22:15 +02003698sub git_project_list {
Jakub Narebski6326b602006-08-01 02:59:12 +02003699 my $order = $cgi->param('o');
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003700 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Jakub Narebskie2860ea2006-08-05 13:15:24 +02003701 die_error(undef, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02003702 }
3703
Jakub Narebski847e01f2006-08-14 02:05:47 +02003704 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02003705 if (!@list) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003706 die_error(undef, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02003707 }
Jakub Narebski6326b602006-08-01 02:59:12 +02003708
Jakub Narebski717b8312006-07-31 21:22:15 +02003709 git_header_html();
3710 if (-f $home_text) {
3711 print "<div class=\"index_include\">\n";
3712 open (my $fd, $home_text);
3713 print <$fd>;
3714 close $fd;
3715 print "</div>\n";
3716 }
Petr Baudise30496d2006-10-24 05:33:17 +02003717 git_project_list_body(\@list, $order);
3718 git_footer_html();
3719}
3720
3721sub git_forks {
3722 my $order = $cgi->param('o');
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003723 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Petr Baudise30496d2006-10-24 05:33:17 +02003724 die_error(undef, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02003725 }
Petr Baudise30496d2006-10-24 05:33:17 +02003726
3727 my @list = git_get_projects_list($project);
3728 if (!@list) {
3729 die_error(undef, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02003730 }
Petr Baudise30496d2006-10-24 05:33:17 +02003731
3732 git_header_html();
3733 git_print_page_nav('','');
3734 git_print_header_div('summary', "$project forks");
3735 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02003736 git_footer_html();
3737}
3738
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003739sub git_project_index {
Petr Baudise30496d2006-10-24 05:33:17 +02003740 my @projects = git_get_projects_list($project);
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003741
3742 print $cgi->header(
3743 -type => 'text/plain',
3744 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02003745 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003746
3747 foreach my $pr (@projects) {
3748 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003749 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003750 }
3751
3752 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3753 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3754 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3755 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3756 $path =~ s/ /\+/g;
3757 $owner =~ s/ /\+/g;
3758
3759 print "$path $owner\n";
3760 }
3761}
3762
Kay Sieversede5e102005-08-07 20:23:12 +02003763sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003764 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00003765 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02003766 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00003767 my $head = $co{'id'};
Kay Sieversede5e102005-08-07 20:23:12 +02003768
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003769 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02003770
Jakub Narebskicd146402006-11-02 20:23:11 +01003771 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003772 # These get_*_list functions return one more to allow us to see if
3773 # there are more ...
3774 my @taglist = git_get_tags_list(16);
3775 my @headlist = git_get_heads_list(16);
Petr Baudise30496d2006-10-24 05:33:17 +02003776 my @forklist;
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08003777 my ($check_forks) = gitweb_check_feature('forks');
3778
3779 if ($check_forks) {
Petr Baudise30496d2006-10-24 05:33:17 +02003780 @forklist = git_get_projects_list($project);
3781 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02003782
Kay Sieversede5e102005-08-07 20:23:12 +02003783 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003784 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003785
Kay Sievers19806692005-08-07 20:26:27 +02003786 print "<div class=\"title\">&nbsp;</div>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02003787 print "<table cellspacing=\"0\">\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003788 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
Jakub Narebskice312af2007-08-28 16:05:43 +02003789 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
Jakub Narebski785cdea2007-05-13 12:39:22 +02003790 if (defined $cd{'rfc2822'}) {
3791 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3792 }
3793
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003794 # use per project git URL list in $projectroot/$project/cloneurl
3795 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02003796 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003797 my @url_list = git_get_project_url_list($project);
3798 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3799 foreach my $git_url (@url_list) {
3800 next unless $git_url;
3801 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02003802 $url_tag = "";
3803 }
3804 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003805
Petr Baudis447ef092006-10-24 05:23:46 +02003806 if (-s "$projectroot/$project/README.html") {
3807 if (open my $fd, "$projectroot/$project/README.html") {
3808 print "<div class=\"title\">readme</div>\n";
3809 print $_ while (<$fd>);
3810 close $fd;
3811 }
3812 }
3813
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003814 # we need to request one more than 16 (0..15) to check if
3815 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02003816 my @commitlist = $head ? parse_commits($head, 17) : ();
3817 if (@commitlist) {
3818 git_print_header_div('shortlog');
3819 git_shortlog_body(\@commitlist, 0, 15, $refs,
3820 $#commitlist <= 15 ? undef :
3821 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3822 }
Kay Sieversede5e102005-08-07 20:23:12 +02003823
Jakub Narebski120ddde2006-09-19 14:33:22 +02003824 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003825 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02003826 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003827 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003828 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02003829 }
Kay Sievers0db37972005-08-07 20:24:35 +02003830
Jakub Narebski120ddde2006-09-19 14:33:22 +02003831 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003832 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02003833 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003834 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003835 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02003836 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003837
Petr Baudise30496d2006-10-24 05:33:17 +02003838 if (@forklist) {
3839 git_print_header_div('forks');
3840 git_project_list_body(\@forklist, undef, 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00003841 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02003842 $cgi->a({-href => href(action=>"forks")}, "..."),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003843 'noheader');
Petr Baudise30496d2006-10-24 05:33:17 +02003844 }
3845
Kay Sieversede5e102005-08-07 20:23:12 +02003846 git_footer_html();
3847}
3848
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003849sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003850 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003851 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003852 git_print_page_nav('','', $head,undef,$head);
3853 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02003854
3855 if (! %tag) {
3856 die_error(undef, "Unknown tag object");
3857 }
3858
Jakub Narebski847e01f2006-08-14 02:05:47 +02003859 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003860 print "<div class=\"title_text\">\n" .
3861 "<table cellspacing=\"0\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02003862 "<tr>\n" .
3863 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003864 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3865 $tag{'object'}) . "</td>\n" .
3866 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3867 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02003868 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003869 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003870 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01003871 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003872 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3873 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3874 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003875 }
3876 print "</table>\n\n" .
3877 "</div>\n";
3878 print "<div class=\"page_body\">";
3879 my $comment = $tag{'comment'};
3880 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08003881 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01003882 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003883 }
3884 print "</div>\n";
3885 git_footer_html();
3886}
3887
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003888sub git_blame2 {
3889 my $fd;
3890 my $ftype;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303891
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05303892 my ($have_blame) = gitweb_check_feature('blame');
3893 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303894 die_error('403 Permission denied', "Permission denied");
3895 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003896 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003897 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003898 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003899 my %co = parse_commit($hash_base)
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003900 or die_error(undef, "Reading commit failed");
3901 if (!defined $hash) {
3902 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3903 or die_error(undef, "Error looking up file");
3904 }
3905 $ftype = git_get_type($hash);
3906 if ($ftype !~ "blob") {
Jakub Narebskif73bbb22007-03-27 02:07:11 +02003907 die_error('400 Bad Request', "Object is not a blob");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003908 }
Junio C Hamano48fd6882006-10-12 00:47:03 -07003909 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003910 $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003911 or die_error(undef, "Open git-blame failed");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003912 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003913 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01003914 $cgi->a({-href => href(action=>"blob", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02003915 "blob") .
3916 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01003917 $cgi->a({-href => href(action=>"history", -replay=>1)},
3918 "history") .
Petr Baudiscae18622006-09-22 03:19:41 +02003919 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003920 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02003921 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02003922 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3923 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003924 git_print_page_path($file_name, $ftype, $hash_base);
Luben Tuikov82f930d2006-08-04 15:09:59 -07003925 my @rev_color = (qw(light2 dark2));
Luben Tuikovcc1bf972006-07-23 13:37:53 -07003926 my $num_colors = scalar(@rev_color);
3927 my $current_color = 0;
3928 my $last_rev;
Jakub Narebski59b9f612006-08-22 23:42:53 +02003929 print <<HTML;
3930<div class="page_body">
3931<table class="blame">
3932<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3933HTML
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003934 my %metainfo = ();
3935 while (1) {
3936 $_ = <$fd>;
3937 last unless defined $_;
Luben Tuikovd15c55a2006-10-11 00:30:05 -07003938 my ($full_rev, $orig_lineno, $lineno, $group_size) =
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003939 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3940 if (!exists $metainfo{$full_rev}) {
3941 $metainfo{$full_rev} = {};
3942 }
3943 my $meta = $metainfo{$full_rev};
3944 while (<$fd>) {
3945 last if (s/^\t//);
3946 if (/^(\S+) (.*)$/) {
3947 $meta->{$1} = $2;
3948 }
3949 }
3950 my $data = $_;
Junio C Hamano70022432006-11-24 14:04:01 -08003951 chomp $data;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003952 my $rev = substr($full_rev, 0, 8);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003953 my $author = $meta->{'author'};
3954 my %date = parse_date($meta->{'author-time'},
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003955 $meta->{'author-tz'});
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003956 my $date = $date{'iso-tz'};
3957 if ($group_size) {
Luben Tuikovcc1bf972006-07-23 13:37:53 -07003958 $current_color = ++$current_color % $num_colors;
3959 }
3960 print "<tr class=\"$rev_color[$current_color]\">\n";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003961 if ($group_size) {
3962 print "<td class=\"sha1\"";
Luben Tuikov5ad08282006-10-30 12:37:54 -08003963 print " title=\"". esc_html($author) . ", $date\"";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003964 print " rowspan=\"$group_size\"" if ($group_size > 1);
3965 print ">";
3966 print $cgi->a({-href => href(action=>"commit",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003967 hash=>$full_rev,
3968 file_name=>$file_name)},
3969 esc_html($rev));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003970 print "</td>\n";
Luben Tuikov9dc5f8c2006-10-04 00:12:17 -07003971 }
Luben Tuikov244a70e2007-01-04 18:37:45 -08003972 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
Jakub Narebskif73bbb22007-03-27 02:07:11 +02003973 or die_error(undef, "Open git-rev-parse failed");
Luben Tuikov244a70e2007-01-04 18:37:45 -08003974 my $parent_commit = <$dd>;
3975 close $dd;
3976 chomp($parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003977 my $blamed = href(action => 'blame',
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003978 file_name => $meta->{'filename'},
3979 hash_base => $parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003980 print "<td class=\"linenr\">";
3981 print $cgi->a({ -href => "$blamed#l$orig_lineno",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003982 -id => "l$lineno",
3983 -class => "linenr" },
3984 esc_html($lineno));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003985 print "</td>";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003986 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3987 print "</tr>\n";
3988 }
3989 print "</table>\n";
3990 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003991 close $fd
3992 or print "Reading blob failed\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003993 git_footer_html();
3994}
3995
Florian Forstere34ef622006-06-11 17:45:19 +02003996sub git_blame {
3997 my $fd;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303998
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05303999 my ($have_blame) = gitweb_check_feature('blame');
4000 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304001 die_error('403 Permission denied', "Permission denied");
4002 }
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004003 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02004004 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004005 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02004006 my %co = parse_commit($hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004007 or die_error(undef, "Reading commit failed");
Florian Forstere34ef622006-06-11 17:45:19 +02004008 if (!defined $hash) {
4009 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004010 or die_error(undef, "Error lookup file");
Florian Forstere34ef622006-06-11 17:45:19 +02004011 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004012 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004013 or die_error(undef, "Open git-annotate failed");
Florian Forstere34ef622006-06-11 17:45:19 +02004014 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004015 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02004016 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4017 "blob") .
4018 " | " .
Petr Baudiscae18622006-09-22 03:19:41 +02004019 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4020 "history") .
4021 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004022 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004023 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004024 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4025 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004026 git_print_page_path($file_name, 'blob', $hash_base);
Florian Forstere34ef622006-06-11 17:45:19 +02004027 print "<div class=\"page_body\">\n";
4028 print <<HTML;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004029<table class="blame">
Florian Forstere34ef622006-06-11 17:45:19 +02004030 <tr>
4031 <th>Commit</th>
4032 <th>Age</th>
4033 <th>Author</th>
4034 <th>Line</th>
4035 <th>Data</th>
4036 </tr>
4037HTML
4038 my @line_class = (qw(light dark));
4039 my $line_class_len = scalar (@line_class);
4040 my $line_class_num = $#line_class;
4041 while (my $line = <$fd>) {
4042 my $long_rev;
4043 my $short_rev;
4044 my $author;
4045 my $time;
4046 my $lineno;
4047 my $data;
4048 my $age;
4049 my $age_str;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004050 my $age_class;
Florian Forstere34ef622006-06-11 17:45:19 +02004051
4052 chomp $line;
4053 $line_class_num = ($line_class_num + 1) % $line_class_len;
4054
Jakub Narebski030b5202006-08-26 02:13:05 +02004055 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
Florian Forstere34ef622006-06-11 17:45:19 +02004056 $long_rev = $1;
4057 $author = $2;
4058 $time = $3;
4059 $lineno = $4;
4060 $data = $5;
4061 } else {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004062 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
Florian Forstere34ef622006-06-11 17:45:19 +02004063 next;
4064 }
4065 $short_rev = substr ($long_rev, 0, 8);
4066 $age = time () - $time;
4067 $age_str = age_string ($age);
4068 $age_str =~ s/ /&nbsp;/g;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004069 $age_class = age_class($age);
Florian Forstere34ef622006-06-11 17:45:19 +02004070 $author = esc_html ($author);
4071 $author =~ s/ /&nbsp;/g;
Jakub Narebskif16db172006-08-06 02:08:31 +02004072
4073 $data = untabify($data);
Florian Forstere34ef622006-06-11 17:45:19 +02004074 $data = esc_html ($data);
Florian Forstere34ef622006-06-11 17:45:19 +02004075
4076 print <<HTML;
4077 <tr class="$line_class[$line_class_num]">
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004078 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004079 <td class="$age_class">$age_str</td>
Florian Forstere34ef622006-06-11 17:45:19 +02004080 <td>$author</td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004081 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4082 <td class="pre">$data</td>
Florian Forstere34ef622006-06-11 17:45:19 +02004083 </tr>
4084HTML
4085 } # while (my $line = <$fd>)
4086 print "</table>\n\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004087 close $fd
4088 or print "Reading blob failed.\n";
Florian Forstere34ef622006-06-11 17:45:19 +02004089 print "</div>";
4090 git_footer_html();
4091}
4092
Kay Sieversede5e102005-08-07 20:23:12 +02004093sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004094 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004095 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004096 git_print_page_nav('','', $head,undef,$head);
4097 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004098
Jakub Narebskicd146402006-11-02 20:23:11 +01004099 my @tagslist = git_get_tags_list();
4100 if (@tagslist) {
4101 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02004102 }
Kay Sieversede5e102005-08-07 20:23:12 +02004103 git_footer_html();
4104}
4105
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02004106sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004107 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004108 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004109 git_print_page_nav('','', $head,undef,$head);
4110 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004111
Jakub Narebskicd146402006-11-02 20:23:11 +01004112 my @headslist = git_get_heads_list();
4113 if (@headslist) {
4114 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02004115 }
Kay Sievers0db37972005-08-07 20:24:35 +02004116 git_footer_html();
4117}
4118
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004119sub git_blob_plain {
Jakub Narebskif2e73302006-08-26 19:14:25 +02004120 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004121
Luben Tuikovcff07712006-07-23 13:28:55 -07004122 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004123 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004124 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004125 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004126 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004127 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004128 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004129 }
Martin Waitz800764c2006-09-16 23:09:02 +02004130 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4131 # blobs defined by non-textual hash id's can be cached
4132 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004133 }
Martin Waitz800764c2006-09-16 23:09:02 +02004134
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004135 my $type = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004136 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicfd82662006-08-05 12:56:04 +02004137 or die_error(undef, "Couldn't cat $file_name, $hash");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004138
Jakub Narebski847e01f2006-08-14 02:05:47 +02004139 $type ||= blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004140
4141 # save as filename, even when no $file_name is given
4142 my $save_as = "$hash";
4143 if (defined $file_name) {
4144 $save_as = $file_name;
4145 } elsif ($type =~ m/^text\//) {
4146 $save_as .= '.txt';
4147 }
4148
Jakub Narebskif2e73302006-08-26 19:14:25 +02004149 print $cgi->header(
4150 -type => "$type",
4151 -expires=>$expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004152 -content_disposition => 'inline; filename="' . "$save_as" . '"');
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004153 undef $/;
4154 binmode STDOUT, ':raw';
4155 print <$fd>;
4156 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4157 $/ = "\n";
4158 close $fd;
4159}
4160
Kay Sievers09bd7892005-08-07 20:21:23 +02004161sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02004162 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004163
Luben Tuikovcff07712006-07-23 13:28:55 -07004164 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004165 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004166 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004167 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004168 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004169 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004170 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004171 }
Martin Waitz800764c2006-09-16 23:09:02 +02004172 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4173 # blobs defined by non-textual hash id's can be cached
4174 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004175 }
Martin Waitz800764c2006-09-16 23:09:02 +02004176
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05304177 my ($have_blame) = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004178 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004179 or die_error(undef, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004180 my $mimetype = blob_mimetype($fd, $file_name);
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004181 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004182 close $fd;
4183 return git_blob_plain($mimetype);
4184 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004185 # we can have blame only for text/* mimetype
4186 $have_blame &&= ($mimetype =~ m!^text/!);
4187
Jakub Narebskif2e73302006-08-26 19:14:25 +02004188 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004189 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02004190 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02004191 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02004192 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004193 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004194 $cgi->a({-href => href(action=>"blame", -replay=>1)},
Jakub Narebski952c65f2006-08-22 16:52:50 +02004195 "blame") .
4196 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02004197 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004198 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004199 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004200 "history") .
4201 " | " .
Jakub Narebskia3823e52007-11-01 13:06:29 +01004202 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004203 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004204 " | " .
4205 $cgi->a({-href => href(action=>"blob",
4206 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004207 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02004208 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004209 $formats_nav .=
Jakub Narebskia3823e52007-11-01 13:06:29 +01004210 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4211 "raw");
Kay Sievers93129442005-10-17 03:27:54 +02004212 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004213 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4214 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004215 } else {
4216 print "<div class=\"page_nav\">\n" .
4217 "<br/><br/></div>\n" .
4218 "<div class=\"title\">$hash</div>\n";
4219 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004220 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02004221 print "<div class=\"page_body\">\n";
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004222 if ($mimetype =~ m!^text/!) {
4223 my $nr;
4224 while (my $line = <$fd>) {
4225 chomp $line;
4226 $nr++;
4227 $line = untabify($line);
4228 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4229 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4230 }
4231 } elsif ($mimetype =~ m!^image/!) {
4232 print qq!<img type="$mimetype"!;
4233 if ($file_name) {
4234 print qq! alt="$file_name" title="$file_name"!;
4235 }
4236 print qq! src="! .
4237 href(action=>"blob_plain", hash=>$hash,
4238 hash_base=>$hash_base, file_name=>$file_name) .
4239 qq!" />\n!;
Kay Sievers161332a2005-08-07 19:49:46 +02004240 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02004241 close $fd
4242 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02004243 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004244 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004245}
4246
4247sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004248 if (!defined $hash_base) {
4249 $hash_base = "HEAD";
4250 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02004251 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02004252 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004253 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4254 } else {
4255 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02004256 }
Kay Sieverse925f382005-08-07 20:23:35 +02004257 }
Kay Sievers232ff552005-11-24 16:56:55 +01004258 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004259 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004260 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02004261 my @entries = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004262 close $fd or die_error(undef, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01004263 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02004264
Jakub Narebski847e01f2006-08-14 02:05:47 +02004265 my $refs = git_get_references();
4266 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02004267 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02004268 my $basedir = '';
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05304269 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02004270 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02004271 my @views_nav = ();
4272 if (defined $file_name) {
4273 push @views_nav,
Jakub Narebskia3823e52007-11-01 13:06:29 +01004274 $cgi->a({-href => href(action=>"history", -replay=>1)},
Petr Baudiscae18622006-09-22 03:19:41 +02004275 "history"),
4276 $cgi->a({-href => href(action=>"tree",
4277 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004278 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02004279 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004280 my $snapshot_links = format_snapshot_links($hash);
4281 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02004282 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004283 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02004284 }
4285 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02004286 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02004287 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02004288 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02004289 print "<div class=\"page_nav\">\n";
4290 print "<br/><br/></div>\n";
4291 print "<div class=\"title\">$hash</div>\n";
4292 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004293 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02004294 $basedir = $file_name;
4295 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4296 $basedir .= '/';
4297 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004298 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004299 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sieversfbb592a2005-08-07 20:12:11 +02004300 print "<div class=\"page_body\">\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004301 print "<table cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004302 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02004303 # '..' (top directory) link if possible
4304 if (defined $hash_base &&
4305 defined $file_name && $file_name =~ m![^/]+$!) {
4306 if ($alternate) {
4307 print "<tr class=\"dark\">\n";
4308 } else {
4309 print "<tr class=\"light\">\n";
4310 }
4311 $alternate ^= 1;
4312
4313 my $up = $file_name;
4314 $up =~ s!/?[^/]+$!!;
4315 undef $up unless $up;
4316 # based on git_print_tree_entry
4317 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4318 print '<td class="list">';
4319 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4320 file_name=>$up)},
4321 "..");
4322 print "</td>\n";
4323 print "<td class=\"link\"></td>\n";
4324
4325 print "</tr>\n";
4326 }
Kay Sievers161332a2005-08-07 19:49:46 +02004327 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02004328 my %t = parse_ls_tree_line($line, -z => 1);
4329
Kay Sieversbddec012005-08-07 20:25:42 +02004330 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02004331 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004332 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02004333 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004334 }
4335 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02004336
Jakub Narebski300454f2006-10-21 17:53:09 +02004337 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02004338
Kay Sievers42f7eb92005-08-07 20:21:46 +02004339 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02004340 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02004341 print "</table>\n" .
4342 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004343 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004344}
4345
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304346sub git_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004347 my @supported_fmts = gitweb_check_feature('snapshot');
Jakub Narebskia7817852007-07-22 23:41:20 +02004348 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004349
4350 my $format = $cgi->param('sf');
Jakub Narebski3473e7d2007-07-25 01:19:58 +02004351 if (!@supported_fmts) {
4352 die_error('403 Permission denied', "Permission denied");
4353 }
4354 # default to first supported snapshot format
4355 $format ||= $supported_fmts[0];
4356 if ($format !~ m/^[a-z0-9]+$/) {
4357 die_error(undef, "Invalid snapshot format parameter");
4358 } elsif (!exists($known_snapshot_formats{$format})) {
4359 die_error(undef, "Unknown snapshot format");
4360 } elsif (!grep($_ eq $format, @supported_fmts)) {
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004361 die_error(undef, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304362 }
4363
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304364 if (!defined $hash) {
4365 $hash = git_get_head_hash($project);
4366 }
4367
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004368 my $git_command = git_cmd_str();
Mark Levedahl072570e2007-05-20 11:46:46 -04004369 my $name = $project;
Matthias Lederhofer9a7d9412007-06-07 11:27:08 +02004370 $name =~ s,([^/])/*\.git$,$1,;
4371 $name = basename($name);
4372 my $filename = to_utf8($name);
Mark Levedahl072570e2007-05-20 11:46:46 -04004373 $name =~ s/\047/\047\\\047\047/g;
Mark Levedahl072570e2007-05-20 11:46:46 -04004374 my $cmd;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004375 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4376 $cmd = "$git_command archive " .
Junio C Hamano887c5262007-07-25 15:49:55 -07004377 "--format=$known_snapshot_formats{$format}{'format'} " .
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004378 "--prefix=\'$name\'/ $hash";
4379 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4380 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
Mark Levedahl072570e2007-05-20 11:46:46 -04004381 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304382
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004383 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004384 -type => $known_snapshot_formats{$format}{'type'},
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004385 -content_disposition => 'inline; filename="' . "$filename" . '"',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004386 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304387
Mark Levedahl072570e2007-05-20 11:46:46 -04004388 open my $fd, "-|", $cmd
4389 or die_error(undef, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304390 binmode STDOUT, ':raw';
4391 print <$fd>;
4392 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4393 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304394}
4395
Kay Sievers09bd7892005-08-07 20:21:23 +02004396sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004397 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004398 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02004399 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02004400 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02004401 if (!defined $page) {
4402 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02004403 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004404 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02004405
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004406 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02004407
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004408 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004409
4410 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004411 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004412
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004413 if (!@commitlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004414 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004415
Jakub Narebski847e01f2006-08-14 02:05:47 +02004416 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02004417 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02004418 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004419 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4420 for (my $i = 0; $i <= $to; $i++) {
4421 my %co = %{$commitlist[$i]};
Kay Sieversb87d78d2005-08-07 20:21:04 +02004422 next if !%co;
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004423 my $commit = $co{'id'};
4424 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski847e01f2006-08-14 02:05:47 +02004425 my %ad = parse_date($co{'author_epoch'});
4426 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02004427 "<span class=\"age\">$co{'age_string'}</span>" .
4428 esc_html($co{'title'}) . $ref,
4429 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02004430 print "<div class=\"title_text\">\n" .
4431 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004432 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004433 " | " .
4434 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Petr Baudis6ef4cb22006-09-22 03:19:48 +02004435 " | " .
Petr Baudisd7267202006-09-22 16:56:43 -07004436 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
Kay Sieverseb282402005-08-07 20:21:34 +02004437 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02004438 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01004439 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02004440 "</div>\n";
4441
4442 print "<div class=\"log_body\">\n";
Jakub Narebskif2069412006-10-24 13:52:46 +02004443 git_print_log($co{'comment'}, -final_empty_line=> 1);
Kay Sievers09bd7892005-08-07 20:21:23 +02004444 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02004445 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004446 if ($#commitlist >= 100) {
4447 print "<div class=\"page_nav\">\n";
Jakub Narebski7afd77b2007-11-01 13:06:28 +01004448 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004449 -accesskey => "n", -title => "Alt-n"}, "next");
4450 print "</div>\n";
4451 }
Kay Sievers034df392005-08-07 20:20:07 +02004452 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004453}
4454
4455sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01004456 $hash ||= $hash_base || "HEAD";
Jakub Narebski847e01f2006-08-14 02:05:47 +02004457 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02004458 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004459 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02004460 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004461 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4462 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02004463
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004464 my $parent = $co{'parent'};
4465 my $parents = $co{'parents'}; # listref
4466
4467 # we need to prepare $formats_nav before any parameter munging
4468 my $formats_nav;
4469 if (!defined $parent) {
4470 # --root commitdiff
4471 $formats_nav .= '(initial)';
4472 } elsif (@$parents == 1) {
4473 # single parent commit
4474 $formats_nav .=
4475 '(parent: ' .
4476 $cgi->a({-href => href(action=>"commit",
4477 hash=>$parent)},
4478 esc_html(substr($parent, 0, 7))) .
4479 ')';
4480 } else {
4481 # merge commit
4482 $formats_nav .=
4483 '(merge: ' .
4484 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01004485 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004486 hash=>$_)},
4487 esc_html(substr($_, 0, 7)));
4488 } @$parents ) .
4489 ')';
4490 }
4491
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004492 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07004493 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004494 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01004495 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02004496 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4497 @diff_opts,
4498 (@$parents <= 1 ? $parent : '-c'),
4499 $hash, "--"
4500 or die_error(undef, "Open git-diff-tree failed");
4501 @difftree = map { chomp; $_ } <$fd>;
4502 close $fd or die_error(undef, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02004503
4504 # non-textual hash id's can be cached
4505 my $expires;
4506 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4507 $expires = "+1d";
4508 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004509 my $refs = git_get_references();
4510 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304511
Jakub Narebski594e2122006-07-31 02:21:52 +02004512 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02004513 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02004514 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004515 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07004516
Kay Sieversb87d78d2005-08-07 20:21:04 +02004517 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004518 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004519 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004520 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004521 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02004522 print "<div class=\"title_text\">\n" .
Kay Sieversb87d78d2005-08-07 20:21:04 +02004523 "<table cellspacing=\"0\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01004524 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02004525 "<tr>" .
4526 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02004527 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004528 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4529 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02004530 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004531 printf(" (%02d:%02d %s)",
4532 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02004533 }
Kay Sieversbddec012005-08-07 20:25:42 +02004534 print "</td>" .
4535 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01004536 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004537 print "<tr><td></td><td> $cd{'rfc2822'}" .
4538 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4539 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004540 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004541 print "<tr>" .
4542 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004543 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004544 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4545 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02004546 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004547 "<td class=\"link\">" .
4548 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4549 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004550 my $snapshot_links = format_snapshot_links($hash);
4551 if (defined $snapshot_links) {
4552 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304553 }
4554 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02004555 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01004556
Kay Sievers3e029292005-08-07 20:05:15 +02004557 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02004558 print "<tr>" .
4559 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004560 "<td class=\"sha1\">" .
4561 $cgi->a({-href => href(action=>"commit", hash=>$par),
4562 class => "list"}, $par) .
4563 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02004564 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004565 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004566 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02004567 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02004568 "</td>" .
4569 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02004570 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02004571 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02004572 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02004573
Kay Sieversfbb592a2005-08-07 20:12:11 +02004574 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02004575 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02004576 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004577
Jakub Narebski208ecb22007-05-07 01:10:08 +02004578 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004579
Kay Sievers12a88f22005-08-07 20:02:47 +02004580 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004581}
4582
Jakub Narebskica946012006-12-10 13:25:47 +01004583sub git_object {
4584 # object is defined by:
4585 # - hash or hash_base alone
4586 # - hash_base and file_name
4587 my $type;
4588
4589 # - hash or hash_base alone
4590 if ($hash || ($hash_base && !defined $file_name)) {
4591 my $object_id = $hash || $hash_base;
4592
4593 my $git_command = git_cmd_str();
4594 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4595 or die_error('404 Not Found', "Object does not exist");
4596 $type = <$fd>;
4597 chomp $type;
4598 close $fd
4599 or die_error('404 Not Found', "Object does not exist");
4600
4601 # - hash_base and file_name
4602 } elsif ($hash_base && defined $file_name) {
4603 $file_name =~ s,/+$,,;
4604
4605 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4606 or die_error('404 Not Found', "Base object does not exist");
4607
4608 # here errors should not hapen
4609 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4610 or die_error(undef, "Open git-ls-tree failed");
4611 my $line = <$fd>;
4612 close $fd;
4613
4614 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4615 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4616 die_error('404 Not Found', "File or directory for given base does not exist");
4617 }
4618 $type = $2;
4619 $hash = $3;
4620 } else {
4621 die_error('404 Not Found', "Not enough information to find object");
4622 }
4623
4624 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4625 hash=>$hash, hash_base=>$hash_base,
4626 file_name=>$file_name),
4627 -status => '302 Found');
4628}
4629
Kay Sievers09bd7892005-08-07 20:21:23 +02004630sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004631 my $format = shift || 'html';
4632
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004633 my $fd;
4634 my @difftree;
4635 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004636 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004637
4638 # preparing $fd and %diffinfo for git_patchset_body
4639 # new style URI
4640 if (defined $hash_base && defined $hash_parent_base) {
4641 if (defined $file_name) {
4642 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004643 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4644 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02004645 "--", (defined $file_parent ? $file_parent : ()), $file_name
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004646 or die_error(undef, "Open git-diff-tree failed");
4647 @difftree = map { chomp; $_ } <$fd>;
4648 close $fd
4649 or die_error(undef, "Reading git-diff-tree failed");
4650 @difftree
4651 or die_error('404 Not Found', "Blob diff not found");
4652
Jakub Narebski0aea3372006-08-27 23:45:26 +02004653 } elsif (defined $hash &&
4654 $hash =~ /[0-9a-fA-F]{40}/) {
4655 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004656
4657 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004658 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4659 $hash_parent_base, $hash_base, "--"
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004660 or die_error(undef, "Open git-diff-tree failed");
4661 @difftree =
4662 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4663 # $hash == to_id
4664 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4665 map { chomp; $_ } <$fd>;
4666 close $fd
4667 or die_error(undef, "Reading git-diff-tree failed");
4668 @difftree
4669 or die_error('404 Not Found', "Blob diff not found");
4670
4671 } else {
4672 die_error('404 Not Found', "Missing one of the blob diff parameters");
4673 }
4674
4675 if (@difftree > 1) {
4676 die_error('404 Not Found', "Ambiguous blob diff specification");
4677 }
4678
4679 %diffinfo = parse_difftree_raw_line($difftree[0]);
Jakub Narebski9d301452007-11-01 12:38:08 +01004680 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4681 $file_name ||= $diffinfo{'to_file'};
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004682
4683 $hash_parent ||= $diffinfo{'from_id'};
4684 $hash ||= $diffinfo{'to_id'};
4685
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004686 # non-textual hash id's can be cached
4687 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4688 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4689 $expires = '+1d';
4690 }
4691
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004692 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004693 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02004694 '-p', ($format eq 'html' ? "--full-index" : ()),
4695 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02004696 "--", (defined $file_parent ? $file_parent : ()), $file_name
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004697 or die_error(undef, "Open git-diff-tree failed");
4698 }
4699
4700 # old/legacy style URI
4701 if (!%diffinfo && # if new style URI failed
4702 defined $hash && defined $hash_parent) {
4703 # fake git-diff-tree raw output
4704 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4705 $diffinfo{'from_id'} = $hash_parent;
4706 $diffinfo{'to_id'} = $hash;
4707 if (defined $file_name) {
4708 if (defined $file_parent) {
4709 $diffinfo{'status'} = '2';
Petr Baudis83915482006-09-24 14:57:40 -07004710 $diffinfo{'from_file'} = $file_parent;
4711 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004712 } else { # assume not renamed
4713 $diffinfo{'status'} = '1';
Petr Baudis83915482006-09-24 14:57:40 -07004714 $diffinfo{'from_file'} = $file_name;
4715 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004716 }
4717 } else { # no filename given
4718 $diffinfo{'status'} = '2';
4719 $diffinfo{'from_file'} = $hash_parent;
4720 $diffinfo{'to_file'} = $hash;
4721 }
4722
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004723 # non-textual hash id's can be cached
4724 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4725 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4726 $expires = '+1d';
4727 }
4728
4729 # open patch output
Jakub Narebski957d6ea2007-04-05 13:45:41 +02004730 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4731 '-p', ($format eq 'html' ? "--full-index" : ()),
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004732 $hash_parent, $hash, "--"
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004733 or die_error(undef, "Open git-diff failed");
4734 } else {
4735 die_error('404 Not Found', "Missing one of the blob diff parameters")
4736 unless %diffinfo;
4737 }
4738
4739 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004740 if ($format eq 'html') {
4741 my $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01004742 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004743 "raw");
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004744 git_header_html(undef, $expires);
4745 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4746 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4747 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4748 } else {
4749 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4750 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4751 }
4752 if (defined $file_name) {
4753 git_print_page_path($file_name, "blob", $hash_base);
4754 } else {
4755 print "<div class=\"page_path\"></div>\n";
4756 }
4757
4758 } elsif ($format eq 'plain') {
4759 print $cgi->header(
4760 -type => 'text/plain',
4761 -charset => 'utf-8',
4762 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004763 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004764
4765 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4766
Kay Sievers09bd7892005-08-07 20:21:23 +02004767 } else {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004768 die_error(undef, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004769 }
4770
4771 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004772 if ($format eq 'html') {
4773 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004774
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004775 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4776 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004777
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004778 print "</div>\n"; # class="page_body"
4779 git_footer_html();
4780
4781 } else {
4782 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01004783 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4784 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004785
4786 print $line;
4787
4788 last if $line =~ m!^\+\+\+!;
4789 }
4790 local $/ = undef;
4791 print <$fd>;
4792 close $fd;
4793 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004794}
4795
Kay Sievers19806692005-08-07 20:26:27 +02004796sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004797 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02004798}
4799
Kay Sievers09bd7892005-08-07 20:21:23 +02004800sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02004801 my $format = shift || 'html';
Jakub Narebski9954f772006-11-18 23:35:41 +01004802 $hash ||= $hash_base || "HEAD";
Jakub Narebski847e01f2006-08-14 02:05:47 +02004803 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02004804 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004805 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02004806 }
Jakub Narebski151602d2006-10-23 00:37:56 +02004807
Jakub Narebskicd030c32007-06-08 13:33:28 +02004808 # choose format for commitdiff for merge
4809 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4810 $hash_parent = '--cc';
4811 }
4812 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02004813 my $formats_nav;
4814 if ($format eq 'html') {
4815 $formats_nav =
Jakub Narebskia3823e52007-11-01 13:06:29 +01004816 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
Jakub Narebski151602d2006-10-23 00:37:56 +02004817 "raw");
4818
Jakub Narebskicd030c32007-06-08 13:33:28 +02004819 if (defined $hash_parent &&
4820 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02004821 # commitdiff with two commits given
4822 my $hash_parent_short = $hash_parent;
4823 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4824 $hash_parent_short = substr($hash_parent, 0, 7);
4825 }
4826 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02004827 ' (from';
4828 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4829 if ($co{'parents'}[$i] eq $hash_parent) {
4830 $formats_nav .= ' parent ' . ($i+1);
4831 last;
4832 }
4833 }
4834 $formats_nav .= ': ' .
Jakub Narebski151602d2006-10-23 00:37:56 +02004835 $cgi->a({-href => href(action=>"commitdiff",
4836 hash=>$hash_parent)},
4837 esc_html($hash_parent_short)) .
4838 ')';
4839 } elsif (!$co{'parent'}) {
4840 # --root commitdiff
4841 $formats_nav .= ' (initial)';
4842 } elsif (scalar @{$co{'parents'}} == 1) {
4843 # single parent commit
4844 $formats_nav .=
4845 ' (parent: ' .
4846 $cgi->a({-href => href(action=>"commitdiff",
4847 hash=>$co{'parent'})},
4848 esc_html(substr($co{'parent'}, 0, 7))) .
4849 ')';
4850 } else {
4851 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02004852 if ($hash_parent eq '--cc') {
4853 $formats_nav .= ' | ' .
4854 $cgi->a({-href => href(action=>"commitdiff",
4855 hash=>$hash, hash_parent=>'-c')},
4856 'combined');
4857 } else { # $hash_parent eq '-c'
4858 $formats_nav .= ' | ' .
4859 $cgi->a({-href => href(action=>"commitdiff",
4860 hash=>$hash, hash_parent=>'--cc')},
4861 'compact');
4862 }
Jakub Narebski151602d2006-10-23 00:37:56 +02004863 $formats_nav .=
4864 ' (merge: ' .
4865 join(' ', map {
4866 $cgi->a({-href => href(action=>"commitdiff",
4867 hash=>$_)},
4868 esc_html(substr($_, 0, 7)));
4869 } @{$co{'parents'}} ) .
4870 ')';
4871 }
4872 }
4873
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004874 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02004875 if (!defined $hash_parent_param) {
4876 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004877 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02004878 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02004879 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004880
4881 # read commitdiff
4882 my $fd;
4883 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02004884 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004885 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004886 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004887 $hash_parent_param, $hash, "--"
Jakub Narebskieee08902006-08-24 00:15:14 +02004888 or die_error(undef, "Open git-diff-tree failed");
4889
Jakub Narebski04408c32006-11-18 23:35:38 +01004890 while (my $line = <$fd>) {
4891 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02004892 # empty line ends raw part of diff-tree output
4893 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004894 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02004895 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004896
Jakub Narebskieee08902006-08-24 00:15:14 +02004897 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004898 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004899 '-p', $hash_parent_param, $hash, "--"
Jakub Narebskieee08902006-08-24 00:15:14 +02004900 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02004901
Jakub Narebskieee08902006-08-24 00:15:14 +02004902 } else {
4903 die_error(undef, "Unknown commitdiff format");
4904 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004905
Kay Sievers11044292005-10-19 03:18:45 +02004906 # non-textual hash id's can be cached
4907 my $expires;
4908 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4909 $expires = "+1d";
4910 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004911
Jakub Narebskieee08902006-08-24 00:15:14 +02004912 # write commit message
4913 if ($format eq 'html') {
4914 my $refs = git_get_references();
4915 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02004916
Jakub Narebskieee08902006-08-24 00:15:14 +02004917 git_header_html(undef, $expires);
4918 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4919 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004920 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02004921 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02004922 if (@{$co{'comment'}} > 1) {
4923 print "<div class=\"log\">\n";
4924 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4925 print "</div>\n"; # class="log"
4926 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02004927
Jakub Narebskieee08902006-08-24 00:15:14 +02004928 } elsif ($format eq 'plain') {
4929 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004930 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02004931 my $filename = basename($project) . "-$hash.patch";
4932
4933 print $cgi->header(
4934 -type => 'text/plain',
4935 -charset => 'utf-8',
4936 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004937 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02004938 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4939 print <<TEXT;
Jakub Narebski59b9f612006-08-22 23:42:53 +02004940From: $co{'author'}
4941Date: $ad{'rfc2822'} ($ad{'tz_local'})
4942Subject: $co{'title'}
4943TEXT
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004944 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02004945 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004946
Jakub Narebskieee08902006-08-24 00:15:14 +02004947 foreach my $line (@{$co{'comment'}}) {
4948 print "$line\n";
Kay Sievers19806692005-08-07 20:26:27 +02004949 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004950 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02004951 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004952
4953 # write patch
4954 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02004955 my $use_parents = !defined $hash_parent ||
4956 $hash_parent eq '-c' || $hash_parent eq '--cc';
4957 git_difftree_body(\@difftree, $hash,
4958 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02004959 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02004960
Jakub Narebskicd030c32007-06-08 13:33:28 +02004961 git_patchset_body($fd, \@difftree, $hash,
4962 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02004963 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02004964 print "</div>\n"; # class="page_body"
4965 git_footer_html();
4966
4967 } elsif ($format eq 'plain') {
4968 local $/ = undef;
4969 print <$fd>;
4970 close $fd
4971 or print "Reading git-diff-tree failed\n";
4972 }
4973}
4974
4975sub git_commitdiff_plain {
4976 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02004977}
4978
Kay Sievers09bd7892005-08-07 20:21:23 +02004979sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07004980 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004981 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02004982 }
Jakub Narebski8be68352006-09-11 00:36:04 +02004983 if (!defined $page) {
4984 $page = 0;
4985 }
Luben Tuikov63433102006-07-23 13:31:15 -07004986 my $ftype;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004987 my %co = parse_commit($hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004988 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004989 die_error(undef, "Unknown commit object");
Kay Sievers2ae100d2005-08-07 20:17:00 +02004990 }
Jakub Narebski8be68352006-09-11 00:36:04 +02004991
Jakub Narebski847e01f2006-08-14 02:05:47 +02004992 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02004993 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4994
Luben Tuikov93d5f062006-07-23 13:30:08 -07004995 if (!defined $hash && defined $file_name) {
4996 $hash = git_get_hash_by_path($hash_base, $file_name);
4997 }
Luben Tuikovcff07712006-07-23 13:28:55 -07004998 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07004999 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07005000 }
Kay Sievers10dba282005-08-07 20:25:27 +02005001
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005002 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005003
Jakub Narebski8be68352006-09-11 00:36:04 +02005004 my $paging_nav = '';
5005 if ($page > 0) {
5006 $paging_nav .=
5007 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5008 file_name=>$file_name)},
5009 "first");
5010 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005011 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebski8be68352006-09-11 00:36:04 +02005012 -accesskey => "p", -title => "Alt-p"}, "prev");
5013 } else {
5014 $paging_nav .= "first";
5015 $paging_nav .= " &sdot; prev";
5016 }
Jakub Narebski8be68352006-09-11 00:36:04 +02005017 my $next_link = '';
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005018 if ($#commitlist >= 100) {
Jakub Narebski8be68352006-09-11 00:36:04 +02005019 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005020 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005021 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005022 $paging_nav .= " &sdot; $next_link";
5023 } else {
5024 $paging_nav .= " &sdot; next";
Jakub Narebski8be68352006-09-11 00:36:04 +02005025 }
Jakub Narebski581860e2006-08-14 02:09:08 +02005026
Jakub Narebski8be68352006-09-11 00:36:04 +02005027 git_header_html();
5028 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5029 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5030 git_print_page_path($file_name, $ftype, $hash_base);
5031
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00005032 git_history_body(\@commitlist, 0, 99,
Jakub Narebski8be68352006-09-11 00:36:04 +02005033 $refs, $hash_base, $ftype, $next_link);
5034
Kay Sieversd51e9022005-08-07 20:16:07 +02005035 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02005036}
Kay Sievers19806692005-08-07 20:26:27 +02005037
5038sub git_search {
Robert Fitzsimons6be93512006-12-23 03:35:16 +00005039 my ($have_search) = gitweb_check_feature('search');
5040 if (!$have_search) {
5041 die_error('403 Permission denied', "Permission denied");
5042 }
Kay Sievers19806692005-08-07 20:26:27 +02005043 if (!defined $searchtext) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02005044 die_error(undef, "Text field empty");
Kay Sievers19806692005-08-07 20:26:27 +02005045 }
5046 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005047 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005048 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005049 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02005050 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02005051 die_error(undef, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02005052 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005053 if (!defined $page) {
5054 $page = 0;
5055 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02005056
Petr Baudis88ad7292006-10-24 05:15:46 +02005057 $searchtype ||= 'commit';
5058 if ($searchtype eq 'pickaxe') {
Jakub Narebski04f7a942006-09-11 00:29:27 +02005059 # pickaxe may take all resources of your box and run for several minutes
5060 # with every query - so decide by yourself how public you make this feature
5061 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5062 if (!$have_pickaxe) {
5063 die_error('403 Permission denied', "Permission denied");
5064 }
Kay Sieversc994d622005-08-07 20:27:18 +02005065 }
Petr Baudise7738552007-05-17 04:31:12 +02005066 if ($searchtype eq 'grep') {
5067 my ($have_grep) = gitweb_check_feature('grep');
5068 if (!$have_grep) {
5069 die_error('403 Permission denied', "Permission denied");
5070 }
5071 }
Petr Baudis88ad7292006-10-24 05:15:46 +02005072
Kay Sievers19806692005-08-07 20:26:27 +02005073 git_header_html();
Kay Sievers19806692005-08-07 20:26:27 +02005074
Petr Baudis88ad7292006-10-24 05:15:46 +02005075 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
Robert Fitzsimons8e574fb2006-12-23 03:35:14 +00005076 my $greptype;
5077 if ($searchtype eq 'commit') {
5078 $greptype = "--grep=";
5079 } elsif ($searchtype eq 'author') {
5080 $greptype = "--author=";
5081 } elsif ($searchtype eq 'committer') {
5082 $greptype = "--committer=";
5083 }
Jakub Narebski7e431ef2007-05-16 01:56:10 +02005084 $greptype .= $search_regexp;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005085 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005086
5087 my $paging_nav = '';
5088 if ($page > 0) {
5089 $paging_nav .=
5090 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005091 searchtext=>$searchtext, searchtype=>$searchtype)},
5092 "first");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005093 $paging_nav .= " &sdot; " .
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005094 $cgi->a({-href => href(-replay=>1, page=>$page-1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005095 -accesskey => "p", -title => "Alt-p"}, "prev");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005096 } else {
5097 $paging_nav .= "first";
5098 $paging_nav .= " &sdot; prev";
5099 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005100 my $next_link = '';
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005101 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005102 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005103 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005104 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005105 $paging_nav .= " &sdot; $next_link";
5106 } else {
5107 $paging_nav .= " &sdot; next";
5108 }
5109
5110 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005111 }
5112
5113 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5114 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005115 git_search_grep_body(\@commitlist, 0, 99, $next_link);
Kay Sieversc994d622005-08-07 20:27:18 +02005116 }
5117
Petr Baudis88ad7292006-10-24 05:15:46 +02005118 if ($searchtype eq 'pickaxe') {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005119 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5120 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5121
5122 print "<table cellspacing=\"0\">\n";
5123 my $alternate = 1;
Kay Sieversc994d622005-08-07 20:27:18 +02005124 $/ = "\n";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005125 my $git_command = git_cmd_str();
Petr Baudis4229aa52007-05-17 04:30:42 +02005126 my $searchqtext = $searchtext;
5127 $searchqtext =~ s/'/'\\''/;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005128 open my $fd, "-|", "$git_command rev-list $hash | " .
Petr Baudis4229aa52007-05-17 04:30:42 +02005129 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
Kay Sieversc994d622005-08-07 20:27:18 +02005130 undef %co;
5131 my @files;
5132 while (my $line = <$fd>) {
5133 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5134 my %set;
5135 $set{'file'} = $6;
5136 $set{'from_id'} = $3;
5137 $set{'to_id'} = $4;
5138 $set{'id'} = $set{'to_id'};
5139 if ($set{'id'} =~ m/0{40}/) {
5140 $set{'id'} = $set{'from_id'};
5141 }
5142 if ($set{'id'} =~ m/0{40}/) {
5143 next;
5144 }
5145 push @files, \%set;
Kay Sievers53b89d82005-08-12 22:12:58 +02005146 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
Kay Sieversc994d622005-08-07 20:27:18 +02005147 if (%co) {
5148 if ($alternate) {
5149 print "<tr class=\"dark\">\n";
5150 } else {
5151 print "<tr class=\"light\">\n";
5152 }
5153 $alternate ^= 1;
David Symondsce58ec92007-10-23 11:31:22 +10005154 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
Kay Sievers71be1e72005-08-07 20:27:27 +02005155 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
David Symondse076a0e2007-10-22 10:28:03 +10005156 "<td><i>" . $author . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02005157 "<td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005158 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5159 -class => "list subject"},
David Symondsd3cd2492007-10-23 11:31:23 +10005160 chop_and_escape_str($co{'title'}, 50) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02005161 while (my $setref = shift @files) {
5162 my %set = %$setref;
Jakub Narebski952c65f2006-08-22 16:52:50 +02005163 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5164 hash=>$set{'id'}, file_name=>$set{'file'}),
5165 -class => "list"},
Jakub Narebski403d0902006-11-08 11:48:56 +01005166 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
Kay Sieversc994d622005-08-07 20:27:18 +02005167 "<br/>\n";
5168 }
5169 print "</td>\n" .
5170 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02005171 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005172 " | " .
5173 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02005174 print "</td>\n" .
5175 "</tr>\n";
5176 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005177 %co = parse_commit($1);
Kay Sieversc994d622005-08-07 20:27:18 +02005178 }
5179 }
5180 close $fd;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005181
5182 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02005183 }
Petr Baudise7738552007-05-17 04:31:12 +02005184
5185 if ($searchtype eq 'grep') {
5186 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5187 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5188
5189 print "<table cellspacing=\"0\">\n";
5190 my $alternate = 1;
5191 my $matches = 0;
5192 $/ = "\n";
5193 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5194 my $lastfile = '';
5195 while (my $line = <$fd>) {
5196 chomp $line;
5197 my ($file, $lno, $ltext, $binary);
5198 last if ($matches++ > 1000);
5199 if ($line =~ /^Binary file (.+) matches$/) {
5200 $file = $1;
5201 $binary = 1;
5202 } else {
5203 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5204 }
5205 if ($file ne $lastfile) {
5206 $lastfile and print "</td></tr>\n";
5207 if ($alternate++) {
5208 print "<tr class=\"dark\">\n";
5209 } else {
5210 print "<tr class=\"light\">\n";
5211 }
5212 print "<td class=\"list\">".
5213 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5214 file_name=>"$file"),
5215 -class => "list"}, esc_path($file));
5216 print "</td><td>\n";
5217 $lastfile = $file;
5218 }
5219 if ($binary) {
5220 print "<div class=\"binary\">Binary file</div>\n";
5221 } else {
5222 $ltext = untabify($ltext);
5223 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5224 $ltext = esc_html($1, -nbsp=>1);
5225 $ltext .= '<span class="match">';
5226 $ltext .= esc_html($2, -nbsp=>1);
5227 $ltext .= '</span>';
5228 $ltext .= esc_html($3, -nbsp=>1);
5229 } else {
5230 $ltext = esc_html($ltext, -nbsp=>1);
5231 }
5232 print "<div class=\"pre\">" .
5233 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5234 file_name=>"$file").'#l'.$lno,
5235 -class => "linenr"}, sprintf('%4i', $lno))
5236 . ' ' . $ltext . "</div>\n";
5237 }
5238 }
5239 if ($lastfile) {
5240 print "</td></tr>\n";
5241 if ($matches > 1000) {
5242 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5243 }
5244 } else {
5245 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5246 }
5247 close $fd;
5248
5249 print "</table>\n";
5250 }
Kay Sievers19806692005-08-07 20:26:27 +02005251 git_footer_html();
5252}
5253
Petr Baudis88ad7292006-10-24 05:15:46 +02005254sub git_search_help {
5255 git_header_html();
5256 git_print_page_nav('','', $hash,$hash,$hash);
5257 print <<EOT;
5258<dl>
5259<dt><b>commit</b></dt>
5260<dd>The commit messages and authorship information will be scanned for the given string.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005261EOT
5262 my ($have_grep) = gitweb_check_feature('grep');
5263 if ($have_grep) {
5264 print <<EOT;
5265<dt><b>grep</b></dt>
5266<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5267 a different one) are searched for the given
5268<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5269(POSIX extended) and the matches are listed. On large
5270trees, this search can take a while and put some strain on the server, so please use it with
5271some consideration.</dd>
5272EOT
5273 }
5274 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02005275<dt><b>author</b></dt>
5276<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5277<dt><b>committer</b></dt>
5278<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5279EOT
5280 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5281 if ($have_pickaxe) {
5282 print <<EOT;
5283<dt><b>pickaxe</b></dt>
5284<dd>All commits that caused the string to appear or disappear from any file (changes that
5285added, removed or "modified" the string) will be listed. This search can take a while and
5286takes a lot of strain on the server, so please use it wisely.</dd>
5287EOT
5288 }
5289 print "</dl>\n";
5290 git_footer_html();
5291}
5292
Kay Sievers19806692005-08-07 20:26:27 +02005293sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005294 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005295 if (!defined $hash) {
5296 $hash = $head;
5297 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005298 if (!defined $page) {
5299 $page = 0;
5300 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005301 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02005302
Robert Fitzsimonsd2229842007-01-03 20:25:55 +00005303 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02005304
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005305 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005306 my $next_link = '';
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005307 if ($#commitlist >= 100) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005308 $next_link =
Jakub Narebski7afd77b2007-11-01 13:06:28 +01005309 $cgi->a({-href => href(-replay=>1, page=>$page+1),
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005310 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005311 }
5312
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005313 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02005314 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5315 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005316
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005317 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005318
Kay Sievers19806692005-08-07 20:26:27 +02005319 git_footer_html();
5320}
Jakub Narebski717b8312006-07-31 21:22:15 +02005321
5322## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005323## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02005324
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005325sub git_feed {
5326 my $format = shift || 'atom';
5327 my ($have_blame) = gitweb_check_feature('blame');
5328
5329 # Atom: http://www.atomenabled.org/developers/syndication/
5330 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5331 if ($format ne 'rss' && $format ne 'atom') {
5332 die_error(undef, "Unknown web feed format");
5333 }
5334
5335 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5336 my $head = $hash || 'HEAD';
Jakub Narebski33f24332007-08-03 19:50:42 +02005337 my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005338
5339 my %latest_commit;
5340 my %latest_date;
5341 my $content_type = "application/$format+xml";
5342 if (defined $cgi->http('HTTP_ACCEPT') &&
5343 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5344 # browser (feed reader) prefers text/xml
5345 $content_type = 'text/xml';
5346 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005347 if (defined($commitlist[0])) {
5348 %latest_commit = %{$commitlist[0]};
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005349 %latest_date = parse_date($latest_commit{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005350 print $cgi->header(
5351 -type => $content_type,
5352 -charset => 'utf-8',
5353 -last_modified => $latest_date{'rfc2822'});
5354 } else {
5355 print $cgi->header(
5356 -type => $content_type,
5357 -charset => 'utf-8');
5358 }
5359
5360 # Optimization: skip generating the body if client asks only
5361 # for Last-Modified date.
5362 return if ($cgi->request_method() eq 'HEAD');
5363
5364 # header variables
5365 my $title = "$site_name - $project/$action";
5366 my $feed_type = 'log';
5367 if (defined $hash) {
5368 $title .= " - '$hash'";
5369 $feed_type = 'branch log';
5370 if (defined $file_name) {
5371 $title .= " :: $file_name";
5372 $feed_type = 'history';
5373 }
5374 } elsif (defined $file_name) {
5375 $title .= " - $file_name";
5376 $feed_type = 'history';
5377 }
5378 $title .= " $feed_type";
5379 my $descr = git_get_project_description($project);
5380 if (defined $descr) {
5381 $descr = esc_html($descr);
5382 } else {
5383 $descr = "$project " .
5384 ($format eq 'rss' ? 'RSS' : 'Atom') .
5385 " feed";
5386 }
5387 my $owner = git_get_project_owner($project);
5388 $owner = esc_html($owner);
5389
5390 #header
5391 my $alt_url;
5392 if (defined $file_name) {
5393 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5394 } elsif (defined $hash) {
5395 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5396 } else {
5397 $alt_url = href(-full=>1, action=>"summary");
5398 }
5399 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5400 if ($format eq 'rss') {
5401 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02005402<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5403<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02005404XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005405 print "<title>$title</title>\n" .
5406 "<link>$alt_url</link>\n" .
5407 "<description>$descr</description>\n" .
5408 "<language>en</language>\n";
5409 } elsif ($format eq 'atom') {
5410 print <<XML;
5411<feed xmlns="http://www.w3.org/2005/Atom">
5412XML
5413 print "<title>$title</title>\n" .
5414 "<subtitle>$descr</subtitle>\n" .
5415 '<link rel="alternate" type="text/html" href="' .
5416 $alt_url . '" />' . "\n" .
5417 '<link rel="self" type="' . $content_type . '" href="' .
5418 $cgi->self_url() . '" />' . "\n" .
5419 "<id>" . href(-full=>1) . "</id>\n" .
5420 # use project owner for feed author
5421 "<author><name>$owner</name></author>\n";
5422 if (defined $favicon) {
5423 print "<icon>" . esc_url($favicon) . "</icon>\n";
5424 }
5425 if (defined $logo_url) {
5426 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01005427 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005428 }
5429 if (! %latest_date) {
5430 # dummy date to keep the feed valid until commits trickle in:
5431 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5432 } else {
5433 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5434 }
5435 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005436
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005437 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005438 for (my $i = 0; $i <= $#commitlist; $i++) {
5439 my %co = %{$commitlist[$i]};
5440 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02005441 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005442 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02005443 last;
5444 }
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005445 my %cd = parse_date($co{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005446
5447 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005448 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02005449 $co{'parent'} || "--root",
5450 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02005451 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02005452 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02005453 close $fd
5454 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005455
5456 # print element (entry, item)
5457 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5458 if ($format eq 'rss') {
5459 print "<item>\n" .
5460 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5461 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5462 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5463 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5464 "<link>$co_url</link>\n" .
5465 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5466 "<content:encoded>" .
5467 "<![CDATA[\n";
5468 } elsif ($format eq 'atom') {
5469 print "<entry>\n" .
5470 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5471 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01005472 "<author>\n" .
5473 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5474 if ($co{'author_email'}) {
5475 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5476 }
5477 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005478 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01005479 "<contributor>\n" .
5480 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5481 if ($co{'committer_email'}) {
5482 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5483 }
5484 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005485 "<published>$cd{'iso-8601'}</published>\n" .
5486 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5487 "<id>$co_url</id>\n" .
5488 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5489 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5490 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005491 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005492 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005493 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005494 $line = esc_html($line);
5495 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005496 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005497 print "</pre><ul>\n";
5498 foreach my $difftree_line (@difftree) {
5499 my %difftree = parse_difftree_raw_line($difftree_line);
5500 next if !$difftree{'from_id'};
5501
5502 my $file = $difftree{'file'} || $difftree{'to_file'};
5503
5504 print "<li>" .
5505 "[" .
5506 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5507 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5508 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5509 file_name=>$file, file_parent=>$difftree{'from_file'}),
5510 -title => "diff"}, 'D');
5511 if ($have_blame) {
5512 print $cgi->a({-href => href(-full=>1, action=>"blame",
5513 file_name=>$file, hash_base=>$commit),
5514 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02005515 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005516 # if this is not a feed of a file history
5517 if (!defined $file_name || $file_name ne $file) {
5518 print $cgi->a({-href => href(-full=>1, action=>"history",
5519 file_name=>$file, hash=>$commit),
5520 -title => "history"}, 'H');
5521 }
5522 $file = esc_path($file);
5523 print "] ".
5524 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005525 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005526 if ($format eq 'rss') {
5527 print "</ul>]]>\n" .
5528 "</content:encoded>\n" .
5529 "</item>\n";
5530 } elsif ($format eq 'atom') {
5531 print "</ul>\n</div>\n" .
5532 "</content>\n" .
5533 "</entry>\n";
5534 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005535 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005536
5537 # end of feed
5538 if ($format eq 'rss') {
5539 print "</channel>\n</rss>\n";
5540 } elsif ($format eq 'atom') {
5541 print "</feed>\n";
5542 }
5543}
5544
5545sub git_rss {
5546 git_feed('rss');
5547}
5548
5549sub git_atom {
5550 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02005551}
5552
5553sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005554 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02005555
5556 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02005557 print <<XML;
5558<?xml version="1.0" encoding="utf-8"?>
5559<opml version="1.0">
5560<head>
Petr Baudis8be28902006-10-24 05:18:39 +02005561 <title>$site_name OPML Export</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02005562</head>
5563<body>
5564<outline text="git RSS feeds">
5565XML
Jakub Narebski717b8312006-07-31 21:22:15 +02005566
5567 foreach my $pr (@list) {
5568 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02005569 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02005570 if (!defined $head) {
5571 next;
5572 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005573 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02005574 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02005575 if (!%co) {
5576 next;
5577 }
5578
5579 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5580 my $rss = "$my_url?p=$proj{'path'};a=rss";
5581 my $html = "$my_url?p=$proj{'path'};a=summary";
5582 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5583 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02005584 print <<XML;
5585</outline>
5586</body>
5587</opml>
5588XML
Jakub Narebski717b8312006-07-31 21:22:15 +02005589}