blob: c4f88245aa23ccbab07cc109a8478f1dd9ed8fdb [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
Kay Sieversb87d78d2005-08-07 20:21:04 +020038# target of the home link on top of all pages
Martin Waitz6132b7e2006-08-17 00:28:39 +020039our $home_link = $my_uri || "/";
Kay Sieversb87d78d2005-08-07 20:21:04 +020040
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090041# string of the home link on top of all pages
42our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43
Alp Toker49da1da2006-07-11 21:10:26 +010044# name of your site or organization to appear in page titles
45# replace this with something more descriptive for clearer bookmarks
Petr Baudis8be28902006-10-24 05:18:39 +020046our $site_name = "++GITWEB_SITENAME++"
47 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
Alp Toker49da1da2006-07-11 21:10:26 +010048
Alan Chandlerb2d34762006-10-03 13:49:03 +010049# filename of html text to include at top of each page
50our $site_header = "++GITWEB_SITE_HEADER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020051# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070052our $home_text = "++GITWEB_HOMETEXT++";
Alan Chandlerb2d34762006-10-03 13:49:03 +010053# filename of html text to include at bottom of each page
54our $site_footer = "++GITWEB_SITE_FOOTER++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020055
Alan Chandlerb2d34762006-10-03 13:49:03 +010056# URI of stylesheets
57our @stylesheets = ("++GITWEB_CSS++");
Petr Baudis887a6122006-10-26 14:41:25 +020058# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
59our $stylesheet = undef;
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020060# URI of GIT logo (72x27 size)
Junio C Hamano06c084d2006-08-02 13:50:20 -070061our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +020062# URI of GIT favicon, assumed to be image/png type
63our $favicon = "++GITWEB_FAVICON++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +020064
Jakub Narebski9a7a62f2006-10-06 12:31:05 +020065# URI and label (title) of GIT logo link
66#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
67#our $logo_label = "git documentation";
68our $logo_url = "http://git.or.cz/";
69our $logo_label = "git homepage";
Junio C Hamano51a7c662006-09-23 12:36:01 -070070
Kay Sievers09bd7892005-08-07 20:21:23 +020071# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -070072our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020073
Michael Hendricks55feb122007-07-04 18:36:48 -060074# the width (in characters) of the projects list "Description" column
75our $projects_list_description_width = 25;
76
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +020077# default order of projects list
78# valid values are none, project, descr, owner, and age
79our $default_projects_order = "project";
80
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +020081# show repository only if this file exists
82# (only effective if this variable evaluates to true)
83our $export_ok = "++GITWEB_EXPORT_OK++";
84
85# only allow viewing of repositories also shown on the overview page
86our $strict_export = "++GITWEB_STRICT_EXPORT++";
87
Jakub Narebski19a87212006-08-15 23:03:17 +020088# list of git base URLs used for URL to where fetch project from,
89# i.e. full URL is "$git_base_url/$project"
Jakub Narebskid6b7e0b2006-10-26 12:26:44 +020090our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
Jakub Narebski19a87212006-08-15 23:03:17 +020091
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020092# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +020093our $default_blob_plain_mimetype = 'text/plain';
94our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020095
Petr Baudis2d007372006-06-18 00:01:06 +020096# file to use for guessing MIME types before trying /etc/mime.types
97# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +020098our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +020099
Martin Koegler00f429a2007-06-03 17:42:44 +0200100# assume this charset if line contains non-UTF-8 characters;
101# it should be valid encoding (see Encoding::Supported(3pm) for list),
102# for which encoding all byte sequences are valid, for example
103# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
104# could be even 'utf-8' for the old behavior)
105our $fallback_encoding = 'latin1';
106
Jakub Narebski69a9b412007-07-20 02:15:09 +0200107# rename detection options for git-diff and git-diff-tree
108# - default is '-M', with the cost proportional to
109# (number of removed files) * (number of new files).
110# - more costly is '-C' (which implies '-M'), with the cost proportional to
111# (number of changed files + number of removed files) * (number of new files)
112# - even more costly is '-C', '--find-copies-harder' with cost
113# (number of files in the original tree) * (number of new files)
114# - one might want to include '-B' option, e.g. '-B', '-M'
115our @diff_opts = ('-M'); # taken from git_commit
116
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200117# information about snapshot formats that gitweb is capable of serving
118our %known_snapshot_formats = (
119 # name => {
120 # 'display' => display name,
121 # 'type' => mime type,
122 # 'suffix' => filename suffix,
123 # 'format' => --format for git-archive,
124 # 'compressor' => [compressor command and arguments]
125 # (array reference, optional)}
126 #
127 'tgz' => {
128 'display' => 'tar.gz',
129 'type' => 'application/x-gzip',
130 'suffix' => '.tar.gz',
131 'format' => 'tar',
132 'compressor' => ['gzip']},
133
134 'tbz2' => {
135 'display' => 'tar.bz2',
136 'type' => 'application/x-bzip2',
137 'suffix' => '.tar.bz2',
138 'format' => 'tar',
139 'compressor' => ['bzip2']},
140
141 'zip' => {
142 'display' => 'zip',
143 'type' => 'application/x-zip',
144 'suffix' => '.zip',
145 'format' => 'zip'},
146);
147
148# Aliases so we understand old gitweb.snapshot values in repository
149# configuration.
150our %known_snapshot_format_aliases = (
151 'gzip' => 'tgz',
152 'bzip2' => 'tbz2',
153
154 # backward compatibility: legacy gitweb config support
155 'x-gzip' => undef, 'gz' => undef,
156 'x-bzip2' => undef, 'bz2' => undef,
157 'x-zip' => undef, '' => undef,
158);
159
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530160# You define site-wide feature defaults here; override them with
161# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +0200162our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +0200163 # feature => {
164 # 'sub' => feature-sub (subroutine),
165 # 'override' => allow-override (boolean),
166 # 'default' => [ default options...] (array reference)}
167 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200168 # if feature is overridable (it means that allow-override has true value),
Jakub Narebski17848fc2006-08-26 19:14:22 +0200169 # then feature-sub will be called with default options as parameters;
170 # return value of feature-sub indicates if to enable specified feature
171 #
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200172 # if there is no 'sub' key (no feature-sub), then feature cannot be
173 # overriden
174 #
Jakub Narebski17848fc2006-08-26 19:14:22 +0200175 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530176
Petr Baudis45a3b122006-10-07 15:17:47 +0200177 # Enable the 'blame' blob view, showing the last commit that modified
178 # each line in the file. This can be very CPU-intensive.
179
180 # To enable system wide have in $GITWEB_CONFIG
181 # $feature{'blame'}{'default'} = [1];
182 # To have project specific config enable override in $GITWEB_CONFIG
183 # $feature{'blame'}{'override'} = 1;
184 # and in project config gitweb.blame = 0|1;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200185 'blame' => {
186 'sub' => \&feature_blame,
187 'override' => 0,
188 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530189
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200190 # Enable the 'snapshot' link, providing a compressed archive of any
Petr Baudis45a3b122006-10-07 15:17:47 +0200191 # tree. This can potentially generate high traffic if you have large
192 # project.
193
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200194 # Value is a list of formats defined in %known_snapshot_formats that
195 # you wish to offer.
Petr Baudis45a3b122006-10-07 15:17:47 +0200196 # To disable system wide have in $GITWEB_CONFIG
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200197 # $feature{'snapshot'}{'default'} = [];
Petr Baudis45a3b122006-10-07 15:17:47 +0200198 # To have project specific config enable override in $GITWEB_CONFIG
Uwe Zeisbergerbbee1d92006-12-08 12:44:31 +0100199 # $feature{'snapshot'}{'override'} = 1;
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200200 # and in project config, a comma-separated list of formats or "none"
201 # to disable. Example: gitweb.snapshot = tbz2,zip;
Jakub Narebski952c65f2006-08-22 16:52:50 +0200202 'snapshot' => {
203 'sub' => \&feature_snapshot,
204 'override' => 0,
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200205 'default' => ['tgz']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200206
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000207 # Enable text search, which will list the commits which match author,
208 # committer or commit text to a given string. Enabled by default.
Jakub Narebskib4b20b22007-05-15 01:55:44 +0200209 # Project specific override is not supported.
Robert Fitzsimons6be93512006-12-23 03:35:16 +0000210 'search' => {
211 'override' => 0,
212 'default' => [1]},
213
Petr Baudise7738552007-05-17 04:31:12 +0200214 # Enable grep search, which will list the files in currently selected
215 # tree containing the given string. Enabled by default. This can be
216 # potentially CPU-intensive, of course.
217
218 # To enable system wide have in $GITWEB_CONFIG
219 # $feature{'grep'}{'default'} = [1];
220 # To have project specific config enable override in $GITWEB_CONFIG
221 # $feature{'grep'}{'override'} = 1;
222 # and in project config gitweb.grep = 0|1;
223 'grep' => {
224 'override' => 0,
225 'default' => [1]},
226
Petr Baudis45a3b122006-10-07 15:17:47 +0200227 # Enable the pickaxe search, which will list the commits that modified
228 # a given string in a file. This can be practical and quite faster
229 # alternative to 'blame', but still potentially CPU-intensive.
230
231 # To enable system wide have in $GITWEB_CONFIG
232 # $feature{'pickaxe'}{'default'} = [1];
233 # To have project specific config enable override in $GITWEB_CONFIG
234 # $feature{'pickaxe'}{'override'} = 1;
235 # and in project config gitweb.pickaxe = 0|1;
Jakub Narebski04f7a942006-09-11 00:29:27 +0200236 'pickaxe' => {
237 'sub' => \&feature_pickaxe,
238 'override' => 0,
239 'default' => [1]},
Martin Waitz9e756902006-10-01 23:57:48 +0200240
Petr Baudis45a3b122006-10-07 15:17:47 +0200241 # Make gitweb use an alternative format of the URLs which can be
242 # more readable and natural-looking: project name is embedded
243 # directly in the path and the query string contains other
244 # auxiliary information. All gitweb installations recognize
245 # URL in either format; this configures in which formats gitweb
246 # generates links.
247
248 # To enable system wide have in $GITWEB_CONFIG
249 # $feature{'pathinfo'}{'default'} = [1];
250 # Project specific override is not supported.
251
252 # Note that you will need to change the default location of CSS,
253 # favicon, logo and possibly other files to an absolute URL. Also,
254 # if gitweb.cgi serves as your indexfile, you will need to force
255 # $my_uri to contain the script name in your $GITWEB_CONFIG.
Martin Waitz9e756902006-10-01 23:57:48 +0200256 'pathinfo' => {
257 'override' => 0,
258 'default' => [0]},
Petr Baudise30496d2006-10-24 05:33:17 +0200259
260 # Make gitweb consider projects in project root subdirectories
261 # to be forks of existing projects. Given project $projname.git,
262 # projects matching $projname/*.git will not be shown in the main
263 # projects list, instead a '+' mark will be added to $projname
264 # there and a 'forks' view will be enabled for the project, listing
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +0200265 # all the forks. If project list is taken from a file, forks have
266 # to be listed after the main project.
Petr Baudise30496d2006-10-24 05:33:17 +0200267
268 # To enable system wide have in $GITWEB_CONFIG
269 # $feature{'forks'}{'default'} = [1];
270 # Project specific override is not supported.
271 'forks' => {
272 'override' => 0,
273 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530274);
275
276sub gitweb_check_feature {
277 my ($name) = @_;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +0200278 return unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200279 my ($sub, $override, @defaults) = (
280 $feature{$name}{'sub'},
281 $feature{$name}{'override'},
282 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530283 if (!$override) { return @defaults; }
Martin Waitza9455912006-10-03 20:07:43 +0200284 if (!defined $sub) {
285 warn "feature $name is not overrideable";
286 return @defaults;
287 }
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530288 return $sub->(@defaults);
289}
290
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530291sub feature_blame {
292 my ($val) = git_get_project_config('blame', '--bool');
293
294 if ($val eq 'true') {
295 return 1;
296 } elsif ($val eq 'false') {
297 return 0;
298 }
299
300 return $_[0];
301}
302
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530303sub feature_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200304 my (@fmts) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530305
306 my ($val) = git_get_project_config('snapshot');
307
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200308 if ($val) {
309 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
310 @fmts = grep { defined } map {
311 exists $known_snapshot_format_aliases{$_} ?
312 $known_snapshot_format_aliases{$_} : $_ } @fmts;
313 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
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
Junio C Hamano06c084d2006-08-02 13:50:20 -0700359our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Dennis Stosberg4b5dc982006-08-29 09:19:02 +0200360do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400361
362# version of the core git binary
363our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
364
365$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400366
Jakub Narebski154b4d72006-08-05 12:55:20 +0200367# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200368# input validation and dispatch
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200369our $action = $cgi->param('a');
Kay Sievers09bd7892005-08-07 20:21:23 +0200370if (defined $action) {
Kay Sieversc91da262005-09-03 14:50:33 +0200371 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200372 die_error(undef, "Invalid action parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200373 }
Kay Sieversb87d78d2005-08-07 20:21:04 +0200374}
375
Jakub Narebski24d06932006-09-26 01:57:02 +0200376# parameters which are pathnames
Martin Waitzdd702352006-09-16 23:08:32 +0200377our $project = $cgi->param('p');
Martin Waitz13d02162006-08-17 00:28:40 +0200378if (defined $project) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200379 if (!validate_pathname($project) ||
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200380 !(-d "$projectroot/$project") ||
Junio C Hamano2172ce42006-10-03 02:30:47 -0700381 !check_head_link("$projectroot/$project") ||
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200382 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
383 ($strict_export && !project_in_list($project))) {
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200384 undef $project;
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200385 die_error(undef, "No such project");
Kay Sievers9cd3d982005-08-07 20:17:42 +0200386 }
Kay Sievers2ad93312005-08-07 20:14:48 +0200387}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200388
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200389our $file_name = $cgi->param('f');
Jakub Narebski24d06932006-09-26 01:57:02 +0200390if (defined $file_name) {
391 if (!validate_pathname($file_name)) {
392 die_error(undef, "Invalid file parameter");
393 }
394}
Martin Waitz5c95fab2006-08-17 00:28:38 +0200395
Jakub Narebski24d06932006-09-26 01:57:02 +0200396our $file_parent = $cgi->param('fp');
397if (defined $file_parent) {
398 if (!validate_pathname($file_parent)) {
399 die_error(undef, "Invalid file parent parameter");
400 }
401}
402
403# parameters which are refnames
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200404our $hash = $cgi->param('h');
Kay Sievers4fac5292005-08-07 20:27:38 +0200405if (defined $hash) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200406 if (!validate_refname($hash)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200407 die_error(undef, "Invalid hash parameter");
Kay Sievers4fac5292005-08-07 20:27:38 +0200408 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200409}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200410
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200411our $hash_parent = $cgi->param('hp');
Kay Sieversc91da262005-09-03 14:50:33 +0200412if (defined $hash_parent) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200413 if (!validate_refname($hash_parent)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200414 die_error(undef, "Invalid hash parent parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200415 }
Kay Sievers09bd7892005-08-07 20:21:23 +0200416}
417
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200418our $hash_base = $cgi->param('hb');
Kay Sieversc91da262005-09-03 14:50:33 +0200419if (defined $hash_base) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200420 if (!validate_refname($hash_base)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200421 die_error(undef, "Invalid hash base parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200422 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200423}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200424
Miklos Vajna868bc062007-07-12 20:39:27 +0200425my %allowed_options = (
426 "--no-merges" => [ qw(rss atom log shortlog history) ],
427);
428
429our @extra_options = $cgi->param('opt');
430if (defined @extra_options) {
431 foreach(@extra_options)
432 {
433 if (not grep(/^$_$/, keys %allowed_options)) {
434 die_error(undef, "Invalid option parameter");
435 }
436 if (not grep(/^$action$/, @{$allowed_options{$_}})) {
437 die_error(undef, "Invalid option parameter for this action");
438 }
439 }
440}
441
Jakub Narebski420e92f2006-08-24 23:53:54 +0200442our $hash_parent_base = $cgi->param('hpb');
443if (defined $hash_parent_base) {
Jakub Narebski24d06932006-09-26 01:57:02 +0200444 if (!validate_refname($hash_parent_base)) {
Jakub Narebski420e92f2006-08-24 23:53:54 +0200445 die_error(undef, "Invalid hash parent base parameter");
446 }
447}
448
Jakub Narebski24d06932006-09-26 01:57:02 +0200449# other parameters
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200450our $page = $cgi->param('pg');
Kay Sieversea4a6df2005-08-07 20:26:49 +0200451if (defined $page) {
Matthias Lederhoferac8e3f22006-09-17 13:52:45 +0200452 if ($page =~ m/[^0-9]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200453 die_error(undef, "Invalid page parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200454 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200455}
Kay Sievers823d5dc2005-08-07 19:57:58 +0200456
Petr Baudise7738552007-05-17 04:31:12 +0200457our $searchtype = $cgi->param('st');
458if (defined $searchtype) {
459 if ($searchtype =~ m/[^a-z]/) {
460 die_error(undef, "Invalid searchtype parameter");
461 }
462}
463
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200464our $searchtext = $cgi->param('s');
Jakub Narebski7e431ef2007-05-16 01:56:10 +0200465our $search_regexp;
Kay Sievers19806692005-08-07 20:26:27 +0200466if (defined $searchtext) {
Petr Baudis4229aa52007-05-17 04:30:42 +0200467 if ($searchtype ne 'grep' and $searchtype ne 'pickaxe' and $searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200468 die_error(undef, "Invalid search parameter");
Kay Sievers19806692005-08-07 20:26:27 +0200469 }
Robert Fitzsimons9d032c72006-12-23 03:35:15 +0000470 if (length($searchtext) < 2) {
471 die_error(undef, "At least two characters are required for search parameter");
472 }
Jakub Narebski7e431ef2007-05-16 01:56:10 +0200473 $search_regexp = quotemeta $searchtext;
Kay Sievers19806692005-08-07 20:26:27 +0200474}
475
Martin Waitzdd702352006-09-16 23:08:32 +0200476# now read PATH_INFO and use it as alternative to parameters
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200477sub evaluate_path_info {
478 return if defined $project;
479 my $path_info = $ENV{"PATH_INFO"};
480 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200481 $path_info =~ s,^/+,,;
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200482 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200483 # find which part of PATH_INFO is project
Martin Waitzdd702352006-09-16 23:08:32 +0200484 $project = $path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200485 $project =~ s,/+$,,;
Junio C Hamano2172ce42006-10-03 02:30:47 -0700486 while ($project && !check_head_link("$projectroot/$project")) {
Martin Waitzdd702352006-09-16 23:08:32 +0200487 $project =~ s,/*[^/]*$,,;
488 }
Jakub Narebskicd90e752006-09-20 00:49:51 +0200489 # validate project
Jakub Narebski24d06932006-09-26 01:57:02 +0200490 $project = validate_pathname($project);
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200491 if (!$project ||
492 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
493 ($strict_export && !project_in_list($project))) {
494 undef $project;
495 return;
Martin Waitzdd702352006-09-16 23:08:32 +0200496 }
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200497 # do not change any parameters if an action is given using the query string
498 return if $action;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200499 $path_info =~ s,^$project/*,,;
500 my ($refname, $pathname) = split(/:/, $path_info, 2);
501 if (defined $pathname) {
502 # we got "project.git/branch:filename" or "project.git/branch:dir/"
503 # we could use git_get_type(branch:pathname), but it needs $git_dir
504 $pathname =~ s,^/+,,;
505 if (!$pathname || substr($pathname, -1) eq "/") {
506 $action ||= "tree";
Martin Waitz053d62b2006-09-21 09:48:21 +0200507 $pathname =~ s,/$,,;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200508 } else {
509 $action ||= "blob_plain";
510 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200511 $hash_base ||= validate_refname($refname);
512 $file_name ||= validate_pathname($pathname);
Jakub Narebskicd90e752006-09-20 00:49:51 +0200513 } elsif (defined $refname) {
Martin Waitzdd702352006-09-16 23:08:32 +0200514 # we got "project.git/branch"
515 $action ||= "shortlog";
Jakub Narebski24d06932006-09-26 01:57:02 +0200516 $hash ||= validate_refname($refname);
Martin Waitzdd702352006-09-16 23:08:32 +0200517 }
518}
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200519evaluate_path_info();
Martin Waitzdd702352006-09-16 23:08:32 +0200520
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200521# path to the current git repository
522our $git_dir;
523$git_dir = "$projectroot/$project" if $project;
Martin Waitzdd702352006-09-16 23:08:32 +0200524
Jakub Narebski717b8312006-07-31 21:22:15 +0200525# dispatch
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200526my %actions = (
527 "blame" => \&git_blame2,
528 "blobdiff" => \&git_blobdiff,
529 "blobdiff_plain" => \&git_blobdiff_plain,
530 "blob" => \&git_blob,
531 "blob_plain" => \&git_blob_plain,
532 "commitdiff" => \&git_commitdiff,
533 "commitdiff_plain" => \&git_commitdiff_plain,
534 "commit" => \&git_commit,
Petr Baudise30496d2006-10-24 05:33:17 +0200535 "forks" => \&git_forks,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200536 "heads" => \&git_heads,
537 "history" => \&git_history,
538 "log" => \&git_log,
539 "rss" => \&git_rss,
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +0100540 "atom" => \&git_atom,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200541 "search" => \&git_search,
Petr Baudis88ad7292006-10-24 05:15:46 +0200542 "search_help" => \&git_search_help,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200543 "shortlog" => \&git_shortlog,
544 "summary" => \&git_summary,
545 "tag" => \&git_tag,
546 "tags" => \&git_tags,
547 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530548 "snapshot" => \&git_snapshot,
Jakub Narebskica946012006-12-10 13:25:47 +0100549 "object" => \&git_object,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200550 # those below don't need $project
551 "opml" => \&git_opml,
552 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200553 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200554);
555
Gerrit Pape7f9778b2007-05-10 07:32:07 +0000556if (!defined $action) {
557 if (defined $hash) {
558 $action = git_get_type($hash);
559 } elsif (defined $hash_base && defined $file_name) {
560 $action = git_get_type("$hash_base:$file_name");
561 } elsif (defined $project) {
562 $action = 'summary';
563 } else {
564 $action = 'project_list';
565 }
Jakub Narebski77a153f2006-08-22 16:59:20 +0200566}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200567if (!defined($actions{$action})) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200568 die_error(undef, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200569}
Jakub Narebskid04d3d42006-09-19 21:53:22 +0200570if ($action !~ m/^(opml|project_list|project_index)$/ &&
571 !$project) {
572 die_error(undef, "Project needed");
573}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200574$actions{$action}->();
575exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200576
Jakub Narebski717b8312006-07-31 21:22:15 +0200577## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200578## action links
579
580sub href(%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200581 my %params = @_;
Jakub Narebskibd5d1e42006-11-19 15:05:21 +0100582 # default is to use -absolute url() i.e. $my_uri
583 my $href = $params{-full} ? $my_url : $my_uri;
Jakub Narebski498fe002006-08-22 19:05:25 +0200584
Petr Baudis34c06112006-10-11 22:31:15 +0200585 # XXX: Warning: If you touch this, check the search form for updating,
586 # too.
Jakub Narebski498fe002006-08-22 19:05:25 +0200587
588 my @mapping = (
Martin Waitz06a9d862006-08-16 00:23:50 +0200589 project => "p",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200590 action => "a",
Martin Waitz06a9d862006-08-16 00:23:50 +0200591 file_name => "f",
Martin Waitz5c95fab2006-08-17 00:28:38 +0200592 file_parent => "fp",
Miklos Vajna868bc062007-07-12 20:39:27 +0200593 extra_options => "opt",
Martin Waitz06a9d862006-08-16 00:23:50 +0200594 hash => "h",
595 hash_parent => "hp",
596 hash_base => "hb",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200597 hash_parent_base => "hpb",
Martin Waitz06a9d862006-08-16 00:23:50 +0200598 page => "pg",
Jakub Narebskia1565c42006-09-15 19:30:34 +0200599 order => "o",
Martin Waitz06a9d862006-08-16 00:23:50 +0200600 searchtext => "s",
Petr Baudis88ad7292006-10-24 05:15:46 +0200601 searchtype => "st",
Matt McCutchena3c8ab32007-07-22 01:30:27 +0200602 snapshot_format => "sf",
Martin Waitz06a9d862006-08-16 00:23:50 +0200603 );
Jakub Narebski498fe002006-08-22 19:05:25 +0200604 my %mapping = @mapping;
Martin Waitz06a9d862006-08-16 00:23:50 +0200605
Jakub Narebskia1565c42006-09-15 19:30:34 +0200606 $params{'project'} = $project unless exists $params{'project'};
Martin Waitz06a9d862006-08-16 00:23:50 +0200607
Martin Waitz9e756902006-10-01 23:57:48 +0200608 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
609 if ($use_pathinfo) {
610 # use PATH_INFO for project name
611 $href .= "/$params{'project'}" if defined $params{'project'};
612 delete $params{'project'};
613
614 # Summary just uses the project path URL
615 if (defined $params{'action'} && $params{'action'} eq 'summary') {
616 delete $params{'action'};
617 }
618 }
619
620 # now encode the parameters explicitly
Jakub Narebski498fe002006-08-22 19:05:25 +0200621 my @result = ();
622 for (my $i = 0; $i < @mapping; $i += 2) {
623 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
624 if (defined $params{$name}) {
625 push @result, $symbol . "=" . esc_param($params{$name});
626 }
627 }
Martin Waitz9e756902006-10-01 23:57:48 +0200628 $href .= "?" . join(';', @result) if scalar @result;
629
630 return $href;
Martin Waitz06a9d862006-08-16 00:23:50 +0200631}
632
633
634## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200635## validation, quoting/unquoting and escaping
636
Jakub Narebski24d06932006-09-26 01:57:02 +0200637sub validate_pathname {
638 my $input = shift || return undef;
Jakub Narebski717b8312006-07-31 21:22:15 +0200639
Jakub Narebski24d06932006-09-26 01:57:02 +0200640 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
641 # at the beginning, at the end, and between slashes.
642 # also this catches doubled slashes
643 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
644 return undef;
645 }
646 # no null characters
647 if ($input =~ m!\0!) {
648 return undef;
649 }
650 return $input;
651}
652
653sub validate_refname {
654 my $input = shift || return undef;
655
656 # textual hashes are O.K.
Jakub Narebski717b8312006-07-31 21:22:15 +0200657 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
658 return $input;
659 }
Jakub Narebski24d06932006-09-26 01:57:02 +0200660 # it must be correct pathname
661 $input = validate_pathname($input)
662 or return undef;
663 # restrictions on ref name according to git-check-ref-format
664 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200665 return undef;
666 }
667 return $input;
668}
669
Martin Koegler00f429a2007-06-03 17:42:44 +0200670# decode sequences of octets in utf8 into Perl's internal form,
671# which is utf-8 with utf8 flag set if needed. gitweb writes out
672# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
673sub to_utf8 {
674 my $str = shift;
675 my $res;
676 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
677 if (defined $res) {
678 return $res;
679 } else {
680 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
681 }
682}
683
Kay Sievers232ff552005-11-24 16:56:55 +0100684# quote unsafe chars, but keep the slash, even when it's not
685# correct, but quoted slashes look too horrible in bookmarks
686sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100687 my $str = shift;
Petr Baudisa2f3db22006-09-24 00:18:41 +0200688 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +0100689 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +0100690 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +0100691 return $str;
692}
693
Jakub Narebskif93bff82006-09-26 01:58:41 +0200694# quote unsafe chars in whole URL, so some charactrs cannot be quoted
695sub esc_url {
696 my $str = shift;
697 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
698 $str =~ s/\+/%2B/g;
699 $str =~ s/ /\+/g;
700 return $str;
701}
702
Kay Sievers232ff552005-11-24 16:56:55 +0100703# replace invalid utf8 character with SUBSTITUTION sequence
Jakub Narebski6255ef02006-11-01 14:33:21 +0100704sub esc_html ($;%) {
Kay Sievers40c13812005-11-19 17:41:29 +0100705 my $str = shift;
Jakub Narebski6255ef02006-11-01 14:33:21 +0100706 my %opts = @_;
707
Martin Koegler00f429a2007-06-03 17:42:44 +0200708 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +0800709 $str = $cgi->escapeHTML($str);
Jakub Narebski6255ef02006-11-01 14:33:21 +0100710 if ($opts{'-nbsp'}) {
711 $str =~ s/ /&nbsp;/g;
712 }
Junio C Hamano25ffbb22006-11-08 15:11:10 -0800713 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
Kay Sievers40c13812005-11-19 17:41:29 +0100714 return $str;
715}
716
Jakub Narebski391862e2006-11-25 09:43:59 +0100717# quote control characters and escape filename to HTML
718sub esc_path {
719 my $str = shift;
720 my %opts = @_;
721
Martin Koegler00f429a2007-06-03 17:42:44 +0200722 $str = to_utf8($str);
Li Yangc390ae92007-03-06 11:58:56 +0800723 $str = $cgi->escapeHTML($str);
Jakub Narebski391862e2006-11-25 09:43:59 +0100724 if ($opts{'-nbsp'}) {
725 $str =~ s/ /&nbsp;/g;
726 }
727 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
728 return $str;
729}
730
731# Make control characters "printable", using character escape codes (CEC)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +0100732sub quot_cec {
733 my $cntrl = shift;
734 my %es = ( # character escape codes, aka escape sequences
735 "\t" => '\t', # tab (HT)
736 "\n" => '\n', # line feed (LF)
737 "\r" => '\r', # carrige return (CR)
738 "\f" => '\f', # form feed (FF)
739 "\b" => '\b', # backspace (BS)
740 "\a" => '\a', # alarm (bell) (BEL)
741 "\e" => '\e', # escape (ESC)
742 "\013" => '\v', # vertical tab (VT)
743 "\000" => '\0', # nul character (NUL)
744 );
745 my $chr = ( (exists $es{$cntrl})
746 ? $es{$cntrl}
747 : sprintf('\%03o', ord($cntrl)) );
748 return "<span class=\"cntrl\">$chr</span>";
749}
750
Jakub Narebski391862e2006-11-25 09:43:59 +0100751# Alternatively use unicode control pictures codepoints,
752# Unicode "printable representation" (PR)
Jakub Narebski1d3bc0c2006-11-08 11:50:07 +0100753sub quot_upr {
754 my $cntrl = shift;
755 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
756 return "<span class=\"cntrl\">$chr</span>";
757}
758
Kay Sievers232ff552005-11-24 16:56:55 +0100759# git may return quoted and escaped filenames
760sub unquote {
761 my $str = shift;
Jakub Narebski403d0902006-11-08 11:48:56 +0100762
763 sub unq {
764 my $seq = shift;
765 my %es = ( # character escape codes, aka escape sequences
766 't' => "\t", # tab (HT, TAB)
767 'n' => "\n", # newline (NL)
768 'r' => "\r", # return (CR)
769 'f' => "\f", # form feed (FF)
770 'b' => "\b", # backspace (BS)
771 'a' => "\a", # alarm (bell) (BEL)
772 'e' => "\e", # escape (ESC)
773 'v' => "\013", # vertical tab (VT)
774 );
775
776 if ($seq =~ m/^[0-7]{1,3}$/) {
777 # octal char sequence
778 return chr(oct($seq));
779 } elsif (exists $es{$seq}) {
780 # C escape sequence, aka character escape code
781 return $es{$seq}
782 }
783 # quoted ordinary character
784 return $seq;
785 }
786
Kay Sievers232ff552005-11-24 16:56:55 +0100787 if ($str =~ m/^"(.*)"$/) {
Jakub Narebski403d0902006-11-08 11:48:56 +0100788 # needs unquoting
Kay Sievers232ff552005-11-24 16:56:55 +0100789 $str = $1;
Jakub Narebski403d0902006-11-08 11:48:56 +0100790 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
Kay Sievers232ff552005-11-24 16:56:55 +0100791 }
792 return $str;
793}
794
Jakub Narebskif16db172006-08-06 02:08:31 +0200795# escape tabs (convert tabs to spaces)
796sub untabify {
797 my $line = shift;
798
799 while ((my $pos = index($line, "\t")) != -1) {
800 if (my $count = (8 - ($pos % 8))) {
801 my $spaces = ' ' x $count;
802 $line =~ s/\t/$spaces/;
803 }
804 }
805
806 return $line;
807}
808
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200809sub project_in_list {
810 my $project = shift;
811 my @list = git_get_projects_list();
812 return @list && scalar(grep { $_->{'path'} eq $project } @list);
813}
814
Jakub Narebski717b8312006-07-31 21:22:15 +0200815## ----------------------------------------------------------------------
816## HTML aware string manipulation
817
818sub chop_str {
819 my $str = shift;
820 my $len = shift;
821 my $add_len = shift || 10;
822
823 # allow only $len chars, but don't cut a word if it would fit in $add_len
824 # if it doesn't fit, cut it if it's still longer than the dots we would add
825 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
826 my $body = $1;
827 my $tail = $2;
828 if (length($tail) > 4) {
829 $tail = " ...";
830 $body =~ s/&[^;]*$//; # remove chopped character entities
831 }
832 return "$body$tail";
833}
834
835## ----------------------------------------------------------------------
836## functions returning short strings
837
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000838# CSS class for given age value (in seconds)
839sub age_class {
840 my $age = shift;
841
Jakub Narebski785cdea2007-05-13 12:39:22 +0200842 if (!defined $age) {
843 return "noage";
844 } elsif ($age < 60*60*2) {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000845 return "age0";
846 } elsif ($age < 60*60*24*2) {
847 return "age1";
848 } else {
849 return "age2";
850 }
851}
852
Jakub Narebski717b8312006-07-31 21:22:15 +0200853# convert age in seconds to "nn units ago" string
854sub age_string {
855 my $age = shift;
856 my $age_str;
857
858 if ($age > 60*60*24*365*2) {
859 $age_str = (int $age/60/60/24/365);
860 $age_str .= " years ago";
861 } elsif ($age > 60*60*24*(365/12)*2) {
862 $age_str = int $age/60/60/24/(365/12);
863 $age_str .= " months ago";
864 } elsif ($age > 60*60*24*7*2) {
865 $age_str = int $age/60/60/24/7;
866 $age_str .= " weeks ago";
867 } elsif ($age > 60*60*24*2) {
868 $age_str = int $age/60/60/24;
869 $age_str .= " days ago";
870 } elsif ($age > 60*60*2) {
871 $age_str = int $age/60/60;
872 $age_str .= " hours ago";
873 } elsif ($age > 60*2) {
874 $age_str = int $age/60;
875 $age_str .= " min ago";
876 } elsif ($age > 2) {
877 $age_str = int $age;
878 $age_str .= " sec ago";
879 } else {
880 $age_str .= " right now";
881 }
882 return $age_str;
883}
884
885# convert file mode in octal to symbolic file mode string
886sub mode_str {
887 my $mode = oct shift;
888
889 if (S_ISDIR($mode & S_IFMT)) {
890 return 'drwxr-xr-x';
891 } elsif (S_ISLNK($mode)) {
892 return 'lrwxrwxrwx';
893 } elsif (S_ISREG($mode)) {
894 # git cares only about the executable bit
895 if ($mode & S_IXUSR) {
896 return '-rwxr-xr-x';
897 } else {
898 return '-rw-r--r--';
899 };
900 } else {
901 return '----------';
902 }
903}
904
905# convert file mode in octal to file type string
906sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +0200907 my $mode = shift;
908
909 if ($mode !~ m/^[0-7]+$/) {
910 return $mode;
911 } else {
912 $mode = oct $mode;
913 }
Jakub Narebski717b8312006-07-31 21:22:15 +0200914
915 if (S_ISDIR($mode & S_IFMT)) {
916 return "directory";
917 } elsif (S_ISLNK($mode)) {
918 return "symlink";
919 } elsif (S_ISREG($mode)) {
920 return "file";
921 } else {
922 return "unknown";
923 }
924}
925
Jakub Narebski744d0ac2006-11-08 17:59:41 +0100926# convert file mode in octal to file type description string
927sub file_type_long {
928 my $mode = shift;
929
930 if ($mode !~ m/^[0-7]+$/) {
931 return $mode;
932 } else {
933 $mode = oct $mode;
934 }
935
936 if (S_ISDIR($mode & S_IFMT)) {
937 return "directory";
938 } elsif (S_ISLNK($mode)) {
939 return "symlink";
940 } elsif (S_ISREG($mode)) {
941 if ($mode & S_IXUSR) {
942 return "executable";
943 } else {
944 return "file";
945 };
946 } else {
947 return "unknown";
948 }
949}
950
951
Jakub Narebski717b8312006-07-31 21:22:15 +0200952## ----------------------------------------------------------------------
953## functions returning short HTML fragments, or transforming HTML fragments
Pavel Roskin3dff5372007-02-03 23:49:16 -0500954## which don't belong to other sections
Jakub Narebski717b8312006-07-31 21:22:15 +0200955
Junio C Hamano225932e2006-11-09 00:57:13 -0800956# format line of commit message.
Jakub Narebski717b8312006-07-31 21:22:15 +0200957sub format_log_line_html {
958 my $line = shift;
959
Junio C Hamano225932e2006-11-09 00:57:13 -0800960 $line = esc_html($line, -nbsp=>1);
Jakub Narebskibfe21912006-12-10 13:25:49 +0100961 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200962 my $hash_text = $1;
Jakub Narebskibfe21912006-12-10 13:25:49 +0100963 my $link =
964 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
965 -class => "text"}, $hash_text);
966 $line =~ s/$hash_text/$link/;
Jakub Narebski717b8312006-07-31 21:22:15 +0200967 }
968 return $line;
969}
970
971# format marker of refs pointing to given object
Jakub Narebski847e01f2006-08-14 02:05:47 +0200972sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +0200973 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200974 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +0200975
976 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200977 foreach my $ref (@{$refs->{$id}}) {
978 my ($type, $name) = qw();
979 # e.g. tags/v2.6.11 or heads/next
980 if ($ref =~ m!^(.*?)s?/(.*)$!) {
981 $type = $1;
982 $name = $2;
983 } else {
984 $type = "ref";
985 $name = $ref;
986 }
987
Jakub Narebski5fce2782006-12-15 23:49:12 +0100988 $markers .= " <span class=\"$type\" title=\"$ref\">" .
989 esc_html($name) . "</span>";
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200990 }
991 }
992
993 if ($markers) {
994 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +0200995 } else {
996 return "";
997 }
998}
999
Jakub Narebski17d07442006-08-14 02:08:27 +02001000# format, perhaps shortened and with markers, title line
1001sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001002 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +02001003 $extra = '' unless defined($extra);
1004
1005 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +02001006 return $cgi->a({-href => $href, -class => "list subject",
Martin Koegler00f429a2007-06-03 17:42:44 +02001007 -title => to_utf8($long)},
Jakub Narebski17d07442006-08-14 02:08:27 +02001008 esc_html($short) . $extra);
1009 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +02001010 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +02001011 esc_html($long) . $extra);
1012 }
1013}
1014
Jakub Narebski90921742007-06-08 13:27:42 +02001015# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1016sub format_git_diff_header_line {
1017 my $line = shift;
1018 my $diffinfo = shift;
1019 my ($from, $to) = @_;
1020
1021 if ($diffinfo->{'nparents'}) {
1022 # combined diff
1023 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1024 if ($to->{'href'}) {
1025 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1026 esc_path($to->{'file'}));
1027 } else { # file was deleted (no href)
1028 $line .= esc_path($to->{'file'});
1029 }
1030 } else {
1031 # "ordinary" diff
1032 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1033 if ($from->{'href'}) {
1034 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1035 'a/' . esc_path($from->{'file'}));
1036 } else { # file was added (no href)
1037 $line .= 'a/' . esc_path($from->{'file'});
1038 }
1039 $line .= ' ';
1040 if ($to->{'href'}) {
1041 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1042 'b/' . esc_path($to->{'file'}));
1043 } else { # file was deleted
1044 $line .= 'b/' . esc_path($to->{'file'});
1045 }
1046 }
1047
1048 return "<div class=\"diff header\">$line</div>\n";
1049}
1050
1051# format extended diff header line, before patch itself
1052sub format_extended_diff_header_line {
1053 my $line = shift;
1054 my $diffinfo = shift;
1055 my ($from, $to) = @_;
1056
1057 # match <path>
1058 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1059 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1060 esc_path($from->{'file'}));
1061 }
1062 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1063 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1064 esc_path($to->{'file'}));
1065 }
1066 # match single <mode>
1067 if ($line =~ m/\s(\d{6})$/) {
1068 $line .= '<span class="info"> (' .
1069 file_type_long($1) .
1070 ')</span>';
1071 }
1072 # match <hash>
1073 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1074 # can match only for combined diff
1075 $line = 'index ';
1076 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1077 if ($from->{'href'}[$i]) {
1078 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1079 -class=>"hash"},
1080 substr($diffinfo->{'from_id'}[$i],0,7));
1081 } else {
1082 $line .= '0' x 7;
1083 }
1084 # separator
1085 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1086 }
1087 $line .= '..';
1088 if ($to->{'href'}) {
1089 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1090 substr($diffinfo->{'to_id'},0,7));
1091 } else {
1092 $line .= '0' x 7;
1093 }
1094
1095 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1096 # can match only for ordinary diff
1097 my ($from_link, $to_link);
1098 if ($from->{'href'}) {
1099 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1100 substr($diffinfo->{'from_id'},0,7));
1101 } else {
1102 $from_link = '0' x 7;
1103 }
1104 if ($to->{'href'}) {
1105 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1106 substr($diffinfo->{'to_id'},0,7));
1107 } else {
1108 $to_link = '0' x 7;
1109 }
1110 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1111 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1112 }
1113
1114 return $line . "<br/>\n";
1115}
1116
1117# format from-file/to-file diff header
1118sub format_diff_from_to_header {
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001119 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
Jakub Narebski90921742007-06-08 13:27:42 +02001120 my $line;
1121 my $result = '';
1122
1123 $line = $from_line;
1124 #assert($line =~ m/^---/) if DEBUG;
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001125 # no extra formatting for "^--- /dev/null"
1126 if (! $diffinfo->{'nparents'}) {
1127 # ordinary (single parent) diff
1128 if ($line =~ m!^--- "?a/!) {
1129 if ($from->{'href'}) {
1130 $line = '--- a/' .
1131 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1132 esc_path($from->{'file'}));
1133 } else {
1134 $line = '--- a/' .
1135 esc_path($from->{'file'});
1136 }
1137 }
1138 $result .= qq!<div class="diff from_file">$line</div>\n!;
1139
1140 } else {
1141 # combined diff (merge commit)
1142 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1143 if ($from->{'href'}[$i]) {
1144 $line = '--- ' .
Jakub Narebski91af4ce2007-06-08 13:32:44 +02001145 $cgi->a({-href=>href(action=>"blobdiff",
1146 hash_parent=>$diffinfo->{'from_id'}[$i],
1147 hash_parent_base=>$parents[$i],
1148 file_parent=>$from->{'file'}[$i],
1149 hash=>$diffinfo->{'to_id'},
1150 hash_base=>$hash,
1151 file_name=>$to->{'file'}),
1152 -class=>"path",
1153 -title=>"diff" . ($i+1)},
1154 $i+1) .
1155 '/' .
Jakub Narebskideaa01a2007-06-08 13:29:49 +02001156 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1157 esc_path($from->{'file'}[$i]));
1158 } else {
1159 $line = '--- /dev/null';
1160 }
1161 $result .= qq!<div class="diff from_file">$line</div>\n!;
Jakub Narebski90921742007-06-08 13:27:42 +02001162 }
1163 }
Jakub Narebski90921742007-06-08 13:27:42 +02001164
1165 $line = $to_line;
1166 #assert($line =~ m/^\+\+\+/) if DEBUG;
1167 # no extra formatting for "^+++ /dev/null"
1168 if ($line =~ m!^\+\+\+ "?b/!) {
1169 if ($to->{'href'}) {
1170 $line = '+++ b/' .
1171 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1172 esc_path($to->{'file'}));
1173 } else {
1174 $line = '+++ b/' .
1175 esc_path($to->{'file'});
1176 }
1177 }
1178 $result .= qq!<div class="diff to_file">$line</div>\n!;
1179
1180 return $result;
1181}
1182
Jakub Narebskicd030c32007-06-08 13:33:28 +02001183# create note for patch simplified by combined diff
1184sub format_diff_cc_simplified {
1185 my ($diffinfo, @parents) = @_;
1186 my $result = '';
1187
1188 $result .= "<div class=\"diff header\">" .
1189 "diff --cc ";
1190 if (!is_deleted($diffinfo)) {
1191 $result .= $cgi->a({-href => href(action=>"blob",
1192 hash_base=>$hash,
1193 hash=>$diffinfo->{'to_id'},
1194 file_name=>$diffinfo->{'to_file'}),
1195 -class => "path"},
1196 esc_path($diffinfo->{'to_file'}));
1197 } else {
1198 $result .= esc_path($diffinfo->{'to_file'});
1199 }
1200 $result .= "</div>\n" . # class="diff header"
1201 "<div class=\"diff nodifferences\">" .
1202 "Simple merge" .
1203 "</div>\n"; # class="diff nodifferences"
1204
1205 return $result;
1206}
1207
Jakub Narebski90921742007-06-08 13:27:42 +02001208# format patch (diff) line (not to be used for diff headers)
Jakub Narebskieee08902006-08-24 00:15:14 +02001209sub format_diff_line {
1210 my $line = shift;
Jakub Narebski59e3b142006-11-18 23:35:40 +01001211 my ($from, $to) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001212 my $diff_class = "";
1213
1214 chomp $line;
1215
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001216 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1217 # combined diff
1218 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1219 if ($line =~ m/^\@{3}/) {
1220 $diff_class = " chunk_header";
1221 } elsif ($line =~ m/^\\/) {
1222 $diff_class = " incomplete";
1223 } elsif ($prefix =~ tr/+/+/) {
1224 $diff_class = " add";
1225 } elsif ($prefix =~ tr/-/-/) {
1226 $diff_class = " rem";
1227 }
1228 } else {
1229 # assume ordinary diff
1230 my $char = substr($line, 0, 1);
1231 if ($char eq '+') {
1232 $diff_class = " add";
1233 } elsif ($char eq '-') {
1234 $diff_class = " rem";
1235 } elsif ($char eq '@') {
1236 $diff_class = " chunk_header";
1237 } elsif ($char eq "\\") {
1238 $diff_class = " incomplete";
1239 }
Jakub Narebskieee08902006-08-24 00:15:14 +02001240 }
1241 $line = untabify($line);
Jakub Narebski59e3b142006-11-18 23:35:40 +01001242 if ($from && $to && $line =~ m/^\@{2} /) {
1243 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1244 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1245
1246 $from_lines = 0 unless defined $from_lines;
1247 $to_lines = 0 unless defined $to_lines;
1248
1249 if ($from->{'href'}) {
1250 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1251 -class=>"list"}, $from_text);
1252 }
1253 if ($to->{'href'}) {
1254 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1255 -class=>"list"}, $to_text);
1256 }
1257 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1258 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1259 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02001260 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1261 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1262 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1263
1264 @from_text = split(' ', $ranges);
1265 for (my $i = 0; $i < @from_text; ++$i) {
1266 ($from_start[$i], $from_nlines[$i]) =
1267 (split(',', substr($from_text[$i], 1)), 0);
1268 }
1269
1270 $to_text = pop @from_text;
1271 $to_start = pop @from_start;
1272 $to_nlines = pop @from_nlines;
1273
1274 $line = "<span class=\"chunk_info\">$prefix ";
1275 for (my $i = 0; $i < @from_text; ++$i) {
1276 if ($from->{'href'}[$i]) {
1277 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1278 -class=>"list"}, $from_text[$i]);
1279 } else {
1280 $line .= $from_text[$i];
1281 }
1282 $line .= " ";
1283 }
1284 if ($to->{'href'}) {
1285 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1286 -class=>"list"}, $to_text);
1287 } else {
1288 $line .= $to_text;
1289 }
1290 $line .= " $prefix</span>" .
1291 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1292 return "<div class=\"diff$diff_class\">$line</div>\n";
Jakub Narebski59e3b142006-11-18 23:35:40 +01001293 }
Jakub Narebski6255ef02006-11-01 14:33:21 +01001294 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001295}
1296
Matt McCutchena3c8ab32007-07-22 01:30:27 +02001297# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1298# linked. Pass the hash of the tree/commit to snapshot.
1299sub format_snapshot_links {
1300 my ($hash) = @_;
1301 my @snapshot_fmts = gitweb_check_feature('snapshot');
1302 my $num_fmts = @snapshot_fmts;
1303 if ($num_fmts > 1) {
1304 # A parenthesized list of links bearing format names.
1305 return "snapshot (" . join(' ', map
1306 $cgi->a({
1307 -href => href(
1308 action=>"snapshot",
1309 hash=>$hash,
1310 snapshot_format=>$_
1311 )
1312 }, $known_snapshot_formats{$_}{'display'})
1313 , @snapshot_fmts) . ")";
1314 } elsif ($num_fmts == 1) {
1315 # A single "snapshot" link whose tooltip bears the format name.
1316 my ($fmt) = @snapshot_fmts;
1317 return $cgi->a({
1318 -href => href(
1319 action=>"snapshot",
1320 hash=>$hash,
1321 snapshot_format=>$fmt
1322 ),
1323 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1324 }, "snapshot");
1325 } else { # $num_fmts == 0
1326 return undef;
1327 }
1328}
1329
Jakub Narebski717b8312006-07-31 21:22:15 +02001330## ----------------------------------------------------------------------
1331## git utility subroutines, invoking git commands
1332
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001333# returns path to the core git executable and the --git-dir parameter as list
1334sub git_cmd {
1335 return $GIT, '--git-dir='.$git_dir;
1336}
1337
1338# returns path to the core git executable and the --git-dir parameter as string
1339sub git_cmd_str {
1340 return join(' ', git_cmd());
1341}
1342
Jakub Narebski717b8312006-07-31 21:22:15 +02001343# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +02001344sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +02001345 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001346 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001347 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001348 $git_dir = "$projectroot/$project";
1349 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +02001350 my $head = <$fd>;
1351 close $fd;
1352 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1353 $retval = $1;
1354 }
1355 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001356 if (defined $o_git_dir) {
1357 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +02001358 }
1359 return $retval;
1360}
1361
1362# get type of given object
1363sub git_get_type {
1364 my $hash = shift;
1365
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001366 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001367 my $type = <$fd>;
1368 close $fd or return;
1369 chomp $type;
1370 return $type;
1371}
1372
1373sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301374 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001375
1376 return unless ($key);
1377 $key =~ s/^gitweb\.//;
1378 return if ($key =~ m/\W/);
1379
Tom Princee0d10e12007-01-28 16:16:53 -08001380 my @x = (git_cmd(), 'config');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301381 if (defined $type) { push @x, $type; }
1382 push @x, "--get";
1383 push @x, "gitweb.$key";
1384 my $val = qx(@x);
1385 chomp $val;
Jakub Narebski717b8312006-07-31 21:22:15 +02001386 return ($val);
1387}
1388
Jakub Narebski717b8312006-07-31 21:22:15 +02001389# get hash of given path at given ref
1390sub git_get_hash_by_path {
1391 my $base = shift;
1392 my $path = shift || return undef;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001393 my $type = shift;
Jakub Narebski717b8312006-07-31 21:22:15 +02001394
Jakub Narebski4b02f482006-09-26 01:54:24 +02001395 $path =~ s,/+$,,;
Jakub Narebski717b8312006-07-31 21:22:15 +02001396
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001397 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Jakub Narebskicac4bd92006-08-05 13:13:53 +02001398 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02001399 my $line = <$fd>;
1400 close $fd or return undef;
1401
Jakub Narebski198a2a82007-05-12 21:16:34 +02001402 if (!defined $line) {
1403 # there is no tree or hash given by $path at $base
1404 return undef;
1405 }
1406
Jakub Narebski717b8312006-07-31 21:22:15 +02001407 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01001408 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
Jakub Narebski1d782b02006-09-21 18:09:12 +02001409 if (defined $type && $type ne $2) {
1410 # type doesn't match
1411 return undef;
1412 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001413 return $3;
1414}
1415
Jakub Narebskied224de2007-05-07 01:10:04 +02001416# get path of entry with given hash at given tree-ish (ref)
1417# used to get 'from' filename for combined diff (merge commit) for renames
1418sub git_get_path_by_hash {
1419 my $base = shift || return;
1420 my $hash = shift || return;
1421
1422 local $/ = "\0";
1423
1424 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1425 or return undef;
1426 while (my $line = <$fd>) {
1427 chomp $line;
1428
1429 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1430 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1431 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1432 close $fd;
1433 return $1;
1434 }
1435 }
1436 close $fd;
1437 return undef;
1438}
1439
Jakub Narebski717b8312006-07-31 21:22:15 +02001440## ......................................................................
1441## git utility functions, directly accessing git repository
1442
Jakub Narebski847e01f2006-08-14 02:05:47 +02001443sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +02001444 my $path = shift;
1445
1446 open my $fd, "$projectroot/$path/description" or return undef;
1447 my $descr = <$fd>;
1448 close $fd;
Junio C Hamano2eb54ef2007-05-16 21:04:16 -07001449 if (defined $descr) {
1450 chomp $descr;
1451 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001452 return $descr;
1453}
1454
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02001455sub git_get_project_url_list {
1456 my $path = shift;
1457
Petr Baudis74d61662006-09-23 01:15:18 +02001458 open my $fd, "$projectroot/$path/cloneurl" or return;
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02001459 my @git_project_url_list = map { chomp; $_ } <$fd>;
1460 close $fd;
1461
1462 return wantarray ? @git_project_url_list : \@git_project_url_list;
1463}
1464
Jakub Narebski847e01f2006-08-14 02:05:47 +02001465sub git_get_projects_list {
Petr Baudise30496d2006-10-24 05:33:17 +02001466 my ($filter) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +02001467 my @list;
1468
Petr Baudise30496d2006-10-24 05:33:17 +02001469 $filter ||= '';
1470 $filter =~ s/\.git$//;
1471
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001472 my ($check_forks) = gitweb_check_feature('forks');
1473
Jakub Narebski717b8312006-07-31 21:22:15 +02001474 if (-d $projects_list) {
1475 # search in directory
Petr Baudise30496d2006-10-24 05:33:17 +02001476 my $dir = $projects_list . ($filter ? "/$filter" : '');
Aneesh Kumar K.V6768d6b2006-11-03 10:41:45 +05301477 # remove the trailing "/"
1478 $dir =~ s!/+$!!;
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001479 my $pfxlen = length("$dir");
1480
1481 File::Find::find({
1482 follow_fast => 1, # follow symbolic links
1483 dangling_symlinks => 0, # ignore dangling symlinks, silently
1484 wanted => sub {
1485 # skip project-list toplevel, if we get it.
1486 return if (m!^[/.]$!);
1487 # only directories can be git repositories
1488 return unless (-d $_);
1489
1490 my $subdir = substr($File::Find::name, $pfxlen + 1);
1491 # we check related file in $projectroot
Petr Baudise30496d2006-10-24 05:33:17 +02001492 if ($check_forks and $subdir =~ m#/.#) {
1493 $File::Find::prune = 1;
1494 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1495 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
Jakub Narebskic0011ff2006-09-14 22:18:59 +02001496 $File::Find::prune = 1;
1497 }
1498 },
1499 }, "$dir");
1500
Jakub Narebski717b8312006-07-31 21:22:15 +02001501 } elsif (-f $projects_list) {
1502 # read from file(url-encoded):
1503 # 'git%2Fgit.git Linus+Torvalds'
1504 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1505 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001506 my %paths;
Jakub Narebskidd1ad5f2006-09-26 01:56:17 +02001507 open my ($fd), $projects_list or return;
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001508 PROJECT:
Jakub Narebski717b8312006-07-31 21:22:15 +02001509 while (my $line = <$fd>) {
1510 chomp $line;
1511 my ($path, $owner) = split ' ', $line;
1512 $path = unescape($path);
1513 $owner = unescape($owner);
1514 if (!defined $path) {
1515 next;
1516 }
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001517 if ($filter ne '') {
1518 # looking for forks;
1519 my $pfx = substr($path, 0, length($filter));
1520 if ($pfx ne $filter) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001521 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001522 }
1523 my $sfx = substr($path, length($filter));
1524 if ($sfx !~ /^\/.*\.git$/) {
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001525 next PROJECT;
1526 }
1527 } elsif ($check_forks) {
1528 PATH:
1529 foreach my $filter (keys %paths) {
1530 # looking for forks;
1531 my $pfx = substr($path, 0, length($filter));
1532 if ($pfx ne $filter) {
1533 next PATH;
1534 }
1535 my $sfx = substr($path, length($filter));
1536 if ($sfx !~ /^\/.*\.git$/) {
1537 next PATH;
1538 }
1539 # is a fork, don't include it in
1540 # the list
1541 next PROJECT;
Junio C Hamano83ee94c2006-11-07 22:37:17 -08001542 }
1543 }
Junio C Hamano2172ce42006-10-03 02:30:47 -07001544 if (check_export_ok("$projectroot/$path")) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001545 my $pr = {
1546 path => $path,
Martin Koegler00f429a2007-06-03 17:42:44 +02001547 owner => to_utf8($owner),
Jakub Narebski717b8312006-07-31 21:22:15 +02001548 };
Frank Lichtenheldc2b8b132007-04-06 23:58:11 +02001549 push @list, $pr;
1550 (my $forks_path = $path) =~ s/\.git$//;
1551 $paths{$forks_path}++;
Jakub Narebski717b8312006-07-31 21:22:15 +02001552 }
1553 }
1554 close $fd;
1555 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001556 return @list;
1557}
1558
Junio C Hamano47852452007-07-03 22:10:42 -07001559our $gitweb_project_owner = undef;
1560sub git_get_project_list_from_file {
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001561
Junio C Hamano47852452007-07-03 22:10:42 -07001562 return if (defined $gitweb_project_owner);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001563
Junio C Hamano47852452007-07-03 22:10:42 -07001564 $gitweb_project_owner = {};
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001565 # read from file (url-encoded):
1566 # 'git%2Fgit.git Linus+Torvalds'
1567 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1568 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1569 if (-f $projects_list) {
1570 open (my $fd , $projects_list);
1571 while (my $line = <$fd>) {
1572 chomp $line;
1573 my ($pr, $ow) = split ' ', $line;
1574 $pr = unescape($pr);
1575 $ow = unescape($ow);
Junio C Hamano47852452007-07-03 22:10:42 -07001576 $gitweb_project_owner->{$pr} = to_utf8($ow);
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001577 }
1578 close $fd;
1579 }
Junio C Hamano47852452007-07-03 22:10:42 -07001580}
1581
1582sub git_get_project_owner {
1583 my $project = shift;
1584 my $owner;
1585
1586 return undef unless $project;
1587
1588 if (!defined $gitweb_project_owner) {
1589 git_get_project_list_from_file();
1590 }
1591
1592 if (exists $gitweb_project_owner->{$project}) {
1593 $owner = $gitweb_project_owner->{$project};
1594 }
Jakub Narebski1e0cf032006-08-14 02:10:06 +02001595 if (!defined $owner) {
1596 $owner = get_file_owner("$projectroot/$project");
1597 }
1598
1599 return $owner;
1600}
1601
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001602sub git_get_last_activity {
1603 my ($path) = @_;
1604 my $fd;
1605
1606 $git_dir = "$projectroot/$path";
1607 open($fd, "-|", git_cmd(), 'for-each-ref',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00001608 '--format=%(committer)',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001609 '--sort=-committerdate',
Robert Fitzsimons0ff5ec72006-12-22 19:38:13 +00001610 '--count=1',
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001611 'refs/heads') or return;
1612 my $most_recent = <$fd>;
1613 close $fd or return;
Jakub Narebski785cdea2007-05-13 12:39:22 +02001614 if (defined $most_recent &&
1615 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001616 my $timestamp = $1;
1617 my $age = time - $timestamp;
1618 return ($age, age_string($age));
1619 }
Matt McCutchenc9563952007-06-28 18:15:22 -04001620 return (undef, undef);
Jakub Narebskic60c56c2006-10-28 19:43:40 +02001621}
1622
Jakub Narebski847e01f2006-08-14 02:05:47 +02001623sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +02001624 my $type = shift || "";
1625 my %refs;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01001626 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1627 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1628 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1629 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
Jakub Narebski9704d752006-09-19 14:31:49 +02001630 or return;
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001631
Jakub Narebski717b8312006-07-31 21:22:15 +02001632 while (my $line = <$fd>) {
1633 chomp $line;
Jakub Narebski28b9d9f2006-11-25 11:32:08 +01001634 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001635 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001636 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02001637 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +02001638 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +02001639 }
1640 }
1641 }
1642 close $fd or return;
1643 return \%refs;
1644}
1645
Jakub Narebski56a322f2006-08-24 19:41:23 +02001646sub git_get_rev_name_tags {
1647 my $hash = shift || return undef;
1648
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001649 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +02001650 or return;
1651 my $name_rev = <$fd>;
1652 close $fd;
1653
1654 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1655 return $1;
1656 } else {
1657 # catches also '$hash undefined' output
1658 return undef;
1659 }
1660}
1661
Jakub Narebski717b8312006-07-31 21:22:15 +02001662## ----------------------------------------------------------------------
1663## parse to hash functions
1664
Jakub Narebski847e01f2006-08-14 02:05:47 +02001665sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +02001666 my $epoch = shift;
1667 my $tz = shift || "-0000";
1668
1669 my %date;
1670 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1671 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1672 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1673 $date{'hour'} = $hour;
1674 $date{'minute'} = $min;
1675 $date{'mday'} = $mday;
1676 $date{'day'} = $days[$wday];
1677 $date{'month'} = $months[$mon];
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001678 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1679 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
Jakub Narebski952c65f2006-08-22 16:52:50 +02001680 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1681 $mday, $months[$mon], $hour ,$min;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001682 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1683 1900+$year, $mon, $mday, $hour ,$min, $sec;
Jakub Narebski717b8312006-07-31 21:22:15 +02001684
1685 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1686 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1687 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1688 $date{'hour_local'} = $hour;
1689 $date{'minute_local'} = $min;
1690 $date{'tz_local'} = $tz;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01001691 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1692 1900+$year, $mon+1, $mday,
1693 $hour, $min, $sec, $tz);
Jakub Narebski717b8312006-07-31 21:22:15 +02001694 return %date;
1695}
1696
Jakub Narebski847e01f2006-08-14 02:05:47 +02001697sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +02001698 my $tag_id = shift;
1699 my %tag;
1700 my @comment;
1701
Dennis Stosberg25691fb2006-08-28 17:49:58 +02001702 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +02001703 $tag{'id'} = $tag_id;
1704 while (my $line = <$fd>) {
1705 chomp $line;
1706 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1707 $tag{'object'} = $1;
1708 } elsif ($line =~ m/^type (.+)$/) {
1709 $tag{'type'} = $1;
1710 } elsif ($line =~ m/^tag (.+)$/) {
1711 $tag{'name'} = $1;
1712 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1713 $tag{'author'} = $1;
1714 $tag{'epoch'} = $2;
1715 $tag{'tz'} = $3;
1716 } elsif ($line =~ m/--BEGIN/) {
1717 push @comment, $line;
1718 last;
1719 } elsif ($line eq "") {
1720 last;
1721 }
1722 }
1723 push @comment, <$fd>;
1724 $tag{'comment'} = \@comment;
1725 close $fd or return;
1726 if (!defined $tag{'name'}) {
1727 return
1728 };
1729 return %tag
1730}
1731
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001732sub parse_commit_text {
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001733 my ($commit_text, $withparents) = @_;
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001734 my @commit_lines = split '\n', $commit_text;
Jakub Narebski717b8312006-07-31 21:22:15 +02001735 my %co;
1736
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001737 pop @commit_lines; # Remove '\0'
1738
Jakub Narebski198a2a82007-05-12 21:16:34 +02001739 if (! @commit_lines) {
1740 return;
1741 }
1742
Jakub Narebski717b8312006-07-31 21:22:15 +02001743 my $header = shift @commit_lines;
Jakub Narebski198a2a82007-05-12 21:16:34 +02001744 if ($header !~ m/^[0-9a-fA-F]{40}/) {
Jakub Narebski717b8312006-07-31 21:22:15 +02001745 return;
1746 }
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001747 ($co{'id'}, my @parents) = split ' ', $header;
Jakub Narebski717b8312006-07-31 21:22:15 +02001748 while (my $line = shift @commit_lines) {
1749 last if $line eq "\n";
1750 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1751 $co{'tree'} = $1;
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001752 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00001753 push @parents, $1;
Jakub Narebski717b8312006-07-31 21:22:15 +02001754 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1755 $co{'author'} = $1;
1756 $co{'author_epoch'} = $2;
1757 $co{'author_tz'} = $3;
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01001758 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1759 $co{'author_name'} = $1;
1760 $co{'author_email'} = $2;
Jakub Narebski717b8312006-07-31 21:22:15 +02001761 } else {
1762 $co{'author_name'} = $co{'author'};
1763 }
1764 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1765 $co{'committer'} = $1;
1766 $co{'committer_epoch'} = $2;
1767 $co{'committer_tz'} = $3;
1768 $co{'committer_name'} = $co{'committer'};
Jakub Narebskiba00b8c2006-11-25 15:54:32 +01001769 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1770 $co{'committer_name'} = $1;
1771 $co{'committer_email'} = $2;
1772 } else {
1773 $co{'committer_name'} = $co{'committer'};
1774 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001775 }
1776 }
1777 if (!defined $co{'tree'}) {
1778 return;
1779 };
Robert Fitzsimons208b2df2006-12-24 14:31:43 +00001780 $co{'parents'} = \@parents;
1781 $co{'parent'} = $parents[0];
Jakub Narebski717b8312006-07-31 21:22:15 +02001782
1783 foreach my $title (@commit_lines) {
1784 $title =~ s/^ //;
1785 if ($title ne "") {
1786 $co{'title'} = chop_str($title, 80, 5);
1787 # remove leading stuff of merges to make the interesting part visible
1788 if (length($title) > 50) {
1789 $title =~ s/^Automatic //;
1790 $title =~ s/^merge (of|with) /Merge ... /i;
1791 if (length($title) > 50) {
1792 $title =~ s/(http|rsync):\/\///;
1793 }
1794 if (length($title) > 50) {
1795 $title =~ s/(master|www|rsync)\.//;
1796 }
1797 if (length($title) > 50) {
1798 $title =~ s/kernel.org:?//;
1799 }
1800 if (length($title) > 50) {
1801 $title =~ s/\/pub\/scm//;
1802 }
1803 }
1804 $co{'title_short'} = chop_str($title, 50, 5);
1805 last;
1806 }
1807 }
Petr Baudis7e0fe5c2006-10-06 18:55:04 +02001808 if ($co{'title'} eq "") {
1809 $co{'title'} = $co{'title_short'} = '(no commit message)';
1810 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001811 # remove added spaces
1812 foreach my $line (@commit_lines) {
1813 $line =~ s/^ //;
1814 }
1815 $co{'comment'} = \@commit_lines;
1816
1817 my $age = time - $co{'committer_epoch'};
1818 $co{'age'} = $age;
1819 $co{'age_string'} = age_string($age);
1820 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1821 if ($age > 60*60*24*7*2) {
1822 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1823 $co{'age_string_age'} = $co{'age_string'};
1824 } else {
1825 $co{'age_string_date'} = $co{'age_string'};
1826 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1827 }
1828 return %co;
1829}
1830
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001831sub parse_commit {
1832 my ($commit_id) = @_;
1833 my %co;
1834
1835 local $/ = "\0";
1836
1837 open my $fd, "-|", git_cmd(), "rev-list",
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001838 "--parents",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001839 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001840 "--max-count=1",
1841 $commit_id,
1842 "--",
1843 or die_error(undef, "Open git-rev-list failed");
Robert Fitzsimonsccdfdea2006-12-27 14:22:21 +00001844 %co = parse_commit_text(<$fd>, 1);
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001845 close $fd;
1846
1847 return %co;
1848}
1849
1850sub parse_commits {
1851 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1852 my @cos;
1853
1854 $maxcount ||= 1;
1855 $skip ||= 0;
1856
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001857 local $/ = "\0";
1858
1859 open my $fd, "-|", git_cmd(), "rev-list",
1860 "--header",
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001861 ($arg ? ($arg) : ()),
1862 ("--max-count=" . $maxcount),
Robert Fitzsimonsf47efbb2006-12-24 14:31:49 +00001863 ("--skip=" . $skip),
Miklos Vajna868bc062007-07-12 20:39:27 +02001864 @extra_options,
Robert Fitzsimons756bbf52006-12-24 14:31:42 +00001865 $commit_id,
1866 "--",
1867 ($filename ? ($filename) : ())
1868 or die_error(undef, "Open git-rev-list failed");
1869 while (my $line = <$fd>) {
1870 my %co = parse_commit_text($line);
1871 push @cos, \%co;
1872 }
1873 close $fd;
1874
1875 return wantarray ? @cos : \@cos;
1876}
1877
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001878# parse ref from ref_file, given by ref_id, with given type
1879sub parse_ref {
1880 my $ref_file = shift;
1881 my $ref_id = shift;
1882 my $type = shift || git_get_type($ref_id);
1883 my %ref_item;
1884
1885 $ref_item{'type'} = $type;
1886 $ref_item{'id'} = $ref_id;
1887 $ref_item{'epoch'} = 0;
1888 $ref_item{'age'} = "unknown";
1889 if ($type eq "tag") {
1890 my %tag = parse_tag($ref_id);
1891 $ref_item{'comment'} = $tag{'comment'};
1892 if ($tag{'type'} eq "commit") {
1893 my %co = parse_commit($tag{'object'});
1894 $ref_item{'epoch'} = $co{'committer_epoch'};
1895 $ref_item{'age'} = $co{'age_string'};
1896 } elsif (defined($tag{'epoch'})) {
1897 my $age = time - $tag{'epoch'};
1898 $ref_item{'epoch'} = $tag{'epoch'};
1899 $ref_item{'age'} = age_string($age);
1900 }
1901 $ref_item{'reftype'} = $tag{'type'};
1902 $ref_item{'name'} = $tag{'name'};
1903 $ref_item{'refid'} = $tag{'object'};
1904 } elsif ($type eq "commit"){
1905 my %co = parse_commit($ref_id);
1906 $ref_item{'reftype'} = "commit";
1907 $ref_item{'name'} = $ref_file;
1908 $ref_item{'title'} = $co{'title'};
1909 $ref_item{'refid'} = $ref_id;
1910 $ref_item{'epoch'} = $co{'committer_epoch'};
1911 $ref_item{'age'} = $co{'age_string'};
1912 } else {
1913 $ref_item{'reftype'} = $type;
1914 $ref_item{'name'} = $ref_file;
1915 $ref_item{'refid'} = $ref_id;
1916 }
1917
1918 return %ref_item;
1919}
1920
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001921# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02001922sub parse_difftree_raw_line {
1923 my $line = shift;
1924 my %res;
1925
1926 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1927 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1928 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1929 $res{'from_mode'} = $1;
1930 $res{'to_mode'} = $2;
1931 $res{'from_id'} = $3;
1932 $res{'to_id'} = $4;
1933 $res{'status'} = $5;
1934 $res{'similarity'} = $6;
1935 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001936 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02001937 } else {
1938 $res{'file'} = unquote($7);
1939 }
1940 }
Jakub Narebski78bc4032007-05-07 01:10:03 +02001941 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1942 # combined diff (for merge commit)
1943 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1944 $res{'nparents'} = length($1);
1945 $res{'from_mode'} = [ split(' ', $2) ];
1946 $res{'to_mode'} = pop @{$res{'from_mode'}};
1947 $res{'from_id'} = [ split(' ', $3) ];
1948 $res{'to_id'} = pop @{$res{'from_id'}};
1949 $res{'status'} = [ split('', $4) ];
1950 $res{'to_file'} = unquote($5);
1951 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02001952 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02001953 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1954 $res{'commit'} = $1;
1955 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02001956
1957 return wantarray ? %res : \%res;
1958}
1959
Jakub Narebskicb849b42006-08-31 00:32:15 +02001960# parse line of git-ls-tree output
1961sub parse_ls_tree_line ($;%) {
1962 my $line = shift;
1963 my %opts = @_;
1964 my %res;
1965
1966 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
Jakub Narebski8b4b94c2006-10-30 22:25:11 +01001967 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
Jakub Narebskicb849b42006-08-31 00:32:15 +02001968
1969 $res{'mode'} = $1;
1970 $res{'type'} = $2;
1971 $res{'hash'} = $3;
1972 if ($opts{'-z'}) {
1973 $res{'name'} = $4;
1974 } else {
1975 $res{'name'} = unquote($4);
1976 }
1977
1978 return wantarray ? %res : \%res;
1979}
1980
Jakub Narebski90921742007-06-08 13:27:42 +02001981# generates _two_ hashes, references to which are passed as 2 and 3 argument
1982sub parse_from_to_diffinfo {
1983 my ($diffinfo, $from, $to, @parents) = @_;
1984
1985 if ($diffinfo->{'nparents'}) {
1986 # combined diff
1987 $from->{'file'} = [];
1988 $from->{'href'} = [];
1989 fill_from_file_info($diffinfo, @parents)
1990 unless exists $diffinfo->{'from_file'};
1991 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1992 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
1993 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
1994 $from->{'href'}[$i] = href(action=>"blob",
1995 hash_base=>$parents[$i],
1996 hash=>$diffinfo->{'from_id'}[$i],
1997 file_name=>$from->{'file'}[$i]);
1998 } else {
1999 $from->{'href'}[$i] = undef;
2000 }
2001 }
2002 } else {
2003 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2004 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2005 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2006 hash=>$diffinfo->{'from_id'},
2007 file_name=>$from->{'file'});
2008 } else {
2009 delete $from->{'href'};
2010 }
2011 }
2012
2013 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2014 if (!is_deleted($diffinfo)) { # file exists in result
2015 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2016 hash=>$diffinfo->{'to_id'},
2017 file_name=>$to->{'file'});
2018 } else {
2019 delete $to->{'href'};
2020 }
2021}
2022
Jakub Narebski717b8312006-07-31 21:22:15 +02002023## ......................................................................
2024## parse to array of hashes functions
2025
Jakub Narebskicd146402006-11-02 20:23:11 +01002026sub git_get_heads_list {
2027 my $limit = shift;
2028 my @headslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002029
Jakub Narebskicd146402006-11-02 20:23:11 +01002030 open my $fd, '-|', git_cmd(), 'for-each-ref',
2031 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2032 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2033 'refs/heads'
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002034 or return;
2035 while (my $line = <$fd>) {
Jakub Narebskicd146402006-11-02 20:23:11 +01002036 my %ref_item;
Jakub Narebski120ddde2006-09-19 14:33:22 +02002037
Jakub Narebskicd146402006-11-02 20:23:11 +01002038 chomp $line;
2039 my ($refinfo, $committerinfo) = split(/\0/, $line);
2040 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2041 my ($committer, $epoch, $tz) =
2042 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2043 $name =~ s!^refs/heads/!!;
2044
2045 $ref_item{'name'} = $name;
2046 $ref_item{'id'} = $hash;
2047 $ref_item{'title'} = $title || '(no commit message)';
2048 $ref_item{'epoch'} = $epoch;
2049 if ($epoch) {
2050 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2051 } else {
2052 $ref_item{'age'} = "unknown";
Jakub Narebski717b8312006-07-31 21:22:15 +02002053 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002054
2055 push @headslist, \%ref_item;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002056 }
2057 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07002058
Jakub Narebskicd146402006-11-02 20:23:11 +01002059 return wantarray ? @headslist : \@headslist;
2060}
Jakub Narebskic83a77e2006-09-15 03:43:28 +02002061
Jakub Narebskicd146402006-11-02 20:23:11 +01002062sub git_get_tags_list {
2063 my $limit = shift;
2064 my @tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002065
Jakub Narebskicd146402006-11-02 20:23:11 +01002066 open my $fd, '-|', git_cmd(), 'for-each-ref',
2067 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2068 '--format=%(objectname) %(objecttype) %(refname) '.
2069 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2070 'refs/tags'
2071 or return;
2072 while (my $line = <$fd>) {
2073 my %ref_item;
2074
2075 chomp $line;
2076 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2077 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2078 my ($creator, $epoch, $tz) =
2079 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2080 $name =~ s!^refs/tags/!!;
2081
2082 $ref_item{'type'} = $type;
2083 $ref_item{'id'} = $id;
2084 $ref_item{'name'} = $name;
2085 if ($type eq "tag") {
2086 $ref_item{'subject'} = $title;
2087 $ref_item{'reftype'} = $reftype;
2088 $ref_item{'refid'} = $refid;
2089 } else {
2090 $ref_item{'reftype'} = $type;
2091 $ref_item{'refid'} = $id;
2092 }
2093
2094 if ($type eq "tag" || $type eq "commit") {
2095 $ref_item{'epoch'} = $epoch;
2096 if ($epoch) {
2097 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2098 } else {
2099 $ref_item{'age'} = "unknown";
2100 }
2101 }
2102
2103 push @tagslist, \%ref_item;
Jakub Narebski717b8312006-07-31 21:22:15 +02002104 }
Jakub Narebskicd146402006-11-02 20:23:11 +01002105 close $fd;
2106
2107 return wantarray ? @tagslist : \@tagslist;
Jakub Narebski717b8312006-07-31 21:22:15 +02002108}
2109
2110## ----------------------------------------------------------------------
2111## filesystem-related functions
2112
2113sub get_file_owner {
2114 my $path = shift;
2115
2116 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2117 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2118 if (!defined $gcos) {
2119 return undef;
2120 }
2121 my $owner = $gcos;
2122 $owner =~ s/[,;].*$//;
Martin Koegler00f429a2007-06-03 17:42:44 +02002123 return to_utf8($owner);
Jakub Narebski717b8312006-07-31 21:22:15 +02002124}
2125
2126## ......................................................................
2127## mimetype related functions
2128
2129sub mimetype_guess_file {
2130 my $filename = shift;
2131 my $mimemap = shift;
2132 -r $mimemap or return undef;
2133
2134 my %mimemap;
2135 open(MIME, $mimemap) or return undef;
2136 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02002137 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02002138 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07002139 if (defined $exts) {
2140 my @exts = split(/\s+/, $exts);
2141 foreach my $ext (@exts) {
2142 $mimemap{$ext} = $mime;
2143 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002144 }
2145 }
2146 close(MIME);
2147
Jakub Narebski80593192006-09-19 13:57:03 +02002148 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02002149 return $mimemap{$1};
2150}
2151
2152sub mimetype_guess {
2153 my $filename = shift;
2154 my $mime;
2155 $filename =~ /\./ or return undef;
2156
2157 if ($mimetypes_file) {
2158 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02002159 if ($file !~ m!^/!) { # if it is relative path
2160 # it is relative to project
2161 $file = "$projectroot/$project/$file";
2162 }
Jakub Narebski717b8312006-07-31 21:22:15 +02002163 $mime = mimetype_guess_file($filename, $file);
2164 }
2165 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2166 return $mime;
2167}
2168
Jakub Narebski847e01f2006-08-14 02:05:47 +02002169sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02002170 my $fd = shift;
2171 my $filename = shift;
2172
2173 if ($filename) {
2174 my $mime = mimetype_guess($filename);
2175 $mime and return $mime;
2176 }
2177
2178 # just in case
2179 return $default_blob_plain_mimetype unless $fd;
2180
2181 if (-T $fd) {
2182 return 'text/plain' .
2183 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2184 } elsif (! $filename) {
2185 return 'application/octet-stream';
2186 } elsif ($filename =~ m/\.png$/i) {
2187 return 'image/png';
2188 } elsif ($filename =~ m/\.gif$/i) {
2189 return 'image/gif';
2190 } elsif ($filename =~ m/\.jpe?g$/i) {
2191 return 'image/jpeg';
2192 } else {
2193 return 'application/octet-stream';
2194 }
2195}
2196
2197## ======================================================================
2198## functions printing HTML: header, footer, error page
2199
Kay Sievers12a88f22005-08-07 20:02:47 +02002200sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02002201 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02002202 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002203
Petr Baudis8be28902006-10-24 05:18:39 +02002204 my $title = "$site_name";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002205 if (defined $project) {
Martin Koegler00f429a2007-06-03 17:42:44 +02002206 $title .= " - " . to_utf8($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002207 if (defined $action) {
2208 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002209 if (defined $file_name) {
Jakub Narebski403d0902006-11-08 11:48:56 +01002210 $title .= " - " . esc_path($file_name);
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00002211 if ($action eq "tree" && $file_name !~ m|/$|) {
2212 $title .= "/";
2213 }
2214 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02002215 }
2216 }
Alp Tokerf6801d62006-07-11 11:19:34 +01002217 my $content_type;
2218 # require explicit support from the UA if we are to send the page as
2219 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2220 # we have to do this because MSIE sometimes globs '*/*', pretending to
2221 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02002222 if (defined $cgi->http('HTTP_ACCEPT') &&
2223 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2224 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01002225 $content_type = 'application/xhtml+xml';
2226 } else {
2227 $content_type = 'text/html';
2228 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002229 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2230 -status=> $status, -expires => $expires);
Jakub Narebski45c9a752006-12-27 23:59:51 +01002231 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
Kay Sieversa59d4af2005-08-07 20:15:44 +02002232 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02002233<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02002234<!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 +02002235<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02002236<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02002237<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02002238<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01002239<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebski45c9a752006-12-27 23:59:51 +01002240<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02002241<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02002242<title>$title</title>
Kay Sievers161332a2005-08-07 19:49:46 +02002243EOF
Alan Chandlerb2d34762006-10-03 13:49:03 +01002244# print out each stylesheet that exist
2245 if (defined $stylesheet) {
2246#provides backwards capability for those people who define style sheet in a config file
2247 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2248 } else {
2249 foreach my $stylesheet (@stylesheets) {
2250 next unless $stylesheet;
2251 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2252 }
2253 }
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002254 if (defined $project) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002255 printf('<link rel="alternate" title="%s log RSS feed" '.
2256 'href="%s" type="application/rss+xml" />'."\n",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002257 esc_param($project), href(action=>"rss"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002258 printf('<link rel="alternate" title="%s log Atom feed" '.
2259 'href="%s" type="application/atom+xml" />'."\n",
2260 esc_param($project), href(action=>"atom"));
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002261 } else {
2262 printf('<link rel="alternate" title="%s projects list" '.
2263 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2264 $site_name, href(project=>undef, action=>"project_index"));
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002265 printf('<link rel="alternate" title="%s projects feeds" '.
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002266 'href="%s" type="text/x-opml"/>'."\n",
2267 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002268 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02002269 if (defined $favicon) {
2270 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2271 }
Jakub Narebski10161352006-08-05 13:18:58 +02002272
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02002273 print "</head>\n" .
Alan Chandlerb2d34762006-10-03 13:49:03 +01002274 "<body>\n";
2275
2276 if (-f $site_header) {
2277 open (my $fd, $site_header);
2278 print <$fd>;
2279 close $fd;
2280 }
2281
2282 print "<div class=\"page_header\">\n" .
Jakub Narebski9a7a62f2006-10-06 12:31:05 +02002283 $cgi->a({-href => esc_url($logo_url),
2284 -title => $logo_label},
2285 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
Jakub Narebskif93bff82006-09-26 01:58:41 +02002286 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002287 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002288 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02002289 if (defined $action) {
2290 print " / $action";
2291 }
Kay Sievers19806692005-08-07 20:26:27 +02002292 print "\n";
Robert Fitzsimons6be93512006-12-23 03:35:16 +00002293 }
Petr Baudisd77b5672007-05-17 04:24:19 +02002294 print "</div>\n";
2295
Robert Fitzsimons6be93512006-12-23 03:35:16 +00002296 my ($have_search) = gitweb_check_feature('search');
2297 if ((defined $project) && ($have_search)) {
Kay Sievers19806692005-08-07 20:26:27 +02002298 if (!defined $searchtext) {
2299 $searchtext = "";
2300 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02002301 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03002302 if (defined $hash_base) {
2303 $search_hash = $hash_base;
2304 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02002305 $search_hash = $hash;
2306 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002307 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02002308 }
Matt McCutchen40375a82007-06-28 14:57:07 -04002309 my $action = $my_uri;
2310 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2311 if ($use_pathinfo) {
2312 $action .= "/$project";
2313 } else {
2314 $cgi->param("p", $project);
2315 }
Kay Sievers19806692005-08-07 20:26:27 +02002316 $cgi->param("a", "search");
Kay Sieversc39e47d2005-09-17 03:00:21 +02002317 $cgi->param("h", $search_hash);
Matt McCutchen40375a82007-06-28 14:57:07 -04002318 print $cgi->startform(-method => "get", -action => $action) .
Kay Sieversc994d622005-08-07 20:27:18 +02002319 "<div class=\"search\">\n" .
Matt McCutchen40375a82007-06-28 14:57:07 -04002320 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
Kay Sieversc994d622005-08-07 20:27:18 +02002321 $cgi->hidden(-name => "a") . "\n" .
Kay Sieversc39e47d2005-09-17 03:00:21 +02002322 $cgi->hidden(-name => "h") . "\n" .
Petr Baudis88ad7292006-10-24 05:15:46 +02002323 $cgi->popup_menu(-name => 'st', -default => 'commit',
Petr Baudise7738552007-05-17 04:31:12 +02002324 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
Petr Baudis88ad7292006-10-24 05:15:46 +02002325 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2326 " search:\n",
Kay Sieversc994d622005-08-07 20:27:18 +02002327 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2328 "</div>" .
2329 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02002330 }
Kay Sievers161332a2005-08-07 19:49:46 +02002331}
2332
Kay Sievers12a88f22005-08-07 20:02:47 +02002333sub git_footer_html {
Kay Sievers6191f8e2005-08-07 20:19:56 +02002334 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02002335 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002336 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002337 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01002338 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02002339 }
Jakub Narebskia1565c42006-09-15 19:30:34 +02002340 print $cgi->a({-href => href(action=>"rss"),
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01002341 -class => "rss_logo"}, "RSS") . " ";
2342 print $cgi->a({-href => href(action=>"atom"),
2343 -class => "rss_logo"}, "Atom") . "\n";
Kay Sieversc994d622005-08-07 20:27:18 +02002344 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02002345 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski9d0734a2006-09-15 11:11:33 +02002346 -class => "rss_logo"}, "OPML") . " ";
2347 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2348 -class => "rss_logo"}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02002349 }
Alan Chandlerb2d34762006-10-03 13:49:03 +01002350 print "</div>\n" ;
2351
2352 if (-f $site_footer) {
2353 open (my $fd, $site_footer);
2354 print <$fd>;
2355 close $fd;
2356 }
2357
2358 print "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02002359 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02002360}
2361
Kay Sievers061cc7c2005-08-07 20:15:57 +02002362sub die_error {
2363 my $status = shift || "403 Forbidden";
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02002364 my $error = shift || "Malformed query, file missing or permission denied";
Kay Sievers664f4cc2005-08-07 20:17:19 +02002365
Kay Sieversa59d4af2005-08-07 20:15:44 +02002366 git_header_html($status);
Jakub Narebski59b9f612006-08-22 23:42:53 +02002367 print <<EOF;
2368<div class="page_body">
2369<br /><br />
2370$status - $error
2371<br />
2372</div>
2373EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02002374 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002375 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02002376}
2377
Jakub Narebski717b8312006-07-31 21:22:15 +02002378## ----------------------------------------------------------------------
2379## functions printing or outputting HTML: navigation
2380
Jakub Narebski847e01f2006-08-14 02:05:47 +02002381sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002382 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2383 $extra = '' if !defined $extra; # pager or formats
2384
2385 my @navs = qw(summary shortlog log commit commitdiff tree);
2386 if ($suppress) {
2387 @navs = grep { $_ ne $suppress } @navs;
2388 }
2389
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002390 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002391 if (defined $head) {
2392 for (qw(commit commitdiff)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02002393 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002394 }
2395 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2396 for (qw(shortlog log)) {
Jakub Narebski3be8e722007-04-01 22:22:21 +02002397 $arg{$_}{'hash'} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002398 }
2399 }
2400 }
Jakub Narebski3be8e722007-04-01 22:22:21 +02002401 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2402 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002403
2404 print "<div class=\"page_nav\">\n" .
2405 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002406 map { $_ eq $current ?
2407 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2408 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02002409 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02002410 "</div>\n";
2411}
2412
Jakub Narebski847e01f2006-08-14 02:05:47 +02002413sub format_paging_nav {
Jakub Narebski6855f422006-07-30 20:31:00 +02002414 my ($action, $hash, $head, $page, $nrevs) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02002415 my $paging_nav;
2416
2417
2418 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002419 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002420 } else {
2421 $paging_nav .= "HEAD";
2422 }
2423
2424 if ($page > 0) {
2425 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002426 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02002427 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002428 } else {
2429 $paging_nav .= " &sdot; prev";
2430 }
2431
2432 if ($nrevs >= (100 * ($page+1)-1)) {
2433 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002434 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02002435 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02002436 } else {
2437 $paging_nav .= " &sdot; next";
2438 }
2439
2440 return $paging_nav;
2441}
2442
Jakub Narebski717b8312006-07-31 21:22:15 +02002443## ......................................................................
2444## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02002445
Jakub Narebski847e01f2006-08-14 02:05:47 +02002446sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02002447 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002448 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02002449
Jakub Narebski3be8e722007-04-01 22:22:21 +02002450 $args{'action'} = $action;
2451 $args{'hash'} = $hash if $hash;
2452 $args{'hash_base'} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02002453
2454 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002455 $cgi->a({-href => href(%args), -class => "title"},
2456 $title ? $title : $action) .
2457 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02002458}
2459
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002460#sub git_print_authorship (\%) {
2461sub git_print_authorship {
2462 my $co = shift;
2463
Jakub Narebskia44465c2006-08-28 23:17:31 +02002464 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002465 print "<div class=\"author_date\">" .
2466 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02002467 " [$ad{'rfc2822'}";
2468 if ($ad{'hour_local'} < 6) {
2469 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2470 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2471 } else {
2472 printf(" (%02d:%02d %s)",
2473 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2474 }
2475 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02002476}
2477
Jakub Narebski717b8312006-07-31 21:22:15 +02002478sub git_print_page_path {
2479 my $name = shift;
2480 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002481 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002482
Jakub Narebski4df118e2006-10-21 17:53:55 +02002483
2484 print "<div class=\"page_path\">";
2485 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
Martin Koegler00f429a2007-06-03 17:42:44 +02002486 -title => 'tree root'}, to_utf8("[$project]"));
Jakub Narebski4df118e2006-10-21 17:53:55 +02002487 print " / ";
2488 if (defined $name) {
Jakub Narebski762c7202006-09-04 18:17:58 +02002489 my @dirname = split '/', $name;
2490 my $basename = pop @dirname;
2491 my $fullname = '';
2492
Jakub Narebski762c7202006-09-04 18:17:58 +02002493 foreach my $dir (@dirname) {
Petr Baudis16fdb482006-09-21 02:05:50 +02002494 $fullname .= ($fullname ? '/' : '') . $dir;
Jakub Narebski762c7202006-09-04 18:17:58 +02002495 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2496 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002497 -title => $fullname}, esc_path($dir));
Petr Baudis26d0a972006-09-23 01:00:12 +02002498 print " / ";
Jakub Narebski762c7202006-09-04 18:17:58 +02002499 }
2500 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002501 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02002502 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002503 -title => $name}, esc_path($basename));
Jakub Narebski762c7202006-09-04 18:17:58 +02002504 } elsif (defined $type && $type eq 'tree') {
2505 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2506 hash_base=>$hb),
Jakub Narebskiedc04e92007-03-07 02:21:25 +01002507 -title => $name}, esc_path($basename));
Jakub Narebski4df118e2006-10-21 17:53:55 +02002508 print " / ";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002509 } else {
Jakub Narebski403d0902006-11-08 11:48:56 +01002510 print esc_path($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002511 }
Kay Sievers8ed23e12005-08-07 20:05:44 +02002512 }
Jakub Narebski4df118e2006-10-21 17:53:55 +02002513 print "<br/></div>\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02002514}
2515
Jakub Narebskib7f92532006-08-28 14:48:10 +02002516# sub git_print_log (\@;%) {
2517sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02002518 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02002519 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02002520
Jakub Narebskib7f92532006-08-28 14:48:10 +02002521 if ($opts{'-remove_title'}) {
2522 # remove title, i.e. first line of log
2523 shift @$log;
2524 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02002525 # remove leading empty lines
2526 while (defined $log->[0] && $log->[0] eq "") {
2527 shift @$log;
2528 }
2529
2530 # print log
2531 my $signoff = 0;
2532 my $empty = 0;
2533 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02002534 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2535 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02002536 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02002537 if (! $opts{'-remove_signoff'}) {
2538 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2539 next;
2540 } else {
2541 # remove signoff lines
2542 next;
2543 }
2544 } else {
2545 $signoff = 0;
2546 }
2547
Jakub Narebskid16d0932006-08-17 11:21:23 +02002548 # print only one empty line
2549 # do not print empty line after signoff
2550 if ($line eq "") {
2551 next if ($empty || $signoff);
2552 $empty = 1;
2553 } else {
2554 $empty = 0;
2555 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02002556
2557 print format_log_line_html($line) . "<br/>\n";
2558 }
2559
2560 if ($opts{'-final_empty_line'}) {
2561 # end with single empty line
2562 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02002563 }
2564}
2565
Jakub Narebskie33fba42006-12-10 13:25:46 +01002566# return link target (what link points to)
2567sub git_get_link_target {
2568 my $hash = shift;
2569 my $link_target;
2570
2571 # read link
2572 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2573 or return;
2574 {
2575 local $/;
2576 $link_target = <$fd>;
2577 }
2578 close $fd
2579 or return;
2580
2581 return $link_target;
2582}
2583
Jakub Narebski3bf9d572006-12-10 13:25:48 +01002584# given link target, and the directory (basedir) the link is in,
2585# return target of link relative to top directory (top tree);
2586# return undef if it is not possible (including absolute links).
2587sub normalize_link_target {
2588 my ($link_target, $basedir, $hash_base) = @_;
2589
2590 # we can normalize symlink target only if $hash_base is provided
2591 return unless $hash_base;
2592
2593 # absolute symlinks (beginning with '/') cannot be normalized
2594 return if (substr($link_target, 0, 1) eq '/');
2595
2596 # normalize link target to path from top (root) tree (dir)
2597 my $path;
2598 if ($basedir) {
2599 $path = $basedir . '/' . $link_target;
2600 } else {
2601 # we are in top (root) tree (dir)
2602 $path = $link_target;
2603 }
2604
2605 # remove //, /./, and /../
2606 my @path_parts;
2607 foreach my $part (split('/', $path)) {
2608 # discard '.' and ''
2609 next if (!$part || $part eq '.');
2610 # handle '..'
2611 if ($part eq '..') {
2612 if (@path_parts) {
2613 pop @path_parts;
2614 } else {
2615 # link leads outside repository (outside top dir)
2616 return;
2617 }
2618 } else {
2619 push @path_parts, $part;
2620 }
2621 }
2622 $path = join('/', @path_parts);
2623
2624 return $path;
2625}
Jakub Narebskie33fba42006-12-10 13:25:46 +01002626
Jakub Narebskifa702002006-08-31 00:35:07 +02002627# print tree entry (row of git_tree), but without encompassing <tr> element
2628sub git_print_tree_entry {
2629 my ($t, $basedir, $hash_base, $have_blame) = @_;
2630
2631 my %base_key = ();
Jakub Narebskie33fba42006-12-10 13:25:46 +01002632 $base_key{'hash_base'} = $hash_base if defined $hash_base;
Jakub Narebskifa702002006-08-31 00:35:07 +02002633
Luben Tuikov4de741b2006-09-25 22:38:16 -07002634 # The format of a table row is: mode list link. Where mode is
2635 # the mode of the entry, list is the name of the entry, an href,
2636 # and link is the action links of the entry.
2637
Jakub Narebskifa702002006-08-31 00:35:07 +02002638 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2639 if ($t->{'type'} eq "blob") {
2640 print "<td class=\"list\">" .
Luben Tuikov4de741b2006-09-25 22:38:16 -07002641 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002642 file_name=>"$basedir$t->{'name'}", %base_key),
Jakub Narebskie33fba42006-12-10 13:25:46 +01002643 -class => "list"}, esc_path($t->{'name'}));
2644 if (S_ISLNK(oct $t->{'mode'})) {
2645 my $link_target = git_get_link_target($t->{'hash'});
2646 if ($link_target) {
Jakub Narebski3bf9d572006-12-10 13:25:48 +01002647 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2648 if (defined $norm_target) {
2649 print " -> " .
2650 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2651 file_name=>$norm_target),
2652 -title => $norm_target}, esc_path($link_target));
2653 } else {
2654 print " -> " . esc_path($link_target);
2655 }
Jakub Narebskie33fba42006-12-10 13:25:46 +01002656 }
2657 }
2658 print "</td>\n";
Luben Tuikov4de741b2006-09-25 22:38:16 -07002659 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02002660 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002661 file_name=>"$basedir$t->{'name'}", %base_key)},
2662 "blob");
Jakub Narebskifa702002006-08-31 00:35:07 +02002663 if ($have_blame) {
Petr Baudis4777b012006-10-24 05:36:10 +02002664 print " | " .
2665 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002666 file_name=>"$basedir$t->{'name'}", %base_key)},
2667 "blame");
Jakub Narebskifa702002006-08-31 00:35:07 +02002668 }
2669 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02002670 print " | " .
2671 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02002672 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2673 "history");
2674 }
2675 print " | " .
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07002676 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002677 file_name=>"$basedir$t->{'name'}")},
2678 "raw");
Luben Tuikov4de741b2006-09-25 22:38:16 -07002679 print "</td>\n";
Jakub Narebskifa702002006-08-31 00:35:07 +02002680
2681 } elsif ($t->{'type'} eq "tree") {
Luben Tuikov0fa105e2006-09-26 12:45:37 -07002682 print "<td class=\"list\">";
2683 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskifa702002006-08-31 00:35:07 +02002684 file_name=>"$basedir$t->{'name'}", %base_key)},
Jakub Narebski403d0902006-11-08 11:48:56 +01002685 esc_path($t->{'name'}));
Luben Tuikov0fa105e2006-09-26 12:45:37 -07002686 print "</td>\n";
2687 print "<td class=\"link\">";
Petr Baudis4777b012006-10-24 05:36:10 +02002688 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
Jakub Narebskie33fba42006-12-10 13:25:46 +01002689 file_name=>"$basedir$t->{'name'}", %base_key)},
2690 "tree");
Jakub Narebskifa702002006-08-31 00:35:07 +02002691 if (defined $hash_base) {
Petr Baudis4777b012006-10-24 05:36:10 +02002692 print " | " .
2693 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
Jakub Narebskifa702002006-08-31 00:35:07 +02002694 file_name=>"$basedir$t->{'name'}")},
2695 "history");
2696 }
2697 print "</td>\n";
2698 }
2699}
2700
Jakub Narebski717b8312006-07-31 21:22:15 +02002701## ......................................................................
2702## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02002703
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002704sub fill_from_file_info {
2705 my ($diff, @parents) = @_;
2706
2707 $diff->{'from_file'} = [ ];
2708 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2709 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2710 if ($diff->{'status'}[$i] eq 'R' ||
2711 $diff->{'status'}[$i] eq 'C') {
2712 $diff->{'from_file'}[$i] =
2713 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2714 }
2715 }
2716
2717 return $diff;
2718}
2719
2720# parameters can be strings, or references to arrays of strings
2721sub from_ids_eq {
2722 my ($a, $b) = @_;
2723
2724 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2725 for (my $i = 0; $i < @$a; ++$i) {
2726 return 0 unless ($a->[$i] eq $b->[$i]);
2727 }
2728 return 1;
2729 } elsif (!ref($a) && !ref($b)) {
2730 return $a eq $b;
2731 } else {
2732 return 0;
2733 }
2734}
2735
Jakub Narebski90921742007-06-08 13:27:42 +02002736sub is_deleted {
2737 my $diffinfo = shift;
2738
2739 return $diffinfo->{'to_id'} eq ('0' x 40);
2740}
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002741
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002742sub git_difftree_body {
Jakub Narebskied224de2007-05-07 01:10:04 +02002743 my ($difftree, $hash, @parents) = @_;
2744 my ($parent) = $parents[0];
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002745 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002746 print "<div class=\"list_head\">\n";
2747 if ($#{$difftree} > 10) {
2748 print(($#{$difftree} + 1) . " files changed:\n");
2749 }
2750 print "</div>\n";
2751
Jakub Narebskied224de2007-05-07 01:10:04 +02002752 print "<table class=\"" .
2753 (@parents > 1 ? "combined " : "") .
2754 "diff_tree\">\n";
Jakub Narebski47598d72007-06-08 13:24:56 +02002755
2756 # header only for combined diff in 'commitdiff' view
2757 my $has_header = @parents > 1 && $action eq 'commitdiff';
2758 if ($has_header) {
2759 # table header
2760 print "<thead><tr>\n" .
2761 "<th></th><th></th>\n"; # filename, patchN link
2762 for (my $i = 0; $i < @parents; $i++) {
2763 my $par = $parents[$i];
2764 print "<th>" .
2765 $cgi->a({-href => href(action=>"commitdiff",
2766 hash=>$hash, hash_parent=>$par),
2767 -title => 'commitdiff to parent number ' .
2768 ($i+1) . ': ' . substr($par,0,7)},
2769 $i+1) .
2770 "&nbsp;</th>\n";
2771 }
2772 print "</tr></thead>\n<tbody>\n";
2773 }
2774
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07002775 my $alternate = 1;
Jakub Narebskib4657e72006-08-28 14:48:14 +02002776 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002777 foreach my $line (@{$difftree}) {
Jakub Narebski493e01d2007-05-07 01:10:06 +02002778 my $diff;
2779 if (ref($line) eq "HASH") {
2780 # pre-parsed (or generated by hand)
2781 $diff = $line;
2782 } else {
2783 $diff = parse_difftree_raw_line($line);
2784 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002785
2786 if ($alternate) {
2787 print "<tr class=\"dark\">\n";
2788 } else {
2789 print "<tr class=\"light\">\n";
2790 }
2791 $alternate ^= 1;
2792
Jakub Narebski493e01d2007-05-07 01:10:06 +02002793 if (exists $diff->{'nparents'}) { # combined diff
Jakub Narebskied224de2007-05-07 01:10:04 +02002794
Jakub Narebski493e01d2007-05-07 01:10:06 +02002795 fill_from_file_info($diff, @parents)
2796 unless exists $diff->{'from_file'};
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02002797
Jakub Narebski90921742007-06-08 13:27:42 +02002798 if (!is_deleted($diff)) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002799 # file exists in the result (child) commit
2800 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02002801 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2802 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002803 hash_base=>$hash),
Jakub Narebski493e01d2007-05-07 01:10:06 +02002804 -class => "list"}, esc_path($diff->{'to_file'})) .
Jakub Narebskied224de2007-05-07 01:10:04 +02002805 "</td>\n";
2806 } else {
2807 print "<td>" .
Jakub Narebski493e01d2007-05-07 01:10:06 +02002808 esc_path($diff->{'to_file'}) .
Jakub Narebskied224de2007-05-07 01:10:04 +02002809 "</td>\n";
2810 }
2811
2812 if ($action eq 'commitdiff') {
2813 # link to patch
2814 $patchno++;
2815 print "<td class=\"link\">" .
2816 $cgi->a({-href => "#patch$patchno"}, "patch") .
2817 " | " .
2818 "</td>\n";
2819 }
2820
2821 my $has_history = 0;
2822 my $not_deleted = 0;
Jakub Narebski493e01d2007-05-07 01:10:06 +02002823 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002824 my $hash_parent = $parents[$i];
Jakub Narebski493e01d2007-05-07 01:10:06 +02002825 my $from_hash = $diff->{'from_id'}[$i];
2826 my $from_path = $diff->{'from_file'}[$i];
2827 my $status = $diff->{'status'}[$i];
Jakub Narebskied224de2007-05-07 01:10:04 +02002828
2829 $has_history ||= ($status ne 'A');
2830 $not_deleted ||= ($status ne 'D');
2831
Jakub Narebskied224de2007-05-07 01:10:04 +02002832 if ($status eq 'A') {
2833 print "<td class=\"link\" align=\"right\"> | </td>\n";
2834 } elsif ($status eq 'D') {
2835 print "<td class=\"link\">" .
2836 $cgi->a({-href => href(action=>"blob",
2837 hash_base=>$hash,
2838 hash=>$from_hash,
2839 file_name=>$from_path)},
2840 "blob" . ($i+1)) .
2841 " | </td>\n";
2842 } else {
Jakub Narebski493e01d2007-05-07 01:10:06 +02002843 if ($diff->{'to_id'} eq $from_hash) {
Jakub Narebskied224de2007-05-07 01:10:04 +02002844 print "<td class=\"link nochange\">";
2845 } else {
2846 print "<td class=\"link\">";
2847 }
2848 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002849 hash=>$diff->{'to_id'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002850 hash_parent=>$from_hash,
2851 hash_base=>$hash,
2852 hash_parent_base=>$hash_parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002853 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002854 file_parent=>$from_path)},
2855 "diff" . ($i+1)) .
2856 " | </td>\n";
2857 }
2858 }
2859
2860 print "<td class=\"link\">";
2861 if ($not_deleted) {
2862 print $cgi->a({-href => href(action=>"blob",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002863 hash=>$diff->{'to_id'},
2864 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002865 hash_base=>$hash)},
2866 "blob");
2867 print " | " if ($has_history);
2868 }
2869 if ($has_history) {
2870 print $cgi->a({-href => href(action=>"history",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002871 file_name=>$diff->{'to_file'},
Jakub Narebskied224de2007-05-07 01:10:04 +02002872 hash_base=>$hash)},
2873 "history");
2874 }
2875 print "</td>\n";
2876
2877 print "</tr>\n";
2878 next; # instead of 'else' clause, to avoid extra indent
2879 }
2880 # else ordinary diff
2881
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002882 my ($to_mode_oct, $to_mode_str, $to_file_type);
2883 my ($from_mode_oct, $from_mode_str, $from_file_type);
Jakub Narebski493e01d2007-05-07 01:10:06 +02002884 if ($diff->{'to_mode'} ne ('0' x 6)) {
2885 $to_mode_oct = oct $diff->{'to_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002886 if (S_ISREG($to_mode_oct)) { # only for regular file
2887 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002888 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002889 $to_file_type = file_type($diff->{'to_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002890 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002891 if ($diff->{'from_mode'} ne ('0' x 6)) {
2892 $from_mode_oct = oct $diff->{'from_mode'};
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002893 if (S_ISREG($to_mode_oct)) { # only for regular file
2894 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2895 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002896 $from_file_type = file_type($diff->{'from_mode'});
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002897 }
2898
Jakub Narebski493e01d2007-05-07 01:10:06 +02002899 if ($diff->{'status'} eq "A") { # created
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002900 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2901 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2902 $mode_chng .= "]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07002903 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02002904 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2905 hash_base=>$hash, file_name=>$diff->{'file'}),
2906 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07002907 print "</td>\n";
2908 print "<td>$mode_chng</td>\n";
2909 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02002910 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02002911 # link to patch
2912 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07002913 print $cgi->a({-href => "#patch$patchno"}, "patch");
Jakub Narebski897d1d22006-11-19 22:51:39 +01002914 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02002915 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002916 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2917 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski3faa5412007-01-08 02:10:42 +01002918 "blob");
Jakub Narebskib4657e72006-08-28 14:48:14 +02002919 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002920
Jakub Narebski493e01d2007-05-07 01:10:06 +02002921 } elsif ($diff->{'status'} eq "D") { # deleted
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002922 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Luben Tuikov499faed2006-09-27 17:23:25 -07002923 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02002924 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2925 hash_base=>$parent, file_name=>$diff->{'file'}),
2926 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07002927 print "</td>\n";
2928 print "<td>$mode_chng</td>\n";
2929 print "<td class=\"link\">";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02002930 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02002931 # link to patch
2932 $patchno++;
Luben Tuikov499faed2006-09-27 17:23:25 -07002933 print $cgi->a({-href => "#patch$patchno"}, "patch");
2934 print " | ";
Jakub Narebskib4657e72006-08-28 14:48:14 +02002935 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002936 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2937 hash_base=>$parent, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01002938 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002939 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01002940 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002941 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01002942 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002943 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02002944 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002945 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002946 "history");
Luben Tuikov499faed2006-09-27 17:23:25 -07002947 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002948
Jakub Narebski493e01d2007-05-07 01:10:06 +02002949 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002950 my $mode_chnge = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02002951 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002952 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
Jakub Narebski6e72cf42007-01-03 20:47:24 +01002953 if ($from_file_type ne $to_file_type) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002954 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002955 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02002956 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2957 if ($from_mode_str && $to_mode_str) {
2958 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2959 } elsif ($to_mode_str) {
2960 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002961 }
2962 }
2963 $mode_chnge .= "]</span>\n";
2964 }
2965 print "<td>";
Jakub Narebski493e01d2007-05-07 01:10:06 +02002966 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2967 hash_base=>$hash, file_name=>$diff->{'file'}),
2968 -class => "list"}, esc_path($diff->{'file'}));
Luben Tuikov499faed2006-09-27 17:23:25 -07002969 print "</td>\n";
2970 print "<td>$mode_chnge</td>\n";
2971 print "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01002972 if ($action eq 'commitdiff') {
2973 # link to patch
2974 $patchno++;
2975 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2976 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02002977 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01002978 # "commit" view and modified file (not onlu mode changed)
2979 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02002980 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01002981 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002982 file_name=>$diff->{'file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01002983 "diff") .
2984 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002985 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02002986 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2987 hash_base=>$hash, file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01002988 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002989 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01002990 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002991 file_name=>$diff->{'file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01002992 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08002993 }
Luben Tuikoveb51ec92006-09-27 17:24:49 -07002994 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02002995 file_name=>$diff->{'file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02002996 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002997 print "</td>\n";
2998
Jakub Narebski493e01d2007-05-07 01:10:06 +02002999 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003000 my %status_name = ('R' => 'moved', 'C' => 'copied');
Jakub Narebski493e01d2007-05-07 01:10:06 +02003001 my $nstatus = $status_name{$diff->{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003002 my $mode_chng = "";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003003 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003004 # mode also for directories, so we cannot use $to_mode_str
3005 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003006 }
3007 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003008 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003009 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3010 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02003011 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3012 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003013 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3014 -class => "list"}, esc_path($diff->{'from_file'})) .
3015 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Luben Tuikov499faed2006-09-27 17:23:25 -07003016 "<td class=\"link\">";
Jakub Narebski241cc592006-10-31 17:36:27 +01003017 if ($action eq 'commitdiff') {
3018 # link to patch
3019 $patchno++;
3020 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3021 " | ";
Jakub Narebski493e01d2007-05-07 01:10:06 +02003022 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
Jakub Narebski241cc592006-10-31 17:36:27 +01003023 # "commit" view and modified file (not only pure rename or copy)
3024 print $cgi->a({-href => href(action=>"blobdiff",
Jakub Narebski493e01d2007-05-07 01:10:06 +02003025 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
Jakub Narebski241cc592006-10-31 17:36:27 +01003026 hash_base=>$hash, hash_parent_base=>$parent,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003027 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
Jakub Narebski241cc592006-10-31 17:36:27 +01003028 "diff") .
3029 " | ";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003030 }
Jakub Narebski493e01d2007-05-07 01:10:06 +02003031 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3032 hash_base=>$parent, file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003033 "blob") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003034 if ($have_blame) {
Jakub Narebski897d1d22006-11-19 22:51:39 +01003035 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003036 file_name=>$diff->{'to_file'})},
Jakub Narebski897d1d22006-11-19 22:51:39 +01003037 "blame") . " | ";
Junio C Hamano2b2a8c72006-11-08 12:22:04 -08003038 }
Jakub Narebski897d1d22006-11-19 22:51:39 +01003039 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
Jakub Narebski493e01d2007-05-07 01:10:06 +02003040 file_name=>$diff->{'to_file'})},
Jakub Narebskie7fb0222006-10-21 17:52:19 +02003041 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003042 print "</td>\n";
3043
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003044 } # we should not encounter Unmerged (U) or Unknown (X) status
3045 print "</tr>\n";
3046 }
Jakub Narebski47598d72007-06-08 13:24:56 +02003047 print "</tbody>" if $has_header;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02003048 print "</table>\n";
3049}
3050
Jakub Narebskieee08902006-08-24 00:15:14 +02003051sub git_patchset_body {
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003052 my ($fd, $difftree, $hash, @hash_parents) = @_;
3053 my ($hash_parent) = $hash_parents[0];
Jakub Narebskieee08902006-08-24 00:15:14 +02003054
3055 my $patch_idx = 0;
Martin Koegler4280cde2007-04-22 22:49:25 -07003056 my $patch_number = 0;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003057 my $patch_line;
Jakub Narebskife875852006-08-25 20:59:39 +02003058 my $diffinfo;
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003059 my (%from, %to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003060
3061 print "<div class=\"patchset\">\n";
3062
Jakub Narebski6d55f052006-11-18 23:35:39 +01003063 # skip to first patch
3064 while ($patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02003065 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02003066
Jakub Narebski6d55f052006-11-18 23:35:39 +01003067 last if ($patch_line =~ m/^diff /);
3068 }
3069
3070 PATCH:
3071 while ($patch_line) {
3072 my @diff_header;
Jakub Narebski62e4f262007-01-07 02:52:23 +01003073 my ($from_id, $to_id);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003074
3075 # git diff header
3076 #assert($patch_line =~ m/^diff /) if DEBUG;
3077 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
Martin Koegler4280cde2007-04-22 22:49:25 -07003078 $patch_number++;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003079 push @diff_header, $patch_line;
3080
3081 # extended diff header
3082 EXTENDED_HEADER:
3083 while ($patch_line = <$fd>) {
3084 chomp $patch_line;
3085
Jakub Narebski66399ef2007-01-07 02:52:25 +01003086 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003087
3088 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3089 $from_id = $1;
3090 $to_id = $2;
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003091 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3092 $from_id = [ split(',', $1) ];
3093 $to_id = $2;
Jakub Narebskieee08902006-08-24 00:15:14 +02003094 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003095
3096 push @diff_header, $patch_line;
3097 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003098 my $last_patch_line = $patch_line;
3099
3100 # check if current patch belong to current raw line
3101 # and parse raw git-diff line if needed
3102 if (defined $diffinfo &&
Jakub Narebski22065372007-05-12 12:42:32 +02003103 defined $from_id && defined $to_id &&
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003104 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
3105 $diffinfo->{'to_id'} eq $to_id) {
Jakub Narebski22065372007-05-12 12:42:32 +02003106 # this is continuation of a split patch
Jakub Narebski6d55f052006-11-18 23:35:39 +01003107 print "<div class=\"patch cont\">\n";
3108 } else {
3109 # advance raw git-diff output if needed
3110 $patch_idx++ if defined $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02003111
Jakub Narebskicd030c32007-06-08 13:33:28 +02003112 # compact combined diff output can have some patches skipped
3113 # find which patch (using pathname of result) we are at now
3114 my $to_name;
3115 if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
3116 $to_name = $1;
Jakub Narebskife875852006-08-25 20:59:39 +02003117 }
Jakub Narebskicd030c32007-06-08 13:33:28 +02003118
3119 do {
3120 # read and prepare patch information
3121 if (ref($difftree->[$patch_idx]) eq "HASH") {
3122 # pre-parsed (or generated by hand)
3123 $diffinfo = $difftree->[$patch_idx];
3124 } else {
3125 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3126 }
3127
3128 # check if current raw line has no patch (it got simplified)
3129 if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3130 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3131 format_diff_cc_simplified($diffinfo, @hash_parents) .
3132 "</div>\n"; # class="patch"
3133
3134 $patch_idx++;
3135 $patch_number++;
3136 }
3137 } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3138 $patch_idx > $#$difftree);
Jakub Narebski90921742007-06-08 13:27:42 +02003139 # modifies %from, %to hashes
3140 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003141 if ($diffinfo->{'nparents'}) {
3142 # combined diff
3143 $from{'file'} = [];
3144 $from{'href'} = [];
3145 fill_from_file_info($diffinfo, @hash_parents)
3146 unless exists $diffinfo->{'from_file'};
3147 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3148 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
3149 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3150 $from{'href'}[$i] = href(action=>"blob",
3151 hash_base=>$hash_parents[$i],
3152 hash=>$diffinfo->{'from_id'}[$i],
3153 file_name=>$from{'file'}[$i]);
3154 } else {
3155 $from{'href'}[$i] = undef;
3156 }
3157 }
Jakub Narebski62e4f262007-01-07 02:52:23 +01003158 } else {
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003159 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
3160 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3161 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3162 hash=>$diffinfo->{'from_id'},
3163 file_name=>$from{'file'});
3164 } else {
3165 delete $from{'href'};
3166 }
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003167 }
Jakub Narebski5f855052007-05-17 22:54:28 +02003168
Jakub Narebskie72c0ea2007-05-07 01:10:05 +02003169 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
Jakub Narebski90921742007-06-08 13:27:42 +02003170 if (!is_deleted($diffinfo)) { # file exists in result
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003171 $to{'href'} = href(action=>"blob", hash_base=>$hash,
3172 hash=>$diffinfo->{'to_id'},
3173 file_name=>$to{'file'});
Jakub Narebski62e4f262007-01-07 02:52:23 +01003174 } else {
3175 delete $to{'href'};
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003176 }
Jakub Narebski6d55f052006-11-18 23:35:39 +01003177 # this is first patch for raw difftree line with $patch_idx index
3178 # we index @$difftree array from 0, but number patches from 1
3179 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebski744d0ac2006-11-08 17:59:41 +01003180 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003181
Jakub Narebski6d55f052006-11-18 23:35:39 +01003182 # print "git diff" header
3183 $patch_line = shift @diff_header;
Jakub Narebski90921742007-06-08 13:27:42 +02003184 print format_git_diff_header_line($patch_line, $diffinfo,
3185 \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003186
Jakub Narebski6d55f052006-11-18 23:35:39 +01003187 # print extended diff header
3188 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3189 EXTENDED_HEADER:
3190 foreach $patch_line (@diff_header) {
Jakub Narebski90921742007-06-08 13:27:42 +02003191 print format_extended_diff_header_line($patch_line, $diffinfo,
3192 \%from, \%to);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003193 }
3194 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
Jakub Narebskie4e4f822006-08-25 21:04:13 +02003195
Jakub Narebski6d55f052006-11-18 23:35:39 +01003196 # from-file/to-file diff header
3197 $patch_line = $last_patch_line;
Jakub Narebski0bdb28c2007-01-10 00:07:43 +01003198 if (! $patch_line) {
3199 print "</div>\n"; # class="patch"
3200 last PATCH;
3201 }
Jakub Narebski66399ef2007-01-07 02:52:25 +01003202 next PATCH if ($patch_line =~ m/^diff /);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003203 #assert($patch_line =~ m/^---/) if DEBUG;
Jakub Narebski90921742007-06-08 13:27:42 +02003204 #assert($patch_line eq $last_patch_line) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003205
3206 $patch_line = <$fd>;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003207 chomp $patch_line;
Jakub Narebski90921742007-06-08 13:27:42 +02003208 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
Jakub Narebski6d55f052006-11-18 23:35:39 +01003209
Jakub Narebski90921742007-06-08 13:27:42 +02003210 print format_diff_from_to_header($last_patch_line, $patch_line,
Jakub Narebski91af4ce2007-06-08 13:32:44 +02003211 $diffinfo, \%from, \%to,
3212 @hash_parents);
Jakub Narebski6d55f052006-11-18 23:35:39 +01003213
3214 # the patch itself
3215 LINE:
3216 while ($patch_line = <$fd>) {
3217 chomp $patch_line;
3218
3219 next PATCH if ($patch_line =~ m/^diff /);
3220
Jakub Narebski59e3b142006-11-18 23:35:40 +01003221 print format_diff_line($patch_line, \%from, \%to);
Jakub Narebskieee08902006-08-24 00:15:14 +02003222 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003223
Jakub Narebski6d55f052006-11-18 23:35:39 +01003224 } continue {
3225 print "</div>\n"; # class="patch"
Jakub Narebskieee08902006-08-24 00:15:14 +02003226 }
Jakub Narebskid26c4262007-05-17 00:05:55 +02003227
Jakub Narebskicd030c32007-06-08 13:33:28 +02003228 # for compact combined (--cc) format, with chunk and patch simpliciaction
3229 # patchset might be empty, but there might be unprocessed raw lines
3230 for ($patch_idx++ if $patch_number > 0;
3231 $patch_idx < @$difftree;
3232 $patch_idx++) {
3233 # read and prepare patch information
3234 if (ref($difftree->[$patch_idx]) eq "HASH") {
3235 # pre-parsed (or generated by hand)
3236 $diffinfo = $difftree->[$patch_idx];
3237 } else {
3238 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3239 }
3240
3241 # generate anchor for "patch" links in difftree / whatchanged part
3242 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3243 format_diff_cc_simplified($diffinfo, @hash_parents) .
3244 "</div>\n"; # class="patch"
3245
3246 $patch_number++;
3247 }
3248
Jakub Narebskid26c4262007-05-17 00:05:55 +02003249 if ($patch_number == 0) {
3250 if (@hash_parents > 1) {
3251 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3252 } else {
3253 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3254 }
3255 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003256
3257 print "</div>\n"; # class="patchset"
3258}
3259
3260# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3261
Petr Baudise30496d2006-10-24 05:33:17 +02003262sub git_project_list_body {
3263 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3264
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08003265 my ($check_forks) = gitweb_check_feature('forks');
Petr Baudise30496d2006-10-24 05:33:17 +02003266
3267 my @projects;
3268 foreach my $pr (@$projlist) {
3269 my (@aa) = git_get_last_activity($pr->{'path'});
3270 unless (@aa) {
3271 next;
3272 }
3273 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3274 if (!defined $pr->{'descr'}) {
3275 my $descr = git_get_project_description($pr->{'path'}) || "";
Martin Koegler00f429a2007-06-03 17:42:44 +02003276 $pr->{'descr_long'} = to_utf8($descr);
Michael Hendricks55feb122007-07-04 18:36:48 -06003277 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
Petr Baudise30496d2006-10-24 05:33:17 +02003278 }
3279 if (!defined $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003280 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
Petr Baudise30496d2006-10-24 05:33:17 +02003281 }
3282 if ($check_forks) {
3283 my $pname = $pr->{'path'};
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003284 if (($pname =~ s/\.git$//) &&
3285 ($pname !~ /\/$/) &&
3286 (-d "$projectroot/$pname")) {
3287 $pr->{'forks'} = "-d $projectroot/$pname";
3288 }
3289 else {
3290 $pr->{'forks'} = 0;
3291 }
Petr Baudise30496d2006-10-24 05:33:17 +02003292 }
3293 push @projects, $pr;
3294 }
3295
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003296 $order ||= $default_projects_order;
Petr Baudise30496d2006-10-24 05:33:17 +02003297 $from = 0 unless defined $from;
3298 $to = $#projects if (!defined $to || $#projects < $to);
3299
3300 print "<table class=\"project_list\">\n";
3301 unless ($no_header) {
3302 print "<tr>\n";
3303 if ($check_forks) {
3304 print "<th></th>\n";
3305 }
3306 if ($order eq "project") {
3307 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3308 print "<th>Project</th>\n";
3309 } else {
3310 print "<th>" .
3311 $cgi->a({-href => href(project=>undef, order=>'project'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003312 -class => "header"}, "Project") .
Petr Baudise30496d2006-10-24 05:33:17 +02003313 "</th>\n";
3314 }
3315 if ($order eq "descr") {
3316 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3317 print "<th>Description</th>\n";
3318 } else {
3319 print "<th>" .
3320 $cgi->a({-href => href(project=>undef, order=>'descr'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003321 -class => "header"}, "Description") .
Petr Baudise30496d2006-10-24 05:33:17 +02003322 "</th>\n";
3323 }
3324 if ($order eq "owner") {
3325 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3326 print "<th>Owner</th>\n";
3327 } else {
3328 print "<th>" .
3329 $cgi->a({-href => href(project=>undef, order=>'owner'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003330 -class => "header"}, "Owner") .
Petr Baudise30496d2006-10-24 05:33:17 +02003331 "</th>\n";
3332 }
3333 if ($order eq "age") {
3334 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3335 print "<th>Last Change</th>\n";
3336 } else {
3337 print "<th>" .
3338 $cgi->a({-href => href(project=>undef, order=>'age'),
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003339 -class => "header"}, "Last Change") .
Petr Baudise30496d2006-10-24 05:33:17 +02003340 "</th>\n";
3341 }
3342 print "<th></th>\n" .
3343 "</tr>\n";
3344 }
3345 my $alternate = 1;
3346 for (my $i = $from; $i <= $to; $i++) {
3347 my $pr = $projects[$i];
3348 if ($alternate) {
3349 print "<tr class=\"dark\">\n";
3350 } else {
3351 print "<tr class=\"light\">\n";
3352 }
3353 $alternate ^= 1;
3354 if ($check_forks) {
3355 print "<td>";
3356 if ($pr->{'forks'}) {
Junio C Hamano83ee94c2006-11-07 22:37:17 -08003357 print "<!-- $pr->{'forks'} -->\n";
Petr Baudise30496d2006-10-24 05:33:17 +02003358 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3359 }
3360 print "</td>\n";
3361 }
3362 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3363 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebskie88ce8a2006-11-26 02:18:26 +01003364 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3365 -class => "list", -title => $pr->{'descr_long'}},
3366 esc_html($pr->{'descr'})) . "</td>\n" .
Petr Baudise30496d2006-10-24 05:33:17 +02003367 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3368 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
Jakub Narebski785cdea2007-05-13 12:39:22 +02003369 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
Petr Baudise30496d2006-10-24 05:33:17 +02003370 "<td class=\"link\">" .
3371 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
Alexandre Julliardfaa1bbf2006-11-15 21:37:50 +01003372 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
Petr Baudise30496d2006-10-24 05:33:17 +02003373 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3374 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3375 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3376 "</td>\n" .
3377 "</tr>\n";
3378 }
3379 if (defined $extra) {
3380 print "<tr>\n";
3381 if ($check_forks) {
3382 print "<td></td>\n";
3383 }
3384 print "<td colspan=\"5\">$extra</td>\n" .
3385 "</tr>\n";
3386 }
3387 print "</table>\n";
3388}
3389
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003390sub git_shortlog_body {
3391 # uses global variable $project
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003392 my ($commitlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303393
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003394 $from = 0 unless defined $from;
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003395 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003396
3397 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003398 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003399 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00003400 my %co = %{$commitlist->[$i]};
3401 my $commit = $co{'id'};
Jakub Narebski847e01f2006-08-14 02:05:47 +02003402 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003403 if ($alternate) {
3404 print "<tr class=\"dark\">\n";
3405 } else {
3406 print "<tr class=\"light\">\n";
3407 }
3408 $alternate ^= 1;
3409 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3410 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3411 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3412 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003413 print format_subject_html($co{'title'}, $co{'title_short'},
3414 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003415 print "</td>\n" .
3416 "<td class=\"link\">" .
Petr Baudis4777b012006-10-24 05:36:10 +02003417 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Petr Baudis35749ae2006-09-22 03:19:44 +02003418 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
Petr Baudis55ff35c2006-10-06 15:57:52 +02003419 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02003420 my $snapshot_links = format_snapshot_links($commit);
3421 if (defined $snapshot_links) {
3422 print " | " . $snapshot_links;
Petr Baudis55ff35c2006-10-06 15:57:52 +02003423 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05303424 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003425 "</tr>\n";
3426 }
3427 if (defined $extra) {
3428 print "<tr>\n" .
3429 "<td colspan=\"4\">$extra</td>\n" .
3430 "</tr>\n";
3431 }
3432 print "</table>\n";
3433}
3434
Jakub Narebski581860e2006-08-14 02:09:08 +02003435sub git_history_body {
3436 # Warning: assumes constant type (blob or tree) during history
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003437 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
Jakub Narebski8be68352006-09-11 00:36:04 +02003438
3439 $from = 0 unless defined $from;
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003440 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02003441
3442 print "<table class=\"history\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003443 my $alternate = 1;
Jakub Narebski8be68352006-09-11 00:36:04 +02003444 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003445 my %co = %{$commitlist->[$i]};
Jakub Narebski581860e2006-08-14 02:09:08 +02003446 if (!%co) {
3447 next;
3448 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00003449 my $commit = $co{'id'};
Jakub Narebski581860e2006-08-14 02:09:08 +02003450
3451 my $ref = format_ref_marker($refs, $commit);
3452
3453 if ($alternate) {
3454 print "<tr class=\"dark\">\n";
3455 } else {
3456 print "<tr class=\"light\">\n";
3457 }
3458 $alternate ^= 1;
3459 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3460 # shortlog uses chop_str($co{'author_name'}, 10)
3461 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3462 "<td>";
3463 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02003464 print format_subject_html($co{'title'}, $co{'title_short'},
3465 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02003466 print "</td>\n" .
3467 "<td class=\"link\">" .
Luben Tuikov6d81c5a2006-09-28 17:21:07 -07003468 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3469 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
Jakub Narebski581860e2006-08-14 02:09:08 +02003470
3471 if ($ftype eq 'blob') {
3472 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3473 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3474 if (defined $blob_current && defined $blob_parent &&
3475 $blob_current ne $blob_parent) {
3476 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02003477 $cgi->a({-href => href(action=>"blobdiff",
3478 hash=>$blob_current, hash_parent=>$blob_parent,
3479 hash_base=>$hash_base, hash_parent_base=>$commit,
3480 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02003481 "diff to current");
3482 }
3483 }
3484 print "</td>\n" .
3485 "</tr>\n";
3486 }
3487 if (defined $extra) {
3488 print "<tr>\n" .
3489 "<td colspan=\"4\">$extra</td>\n" .
3490 "</tr>\n";
3491 }
3492 print "</table>\n";
3493}
3494
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003495sub git_tags_body {
3496 # uses global variable $project
3497 my ($taglist, $from, $to, $extra) = @_;
3498 $from = 0 unless defined $from;
3499 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3500
3501 print "<table class=\"tags\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003502 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003503 for (my $i = $from; $i <= $to; $i++) {
3504 my $entry = $taglist->[$i];
3505 my %tag = %$entry;
Jakub Narebskicd146402006-11-02 20:23:11 +01003506 my $comment = $tag{'subject'};
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003507 my $comment_short;
3508 if (defined $comment) {
3509 $comment_short = chop_str($comment, 30, 5);
3510 }
3511 if ($alternate) {
3512 print "<tr class=\"dark\">\n";
3513 } else {
3514 print "<tr class=\"light\">\n";
3515 }
3516 $alternate ^= 1;
Jakub Narebski27dd1a82007-01-03 22:54:29 +01003517 if (defined $tag{'age'}) {
3518 print "<td><i>$tag{'age'}</i></td>\n";
3519 } else {
3520 print "<td></td>\n";
3521 }
3522 print "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003523 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02003524 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003525 "</td>\n" .
3526 "<td>";
3527 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02003528 print format_subject_html($comment, $comment_short,
3529 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003530 }
3531 print "</td>\n" .
3532 "<td class=\"selflink\">";
3533 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003534 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003535 } else {
3536 print "&nbsp;";
3537 }
3538 print "</td>\n" .
3539 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003540 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003541 if ($tag{'reftype'} eq "commit") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003542 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
Jakub Narebskicd146402006-11-02 20:23:11 +01003543 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003544 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003545 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003546 }
3547 print "</td>\n" .
3548 "</tr>";
3549 }
3550 if (defined $extra) {
3551 print "<tr>\n" .
3552 "<td colspan=\"5\">$extra</td>\n" .
3553 "</tr>\n";
3554 }
3555 print "</table>\n";
3556}
3557
3558sub git_heads_body {
3559 # uses global variable $project
Jakub Narebski120ddde2006-09-19 14:33:22 +02003560 my ($headlist, $head, $from, $to, $extra) = @_;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003561 $from = 0 unless defined $from;
Jakub Narebski120ddde2006-09-19 14:33:22 +02003562 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003563
3564 print "<table class=\"heads\" cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07003565 my $alternate = 1;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003566 for (my $i = $from; $i <= $to; $i++) {
Jakub Narebski120ddde2006-09-19 14:33:22 +02003567 my $entry = $headlist->[$i];
Jakub Narebskicd146402006-11-02 20:23:11 +01003568 my %ref = %$entry;
3569 my $curr = $ref{'id'} eq $head;
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003570 if ($alternate) {
3571 print "<tr class=\"dark\">\n";
3572 } else {
3573 print "<tr class=\"light\">\n";
3574 }
3575 $alternate ^= 1;
Jakub Narebskicd146402006-11-02 20:23:11 +01003576 print "<td><i>$ref{'age'}</i></td>\n" .
3577 ($curr ? "<td class=\"current_head\">" : "<td>") .
3578 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3579 -class => "list name"},esc_html($ref{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003580 "</td>\n" .
3581 "<td class=\"link\">" .
Jakub Narebskicd146402006-11-02 20:23:11 +01003582 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3583 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3584 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003585 "</td>\n" .
3586 "</tr>";
3587 }
3588 if (defined $extra) {
3589 print "<tr>\n" .
3590 "<td colspan=\"3\">$extra</td>\n" .
3591 "</tr>\n";
3592 }
3593 print "</table>\n";
3594}
3595
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003596sub git_search_grep_body {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003597 my ($commitlist, $from, $to, $extra) = @_;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003598 $from = 0 unless defined $from;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003599 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003600
3601 print "<table class=\"grep\" cellspacing=\"0\">\n";
3602 my $alternate = 1;
3603 for (my $i = $from; $i <= $to; $i++) {
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003604 my %co = %{$commitlist->[$i]};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003605 if (!%co) {
3606 next;
3607 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00003608 my $commit = $co{'id'};
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003609 if ($alternate) {
3610 print "<tr class=\"dark\">\n";
3611 } else {
3612 print "<tr class=\"light\">\n";
3613 }
3614 $alternate ^= 1;
3615 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3616 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3617 "<td>" .
3618 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3619 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3620 my $comment = $co{'comment'};
3621 foreach my $line (@$comment) {
Jakub Narebski7e431ef2007-05-16 01:56:10 +02003622 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00003623 my $lead = esc_html($1) || "";
3624 $lead = chop_str($lead, 30, 10);
3625 my $match = esc_html($2) || "";
3626 my $trail = esc_html($3) || "";
3627 $trail = chop_str($trail, 30, 10);
3628 my $text = "$lead<span class=\"match\">$match</span>$trail";
3629 print chop_str($text, 80, 5) . "<br/>\n";
3630 }
3631 }
3632 print "</td>\n" .
3633 "<td class=\"link\">" .
3634 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3635 " | " .
3636 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3637 print "</td>\n" .
3638 "</tr>\n";
3639 }
3640 if (defined $extra) {
3641 print "<tr>\n" .
3642 "<td colspan=\"3\">$extra</td>\n" .
3643 "</tr>\n";
3644 }
3645 print "</table>\n";
3646}
3647
Jakub Narebski717b8312006-07-31 21:22:15 +02003648## ======================================================================
3649## ======================================================================
3650## actions
3651
Jakub Narebski717b8312006-07-31 21:22:15 +02003652sub git_project_list {
Jakub Narebski6326b602006-08-01 02:59:12 +02003653 my $order = $cgi->param('o');
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003654 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Jakub Narebskie2860ea2006-08-05 13:15:24 +02003655 die_error(undef, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02003656 }
3657
Jakub Narebski847e01f2006-08-14 02:05:47 +02003658 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02003659 if (!@list) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003660 die_error(undef, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02003661 }
Jakub Narebski6326b602006-08-01 02:59:12 +02003662
Jakub Narebski717b8312006-07-31 21:22:15 +02003663 git_header_html();
3664 if (-f $home_text) {
3665 print "<div class=\"index_include\">\n";
3666 open (my $fd, $home_text);
3667 print <$fd>;
3668 close $fd;
3669 print "</div>\n";
3670 }
Petr Baudise30496d2006-10-24 05:33:17 +02003671 git_project_list_body(\@list, $order);
3672 git_footer_html();
3673}
3674
3675sub git_forks {
3676 my $order = $cgi->param('o');
Frank Lichtenheldb06dcf82007-04-06 23:58:24 +02003677 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
Petr Baudise30496d2006-10-24 05:33:17 +02003678 die_error(undef, "Unknown order parameter");
Jakub Narebski717b8312006-07-31 21:22:15 +02003679 }
Petr Baudise30496d2006-10-24 05:33:17 +02003680
3681 my @list = git_get_projects_list($project);
3682 if (!@list) {
3683 die_error(undef, "No forks found");
Jakub Narebski717b8312006-07-31 21:22:15 +02003684 }
Petr Baudise30496d2006-10-24 05:33:17 +02003685
3686 git_header_html();
3687 git_print_page_nav('','');
3688 git_print_header_div('summary', "$project forks");
3689 git_project_list_body(\@list, $order);
Jakub Narebski717b8312006-07-31 21:22:15 +02003690 git_footer_html();
3691}
3692
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003693sub git_project_index {
Petr Baudise30496d2006-10-24 05:33:17 +02003694 my @projects = git_get_projects_list($project);
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003695
3696 print $cgi->header(
3697 -type => 'text/plain',
3698 -charset => 'utf-8',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02003699 -content_disposition => 'inline; filename="index.aux"');
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003700
3701 foreach my $pr (@projects) {
3702 if (!exists $pr->{'owner'}) {
Miklos Vajna76e4f5d2007-07-04 00:11:23 +02003703 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02003704 }
3705
3706 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3707 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3708 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3709 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3710 $path =~ s/ /\+/g;
3711 $owner =~ s/ /\+/g;
3712
3713 print "$path $owner\n";
3714 }
3715}
3716
Kay Sieversede5e102005-08-07 20:23:12 +02003717sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003718 my $descr = git_get_project_description($project) || "none";
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00003719 my %co = parse_commit("HEAD");
Jakub Narebski785cdea2007-05-13 12:39:22 +02003720 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
Robert Fitzsimonsa979d122006-12-22 19:38:15 +00003721 my $head = $co{'id'};
Kay Sieversede5e102005-08-07 20:23:12 +02003722
Jakub Narebski1e0cf032006-08-14 02:10:06 +02003723 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02003724
Jakub Narebskicd146402006-11-02 20:23:11 +01003725 my $refs = git_get_references();
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003726 # These get_*_list functions return one more to allow us to see if
3727 # there are more ...
3728 my @taglist = git_get_tags_list(16);
3729 my @headlist = git_get_heads_list(16);
Petr Baudise30496d2006-10-24 05:33:17 +02003730 my @forklist;
Junio C Hamano5dd5ed02006-11-07 22:00:45 -08003731 my ($check_forks) = gitweb_check_feature('forks');
3732
3733 if ($check_forks) {
Petr Baudise30496d2006-10-24 05:33:17 +02003734 @forklist = git_get_projects_list($project);
3735 }
Jakub Narebski120ddde2006-09-19 14:33:22 +02003736
Kay Sieversede5e102005-08-07 20:23:12 +02003737 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003738 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003739
Kay Sievers19806692005-08-07 20:26:27 +02003740 print "<div class=\"title\">&nbsp;</div>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02003741 print "<table cellspacing=\"0\">\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003742 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
Jakub Narebski785cdea2007-05-13 12:39:22 +02003743 "<tr><td>owner</td><td>$owner</td></tr>\n";
3744 if (defined $cd{'rfc2822'}) {
3745 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3746 }
3747
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003748 # use per project git URL list in $projectroot/$project/cloneurl
3749 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02003750 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02003751 my @url_list = git_get_project_url_list($project);
3752 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3753 foreach my $git_url (@url_list) {
3754 next unless $git_url;
3755 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02003756 $url_tag = "";
3757 }
3758 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003759
Petr Baudis447ef092006-10-24 05:23:46 +02003760 if (-s "$projectroot/$project/README.html") {
3761 if (open my $fd, "$projectroot/$project/README.html") {
3762 print "<div class=\"title\">readme</div>\n";
3763 print $_ while (<$fd>);
3764 close $fd;
3765 }
3766 }
3767
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003768 # we need to request one more than 16 (0..15) to check if
3769 # those 16 are all
Jakub Narebski785cdea2007-05-13 12:39:22 +02003770 my @commitlist = $head ? parse_commits($head, 17) : ();
3771 if (@commitlist) {
3772 git_print_header_div('shortlog');
3773 git_shortlog_body(\@commitlist, 0, 15, $refs,
3774 $#commitlist <= 15 ? undef :
3775 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3776 }
Kay Sieversede5e102005-08-07 20:23:12 +02003777
Jakub Narebski120ddde2006-09-19 14:33:22 +02003778 if (@taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003779 git_print_header_div('tags');
Jakub Narebski120ddde2006-09-19 14:33:22 +02003780 git_tags_body(\@taglist, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003781 $#taglist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003782 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02003783 }
Kay Sievers0db37972005-08-07 20:24:35 +02003784
Jakub Narebski120ddde2006-09-19 14:33:22 +02003785 if (@headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003786 git_print_header_div('heads');
Jakub Narebski120ddde2006-09-19 14:33:22 +02003787 git_heads_body(\@headlist, $head, 0, 15,
Robert Fitzsimons313ce8c2006-12-19 12:08:54 +00003788 $#headlist <= 15 ? undef :
Martin Waitz1c2a4f52006-08-16 00:24:30 +02003789 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02003790 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003791
Petr Baudise30496d2006-10-24 05:33:17 +02003792 if (@forklist) {
3793 git_print_header_div('forks');
3794 git_project_list_body(\@forklist, undef, 0, 15,
Robert Fitzsimonsaaca9672006-12-22 19:38:12 +00003795 $#forklist <= 15 ? undef :
Petr Baudise30496d2006-10-24 05:33:17 +02003796 $cgi->a({-href => href(action=>"forks")}, "..."),
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003797 'noheader');
Petr Baudise30496d2006-10-24 05:33:17 +02003798 }
3799
Kay Sieversede5e102005-08-07 20:23:12 +02003800 git_footer_html();
3801}
3802
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003803sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003804 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003805 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003806 git_print_page_nav('','', $head,undef,$head);
3807 my %tag = parse_tag($hash);
Jakub Narebski198a2a82007-05-12 21:16:34 +02003808
3809 if (! %tag) {
3810 die_error(undef, "Unknown tag object");
3811 }
3812
Jakub Narebski847e01f2006-08-14 02:05:47 +02003813 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003814 print "<div class=\"title_text\">\n" .
3815 "<table cellspacing=\"0\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02003816 "<tr>\n" .
3817 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003818 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3819 $tag{'object'}) . "</td>\n" .
3820 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3821 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02003822 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003823 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003824 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01003825 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003826 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3827 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3828 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003829 }
3830 print "</table>\n\n" .
3831 "</div>\n";
3832 print "<div class=\"page_body\">";
3833 my $comment = $tag{'comment'};
3834 foreach my $line (@$comment) {
Junio C Hamano70022432006-11-24 14:04:01 -08003835 chomp $line;
Jakub Narebski793c4002006-11-24 22:25:50 +01003836 print esc_html($line, -nbsp=>1) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02003837 }
3838 print "</div>\n";
3839 git_footer_html();
3840}
3841
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003842sub git_blame2 {
3843 my $fd;
3844 my $ftype;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303845
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05303846 my ($have_blame) = gitweb_check_feature('blame');
3847 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303848 die_error('403 Permission denied', "Permission denied");
3849 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003850 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003851 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003852 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003853 my %co = parse_commit($hash_base)
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003854 or die_error(undef, "Reading commit failed");
3855 if (!defined $hash) {
3856 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3857 or die_error(undef, "Error looking up file");
3858 }
3859 $ftype = git_get_type($hash);
3860 if ($ftype !~ "blob") {
Jakub Narebskif73bbb22007-03-27 02:07:11 +02003861 die_error('400 Bad Request', "Object is not a blob");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003862 }
Junio C Hamano48fd6882006-10-12 00:47:03 -07003863 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003864 $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003865 or die_error(undef, "Open git-blame failed");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003866 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003867 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02003868 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3869 "blob") .
3870 " | " .
Petr Baudiscae18622006-09-22 03:19:41 +02003871 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3872 "history") .
3873 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003874 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02003875 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02003876 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3877 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003878 git_print_page_path($file_name, $ftype, $hash_base);
Luben Tuikov82f930d2006-08-04 15:09:59 -07003879 my @rev_color = (qw(light2 dark2));
Luben Tuikovcc1bf972006-07-23 13:37:53 -07003880 my $num_colors = scalar(@rev_color);
3881 my $current_color = 0;
3882 my $last_rev;
Jakub Narebski59b9f612006-08-22 23:42:53 +02003883 print <<HTML;
3884<div class="page_body">
3885<table class="blame">
3886<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3887HTML
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003888 my %metainfo = ();
3889 while (1) {
3890 $_ = <$fd>;
3891 last unless defined $_;
Luben Tuikovd15c55a2006-10-11 00:30:05 -07003892 my ($full_rev, $orig_lineno, $lineno, $group_size) =
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003893 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3894 if (!exists $metainfo{$full_rev}) {
3895 $metainfo{$full_rev} = {};
3896 }
3897 my $meta = $metainfo{$full_rev};
3898 while (<$fd>) {
3899 last if (s/^\t//);
3900 if (/^(\S+) (.*)$/) {
3901 $meta->{$1} = $2;
3902 }
3903 }
3904 my $data = $_;
Junio C Hamano70022432006-11-24 14:04:01 -08003905 chomp $data;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003906 my $rev = substr($full_rev, 0, 8);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003907 my $author = $meta->{'author'};
3908 my %date = parse_date($meta->{'author-time'},
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003909 $meta->{'author-tz'});
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003910 my $date = $date{'iso-tz'};
3911 if ($group_size) {
Luben Tuikovcc1bf972006-07-23 13:37:53 -07003912 $current_color = ++$current_color % $num_colors;
3913 }
3914 print "<tr class=\"$rev_color[$current_color]\">\n";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003915 if ($group_size) {
3916 print "<td class=\"sha1\"";
Luben Tuikov5ad08282006-10-30 12:37:54 -08003917 print " title=\"". esc_html($author) . ", $date\"";
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003918 print " rowspan=\"$group_size\"" if ($group_size > 1);
3919 print ">";
3920 print $cgi->a({-href => href(action=>"commit",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003921 hash=>$full_rev,
3922 file_name=>$file_name)},
3923 esc_html($rev));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003924 print "</td>\n";
Luben Tuikov9dc5f8c2006-10-04 00:12:17 -07003925 }
Luben Tuikov244a70e2007-01-04 18:37:45 -08003926 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
Jakub Narebskif73bbb22007-03-27 02:07:11 +02003927 or die_error(undef, "Open git-rev-parse failed");
Luben Tuikov244a70e2007-01-04 18:37:45 -08003928 my $parent_commit = <$dd>;
3929 close $dd;
3930 chomp($parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003931 my $blamed = href(action => 'blame',
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003932 file_name => $meta->{'filename'},
3933 hash_base => $parent_commit);
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003934 print "<td class=\"linenr\">";
3935 print $cgi->a({ -href => "$blamed#l$orig_lineno",
Jakub Narebskia23f0a72007-04-01 22:21:38 +02003936 -id => "l$lineno",
3937 -class => "linenr" },
3938 esc_html($lineno));
Junio C Hamanoeeef88c2006-10-05 13:55:58 -07003939 print "</td>";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003940 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3941 print "</tr>\n";
3942 }
3943 print "</table>\n";
3944 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02003945 close $fd
3946 or print "Reading blob failed\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07003947 git_footer_html();
3948}
3949
Florian Forstere34ef622006-06-11 17:45:19 +02003950sub git_blame {
3951 my $fd;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303952
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05303953 my ($have_blame) = gitweb_check_feature('blame');
3954 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05303955 die_error('403 Permission denied', "Permission denied");
3956 }
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003957 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003958 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003959 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02003960 my %co = parse_commit($hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003961 or die_error(undef, "Reading commit failed");
Florian Forstere34ef622006-06-11 17:45:19 +02003962 if (!defined $hash) {
3963 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003964 or die_error(undef, "Error lookup file");
Florian Forstere34ef622006-06-11 17:45:19 +02003965 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003966 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003967 or die_error(undef, "Open git-annotate failed");
Florian Forstere34ef622006-06-11 17:45:19 +02003968 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003969 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02003970 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3971 "blob") .
3972 " | " .
Petr Baudiscae18622006-09-22 03:19:41 +02003973 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3974 "history") .
3975 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003976 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02003977 "HEAD");
Jakub Narebski847e01f2006-08-14 02:05:47 +02003978 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3979 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003980 git_print_page_path($file_name, 'blob', $hash_base);
Florian Forstere34ef622006-06-11 17:45:19 +02003981 print "<div class=\"page_body\">\n";
3982 print <<HTML;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00003983<table class="blame">
Florian Forstere34ef622006-06-11 17:45:19 +02003984 <tr>
3985 <th>Commit</th>
3986 <th>Age</th>
3987 <th>Author</th>
3988 <th>Line</th>
3989 <th>Data</th>
3990 </tr>
3991HTML
3992 my @line_class = (qw(light dark));
3993 my $line_class_len = scalar (@line_class);
3994 my $line_class_num = $#line_class;
3995 while (my $line = <$fd>) {
3996 my $long_rev;
3997 my $short_rev;
3998 my $author;
3999 my $time;
4000 my $lineno;
4001 my $data;
4002 my $age;
4003 my $age_str;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004004 my $age_class;
Florian Forstere34ef622006-06-11 17:45:19 +02004005
4006 chomp $line;
4007 $line_class_num = ($line_class_num + 1) % $line_class_len;
4008
Jakub Narebski030b5202006-08-26 02:13:05 +02004009 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 +02004010 $long_rev = $1;
4011 $author = $2;
4012 $time = $3;
4013 $lineno = $4;
4014 $data = $5;
4015 } else {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004016 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
Florian Forstere34ef622006-06-11 17:45:19 +02004017 next;
4018 }
4019 $short_rev = substr ($long_rev, 0, 8);
4020 $age = time () - $time;
4021 $age_str = age_string ($age);
4022 $age_str =~ s/ /&nbsp;/g;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004023 $age_class = age_class($age);
Florian Forstere34ef622006-06-11 17:45:19 +02004024 $author = esc_html ($author);
4025 $author =~ s/ /&nbsp;/g;
Jakub Narebskif16db172006-08-06 02:08:31 +02004026
4027 $data = untabify($data);
Florian Forstere34ef622006-06-11 17:45:19 +02004028 $data = esc_html ($data);
Florian Forstere34ef622006-06-11 17:45:19 +02004029
4030 print <<HTML;
4031 <tr class="$line_class[$line_class_num]">
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004032 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004033 <td class="$age_class">$age_str</td>
Florian Forstere34ef622006-06-11 17:45:19 +02004034 <td>$author</td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004035 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4036 <td class="pre">$data</td>
Florian Forstere34ef622006-06-11 17:45:19 +02004037 </tr>
4038HTML
4039 } # while (my $line = <$fd>)
4040 print "</table>\n\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004041 close $fd
4042 or print "Reading blob failed.\n";
Florian Forstere34ef622006-06-11 17:45:19 +02004043 print "</div>";
4044 git_footer_html();
4045}
4046
Kay Sieversede5e102005-08-07 20:23:12 +02004047sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004048 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02004049 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004050 git_print_page_nav('','', $head,undef,$head);
4051 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004052
Jakub Narebskicd146402006-11-02 20:23:11 +01004053 my @tagslist = git_get_tags_list();
4054 if (@tagslist) {
4055 git_tags_body(\@tagslist);
Kay Sieversede5e102005-08-07 20:23:12 +02004056 }
Kay Sieversede5e102005-08-07 20:23:12 +02004057 git_footer_html();
4058}
4059
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02004060sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004061 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004062 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004063 git_print_page_nav('','', $head,undef,$head);
4064 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004065
Jakub Narebskicd146402006-11-02 20:23:11 +01004066 my @headslist = git_get_heads_list();
4067 if (@headslist) {
4068 git_heads_body(\@headslist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02004069 }
Kay Sievers0db37972005-08-07 20:24:35 +02004070 git_footer_html();
4071}
4072
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004073sub git_blob_plain {
Jakub Narebskif2e73302006-08-26 19:14:25 +02004074 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004075
Luben Tuikovcff07712006-07-23 13:28:55 -07004076 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004077 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004078 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004079 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004080 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004081 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004082 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004083 }
Martin Waitz800764c2006-09-16 23:09:02 +02004084 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4085 # blobs defined by non-textual hash id's can be cached
4086 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004087 }
Martin Waitz800764c2006-09-16 23:09:02 +02004088
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004089 my $type = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004090 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicfd82662006-08-05 12:56:04 +02004091 or die_error(undef, "Couldn't cat $file_name, $hash");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004092
Jakub Narebski847e01f2006-08-14 02:05:47 +02004093 $type ||= blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004094
4095 # save as filename, even when no $file_name is given
4096 my $save_as = "$hash";
4097 if (defined $file_name) {
4098 $save_as = $file_name;
4099 } elsif ($type =~ m/^text\//) {
4100 $save_as .= '.txt';
4101 }
4102
Jakub Narebskif2e73302006-08-26 19:14:25 +02004103 print $cgi->header(
4104 -type => "$type",
4105 -expires=>$expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004106 -content_disposition => 'inline; filename="' . "$save_as" . '"');
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004107 undef $/;
4108 binmode STDOUT, ':raw';
4109 print <$fd>;
4110 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4111 $/ = "\n";
4112 close $fd;
4113}
4114
Kay Sievers09bd7892005-08-07 20:21:23 +02004115sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02004116 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02004117
Luben Tuikovcff07712006-07-23 13:28:55 -07004118 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004119 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004120 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004121 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004122 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004123 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004124 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004125 }
Martin Waitz800764c2006-09-16 23:09:02 +02004126 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4127 # blobs defined by non-textual hash id's can be cached
4128 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02004129 }
Martin Waitz800764c2006-09-16 23:09:02 +02004130
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05304131 my ($have_blame) = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004132 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004133 or die_error(undef, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02004134 my $mimetype = blob_mimetype($fd, $file_name);
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004135 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
Luben Tuikov930cf7d2006-07-09 20:18:57 -07004136 close $fd;
4137 return git_blob_plain($mimetype);
4138 }
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004139 # we can have blame only for text/* mimetype
4140 $have_blame &&= ($mimetype =~ m!^text/!);
4141
Jakub Narebskif2e73302006-08-26 19:14:25 +02004142 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004143 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02004144 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02004145 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02004146 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004147 $formats_nav .=
4148 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4149 hash=>$hash, file_name=>$file_name)},
4150 "blame") .
4151 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02004152 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004153 $formats_nav .=
Petr Baudiscae18622006-09-22 03:19:41 +02004154 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4155 hash=>$hash, file_name=>$file_name)},
4156 "history") .
4157 " | " .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004158 $cgi->a({-href => href(action=>"blob_plain",
4159 hash=>$hash, file_name=>$file_name)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004160 "raw") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004161 " | " .
4162 $cgi->a({-href => href(action=>"blob",
4163 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004164 "HEAD");
Kay Sievers93129442005-10-17 03:27:54 +02004165 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004166 $formats_nav .=
Petr Baudis35329cc2006-09-22 03:19:50 +02004167 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
Kay Sievers93129442005-10-17 03:27:54 +02004168 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004169 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4170 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004171 } else {
4172 print "<div class=\"page_nav\">\n" .
4173 "<br/><br/></div>\n" .
4174 "<div class=\"title\">$hash</div>\n";
4175 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004176 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02004177 print "<div class=\"page_body\">\n";
Jakub Narebski5a4cf332006-12-04 23:47:22 +01004178 if ($mimetype =~ m!^text/!) {
4179 my $nr;
4180 while (my $line = <$fd>) {
4181 chomp $line;
4182 $nr++;
4183 $line = untabify($line);
4184 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4185 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4186 }
4187 } elsif ($mimetype =~ m!^image/!) {
4188 print qq!<img type="$mimetype"!;
4189 if ($file_name) {
4190 print qq! alt="$file_name" title="$file_name"!;
4191 }
4192 print qq! src="! .
4193 href(action=>"blob_plain", hash=>$hash,
4194 hash_base=>$hash_base, file_name=>$file_name) .
4195 qq!" />\n!;
Kay Sievers161332a2005-08-07 19:49:46 +02004196 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02004197 close $fd
4198 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02004199 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004200 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004201}
4202
4203sub git_tree {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004204 if (!defined $hash_base) {
4205 $hash_base = "HEAD";
4206 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02004207 if (!defined $hash) {
Kay Sievers09bd7892005-08-07 20:21:23 +02004208 if (defined $file_name) {
Luben Tuikov6f7ea5f2006-09-29 09:57:43 -07004209 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4210 } else {
4211 $hash = $hash_base;
Kay Sievers10dba282005-08-07 20:25:27 +02004212 }
Kay Sieverse925f382005-08-07 20:23:35 +02004213 }
Kay Sievers232ff552005-11-24 16:56:55 +01004214 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004215 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004216 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02004217 my @entries = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004218 close $fd or die_error(undef, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01004219 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02004220
Jakub Narebski847e01f2006-08-14 02:05:47 +02004221 my $refs = git_get_references();
4222 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02004223 git_header_html();
Jakub Narebski300454f2006-10-21 17:53:09 +02004224 my $basedir = '';
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05304225 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02004226 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Petr Baudiscae18622006-09-22 03:19:41 +02004227 my @views_nav = ();
4228 if (defined $file_name) {
4229 push @views_nav,
4230 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4231 hash=>$hash, file_name=>$file_name)},
4232 "history"),
4233 $cgi->a({-href => href(action=>"tree",
4234 hash_base=>"HEAD", file_name=>$file_name)},
Petr Baudisf35274d2006-09-22 03:19:53 +02004235 "HEAD"),
Petr Baudiscae18622006-09-22 03:19:41 +02004236 }
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004237 my $snapshot_links = format_snapshot_links($hash);
4238 if (defined $snapshot_links) {
Petr Baudiscae18622006-09-22 03:19:41 +02004239 # FIXME: Should be available when we have no hash base as well.
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004240 push @views_nav, $snapshot_links;
Petr Baudiscae18622006-09-22 03:19:41 +02004241 }
4242 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
Jakub Narebski847e01f2006-08-14 02:05:47 +02004243 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02004244 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02004245 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02004246 print "<div class=\"page_nav\">\n";
4247 print "<br/><br/></div>\n";
4248 print "<div class=\"title\">$hash</div>\n";
4249 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004250 if (defined $file_name) {
Jakub Narebski300454f2006-10-21 17:53:09 +02004251 $basedir = $file_name;
4252 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4253 $basedir .= '/';
4254 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004255 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07004256 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sieversfbb592a2005-08-07 20:12:11 +02004257 print "<div class=\"page_body\">\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02004258 print "<table cellspacing=\"0\">\n";
Luben Tuikov6dd36ac2006-09-28 16:47:50 -07004259 my $alternate = 1;
Jakub Narebskib6b7fc72006-10-21 17:54:44 +02004260 # '..' (top directory) link if possible
4261 if (defined $hash_base &&
4262 defined $file_name && $file_name =~ m![^/]+$!) {
4263 if ($alternate) {
4264 print "<tr class=\"dark\">\n";
4265 } else {
4266 print "<tr class=\"light\">\n";
4267 }
4268 $alternate ^= 1;
4269
4270 my $up = $file_name;
4271 $up =~ s!/?[^/]+$!!;
4272 undef $up unless $up;
4273 # based on git_print_tree_entry
4274 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4275 print '<td class="list">';
4276 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4277 file_name=>$up)},
4278 "..");
4279 print "</td>\n";
4280 print "<td class=\"link\"></td>\n";
4281
4282 print "</tr>\n";
4283 }
Kay Sievers161332a2005-08-07 19:49:46 +02004284 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02004285 my %t = parse_ls_tree_line($line, -z => 1);
4286
Kay Sieversbddec012005-08-07 20:25:42 +02004287 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02004288 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004289 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02004290 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004291 }
4292 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02004293
Jakub Narebski300454f2006-10-21 17:53:09 +02004294 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
Jakub Narebskifa702002006-08-31 00:35:07 +02004295
Kay Sievers42f7eb92005-08-07 20:21:46 +02004296 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02004297 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02004298 print "</table>\n" .
4299 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02004300 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004301}
4302
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304303sub git_snapshot {
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004304 my @supported_fmts = gitweb_check_feature('snapshot');
4305
4306 my $format = $cgi->param('sf');
4307 unless ($format =~ m/[a-z0-9]+/
4308 && exists($known_snapshot_formats{$format})
4309 && grep($_ eq $format, @supported_fmts)) {
4310 die_error(undef, "Unsupported snapshot format");
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304311 }
4312
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304313 if (!defined $hash) {
4314 $hash = git_get_head_hash($project);
4315 }
4316
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004317 my $git_command = git_cmd_str();
Mark Levedahl072570e2007-05-20 11:46:46 -04004318 my $name = $project;
Matthias Lederhofer9a7d9412007-06-07 11:27:08 +02004319 $name =~ s,([^/])/*\.git$,$1,;
4320 $name = basename($name);
4321 my $filename = to_utf8($name);
Mark Levedahl072570e2007-05-20 11:46:46 -04004322 $name =~ s/\047/\047\\\047\047/g;
Mark Levedahl072570e2007-05-20 11:46:46 -04004323 my $cmd;
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004324 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4325 $cmd = "$git_command archive " .
4326 "--format=$known_snapshot_formats{$format}{'format'}" .
4327 "--prefix=\'$name\'/ $hash";
4328 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4329 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
Mark Levedahl072570e2007-05-20 11:46:46 -04004330 }
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304331
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004332 print $cgi->header(
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004333 -type => $known_snapshot_formats{$format}{'type'},
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004334 -content_disposition => 'inline; filename="' . "$filename" . '"',
Jakub Narebskiab41dfb2006-09-26 01:59:43 +02004335 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304336
Mark Levedahl072570e2007-05-20 11:46:46 -04004337 open my $fd, "-|", $cmd
4338 or die_error(undef, "Execute git-archive failed");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304339 binmode STDOUT, ':raw';
4340 print <$fd>;
4341 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4342 close $fd;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304343}
4344
Kay Sievers09bd7892005-08-07 20:21:23 +02004345sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004346 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02004347 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02004348 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02004349 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02004350 if (!defined $page) {
4351 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02004352 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004353 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02004354
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004355 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02004356
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004357 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004358
4359 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02004360 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02004361
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004362 if (!@commitlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004363 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02004364
Jakub Narebski847e01f2006-08-14 02:05:47 +02004365 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02004366 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02004367 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004368 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4369 for (my $i = 0; $i <= $to; $i++) {
4370 my %co = %{$commitlist[$i]};
Kay Sieversb87d78d2005-08-07 20:21:04 +02004371 next if !%co;
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004372 my $commit = $co{'id'};
4373 my $ref = format_ref_marker($refs, $commit);
Jakub Narebski847e01f2006-08-14 02:05:47 +02004374 my %ad = parse_date($co{'author_epoch'});
4375 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02004376 "<span class=\"age\">$co{'age_string'}</span>" .
4377 esc_html($co{'title'}) . $ref,
4378 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02004379 print "<div class=\"title_text\">\n" .
4380 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004381 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004382 " | " .
4383 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Petr Baudis6ef4cb22006-09-22 03:19:48 +02004384 " | " .
Petr Baudisd7267202006-09-22 16:56:43 -07004385 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
Kay Sieverseb282402005-08-07 20:21:34 +02004386 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02004387 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01004388 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02004389 "</div>\n";
4390
4391 print "<div class=\"log_body\">\n";
Jakub Narebskif2069412006-10-24 13:52:46 +02004392 git_print_log($co{'comment'}, -final_empty_line=> 1);
Kay Sievers09bd7892005-08-07 20:21:23 +02004393 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02004394 }
Robert Fitzsimons719dad22006-12-24 14:31:45 +00004395 if ($#commitlist >= 100) {
4396 print "<div class=\"page_nav\">\n";
4397 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4398 -accesskey => "n", -title => "Alt-n"}, "next");
4399 print "</div>\n";
4400 }
Kay Sievers034df392005-08-07 20:20:07 +02004401 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004402}
4403
4404sub git_commit {
Jakub Narebski9954f772006-11-18 23:35:41 +01004405 $hash ||= $hash_base || "HEAD";
Jakub Narebski847e01f2006-08-14 02:05:47 +02004406 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02004407 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004408 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02004409 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004410 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4411 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02004412
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004413 my $parent = $co{'parent'};
4414 my $parents = $co{'parents'}; # listref
4415
4416 # we need to prepare $formats_nav before any parameter munging
4417 my $formats_nav;
4418 if (!defined $parent) {
4419 # --root commitdiff
4420 $formats_nav .= '(initial)';
4421 } elsif (@$parents == 1) {
4422 # single parent commit
4423 $formats_nav .=
4424 '(parent: ' .
4425 $cgi->a({-href => href(action=>"commit",
4426 hash=>$parent)},
4427 esc_html(substr($parent, 0, 7))) .
4428 ')';
4429 } else {
4430 # merge commit
4431 $formats_nav .=
4432 '(merge: ' .
4433 join(' ', map {
Jakub Narebskif9308a12007-03-23 21:04:31 +01004434 $cgi->a({-href => href(action=>"commit",
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004435 hash=>$_)},
4436 esc_html(substr($_, 0, 7)));
4437 } @$parents ) .
4438 ')';
4439 }
4440
Kay Sieversd8a20ba2005-08-07 20:28:53 +02004441 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07004442 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02004443 }
Jakub Narebski549ab4a2006-12-15 17:53:45 +01004444 my @difftree;
Jakub Narebski208ecb22007-05-07 01:10:08 +02004445 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4446 @diff_opts,
4447 (@$parents <= 1 ? $parent : '-c'),
4448 $hash, "--"
4449 or die_error(undef, "Open git-diff-tree failed");
4450 @difftree = map { chomp; $_ } <$fd>;
4451 close $fd or die_error(undef, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02004452
4453 # non-textual hash id's can be cached
4454 my $expires;
4455 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4456 $expires = "+1d";
4457 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02004458 my $refs = git_get_references();
4459 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05304460
Jakub Narebski594e2122006-07-31 02:21:52 +02004461 git_header_html(undef, $expires);
Petr Baudisa1441542006-10-06 18:59:33 +02004462 git_print_page_nav('commit', '',
Jakub Narebski952c65f2006-08-22 16:52:50 +02004463 $hash, $co{'tree'}, $hash,
Jakub Narebskic9d193d2006-12-15 21:57:16 +01004464 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07004465
Kay Sieversb87d78d2005-08-07 20:21:04 +02004466 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004467 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004468 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004469 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02004470 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02004471 print "<div class=\"title_text\">\n" .
Kay Sieversb87d78d2005-08-07 20:21:04 +02004472 "<table cellspacing=\"0\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01004473 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02004474 "<tr>" .
4475 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02004476 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004477 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4478 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02004479 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02004480 printf(" (%02d:%02d %s)",
4481 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02004482 }
Kay Sieversbddec012005-08-07 20:25:42 +02004483 print "</td>" .
4484 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01004485 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02004486 print "<tr><td></td><td> $cd{'rfc2822'}" .
4487 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4488 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004489 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02004490 print "<tr>" .
4491 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00004492 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004493 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4494 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02004495 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004496 "<td class=\"link\">" .
4497 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4498 "tree");
Matt McCutchena3c8ab32007-07-22 01:30:27 +02004499 my $snapshot_links = format_snapshot_links($hash);
4500 if (defined $snapshot_links) {
4501 print " | " . $snapshot_links;
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05304502 }
4503 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02004504 "</tr>\n";
Jakub Narebski549ab4a2006-12-15 17:53:45 +01004505
Kay Sievers3e029292005-08-07 20:05:15 +02004506 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02004507 print "<tr>" .
4508 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004509 "<td class=\"sha1\">" .
4510 $cgi->a({-href => href(action=>"commit", hash=>$par),
4511 class => "list"}, $par) .
4512 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02004513 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02004514 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02004515 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02004516 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02004517 "</td>" .
4518 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02004519 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02004520 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02004521 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02004522
Kay Sieversfbb592a2005-08-07 20:12:11 +02004523 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02004524 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02004525 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004526
Jakub Narebski208ecb22007-05-07 01:10:08 +02004527 git_difftree_body(\@difftree, $hash, @$parents);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02004528
Kay Sievers12a88f22005-08-07 20:02:47 +02004529 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02004530}
4531
Jakub Narebskica946012006-12-10 13:25:47 +01004532sub git_object {
4533 # object is defined by:
4534 # - hash or hash_base alone
4535 # - hash_base and file_name
4536 my $type;
4537
4538 # - hash or hash_base alone
4539 if ($hash || ($hash_base && !defined $file_name)) {
4540 my $object_id = $hash || $hash_base;
4541
4542 my $git_command = git_cmd_str();
4543 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4544 or die_error('404 Not Found', "Object does not exist");
4545 $type = <$fd>;
4546 chomp $type;
4547 close $fd
4548 or die_error('404 Not Found', "Object does not exist");
4549
4550 # - hash_base and file_name
4551 } elsif ($hash_base && defined $file_name) {
4552 $file_name =~ s,/+$,,;
4553
4554 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4555 or die_error('404 Not Found', "Base object does not exist");
4556
4557 # here errors should not hapen
4558 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4559 or die_error(undef, "Open git-ls-tree failed");
4560 my $line = <$fd>;
4561 close $fd;
4562
4563 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4564 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4565 die_error('404 Not Found', "File or directory for given base does not exist");
4566 }
4567 $type = $2;
4568 $hash = $3;
4569 } else {
4570 die_error('404 Not Found', "Not enough information to find object");
4571 }
4572
4573 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4574 hash=>$hash, hash_base=>$hash_base,
4575 file_name=>$file_name),
4576 -status => '302 Found');
4577}
4578
Kay Sievers09bd7892005-08-07 20:21:23 +02004579sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004580 my $format = shift || 'html';
4581
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004582 my $fd;
4583 my @difftree;
4584 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004585 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004586
4587 # preparing $fd and %diffinfo for git_patchset_body
4588 # new style URI
4589 if (defined $hash_base && defined $hash_parent_base) {
4590 if (defined $file_name) {
4591 # read raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004592 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4593 $hash_parent_base, $hash_base,
Jakub Narebski5ae917a2007-03-30 23:41:26 +02004594 "--", (defined $file_parent ? $file_parent : ()), $file_name
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004595 or die_error(undef, "Open git-diff-tree failed");
4596 @difftree = map { chomp; $_ } <$fd>;
4597 close $fd
4598 or die_error(undef, "Reading git-diff-tree failed");
4599 @difftree
4600 or die_error('404 Not Found', "Blob diff not found");
4601
Jakub Narebski0aea3372006-08-27 23:45:26 +02004602 } elsif (defined $hash &&
4603 $hash =~ /[0-9a-fA-F]{40}/) {
4604 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004605
4606 # read filtered raw output
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004607 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4608 $hash_parent_base, $hash_base, "--"
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004609 or die_error(undef, "Open git-diff-tree failed");
4610 @difftree =
4611 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4612 # $hash == to_id
4613 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4614 map { chomp; $_ } <$fd>;
4615 close $fd
4616 or die_error(undef, "Reading git-diff-tree failed");
4617 @difftree
4618 or die_error('404 Not Found', "Blob diff not found");
4619
4620 } else {
4621 die_error('404 Not Found', "Missing one of the blob diff parameters");
4622 }
4623
4624 if (@difftree > 1) {
4625 die_error('404 Not Found', "Ambiguous blob diff specification");
4626 }
4627
4628 %diffinfo = parse_difftree_raw_line($difftree[0]);
4629 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4630 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4631
4632 $hash_parent ||= $diffinfo{'from_id'};
4633 $hash ||= $diffinfo{'to_id'};
4634
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004635 # non-textual hash id's can be cached
4636 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4637 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4638 $expires = '+1d';
4639 }
4640
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004641 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004642 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski957d6ea2007-04-05 13:45:41 +02004643 '-p', ($format eq 'html' ? "--full-index" : ()),
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 }
4648
4649 # old/legacy style URI
4650 if (!%diffinfo && # if new style URI failed
4651 defined $hash && defined $hash_parent) {
4652 # fake git-diff-tree raw output
4653 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4654 $diffinfo{'from_id'} = $hash_parent;
4655 $diffinfo{'to_id'} = $hash;
4656 if (defined $file_name) {
4657 if (defined $file_parent) {
4658 $diffinfo{'status'} = '2';
Petr Baudis83915482006-09-24 14:57:40 -07004659 $diffinfo{'from_file'} = $file_parent;
4660 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004661 } else { # assume not renamed
4662 $diffinfo{'status'} = '1';
Petr Baudis83915482006-09-24 14:57:40 -07004663 $diffinfo{'from_file'} = $file_name;
4664 $diffinfo{'to_file'} = $file_name;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004665 }
4666 } else { # no filename given
4667 $diffinfo{'status'} = '2';
4668 $diffinfo{'from_file'} = $hash_parent;
4669 $diffinfo{'to_file'} = $hash;
4670 }
4671
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004672 # non-textual hash id's can be cached
4673 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4674 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4675 $expires = '+1d';
4676 }
4677
4678 # open patch output
Jakub Narebski957d6ea2007-04-05 13:45:41 +02004679 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4680 '-p', ($format eq 'html' ? "--full-index" : ()),
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004681 $hash_parent, $hash, "--"
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004682 or die_error(undef, "Open git-diff failed");
4683 } else {
4684 die_error('404 Not Found', "Missing one of the blob diff parameters")
4685 unless %diffinfo;
4686 }
4687
4688 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004689 if ($format eq 'html') {
4690 my $formats_nav =
4691 $cgi->a({-href => href(action=>"blobdiff_plain",
4692 hash=>$hash, hash_parent=>$hash_parent,
4693 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4694 file_name=>$file_name, file_parent=>$file_parent)},
Petr Baudis35329cc2006-09-22 03:19:50 +02004695 "raw");
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004696 git_header_html(undef, $expires);
4697 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4698 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4699 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4700 } else {
4701 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4702 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4703 }
4704 if (defined $file_name) {
4705 git_print_page_path($file_name, "blob", $hash_base);
4706 } else {
4707 print "<div class=\"page_path\"></div>\n";
4708 }
4709
4710 } elsif ($format eq 'plain') {
4711 print $cgi->header(
4712 -type => 'text/plain',
4713 -charset => 'utf-8',
4714 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004715 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004716
4717 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4718
Kay Sievers09bd7892005-08-07 20:21:23 +02004719 } else {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004720 die_error(undef, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004721 }
4722
4723 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004724 if ($format eq 'html') {
4725 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004726
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004727 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4728 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02004729
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004730 print "</div>\n"; # class="page_body"
4731 git_footer_html();
4732
4733 } else {
4734 while (my $line = <$fd>) {
Jakub Narebski403d0902006-11-08 11:48:56 +01004735 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4736 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004737
4738 print $line;
4739
4740 last if $line =~ m!^\+\+\+!;
4741 }
4742 local $/ = undef;
4743 print <$fd>;
4744 close $fd;
4745 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004746}
4747
Kay Sievers19806692005-08-07 20:26:27 +02004748sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02004749 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02004750}
4751
Kay Sievers09bd7892005-08-07 20:21:23 +02004752sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02004753 my $format = shift || 'html';
Jakub Narebski9954f772006-11-18 23:35:41 +01004754 $hash ||= $hash_base || "HEAD";
Jakub Narebski847e01f2006-08-14 02:05:47 +02004755 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02004756 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004757 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02004758 }
Jakub Narebski151602d2006-10-23 00:37:56 +02004759
Jakub Narebskicd030c32007-06-08 13:33:28 +02004760 # choose format for commitdiff for merge
4761 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4762 $hash_parent = '--cc';
4763 }
4764 # we need to prepare $formats_nav before almost any parameter munging
Jakub Narebski151602d2006-10-23 00:37:56 +02004765 my $formats_nav;
4766 if ($format eq 'html') {
4767 $formats_nav =
4768 $cgi->a({-href => href(action=>"commitdiff_plain",
4769 hash=>$hash, hash_parent=>$hash_parent)},
4770 "raw");
4771
Jakub Narebskicd030c32007-06-08 13:33:28 +02004772 if (defined $hash_parent &&
4773 $hash_parent ne '-c' && $hash_parent ne '--cc') {
Jakub Narebski151602d2006-10-23 00:37:56 +02004774 # commitdiff with two commits given
4775 my $hash_parent_short = $hash_parent;
4776 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4777 $hash_parent_short = substr($hash_parent, 0, 7);
4778 }
4779 $formats_nav .=
Jakub Narebskiada3e1f2007-06-08 13:26:31 +02004780 ' (from';
4781 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4782 if ($co{'parents'}[$i] eq $hash_parent) {
4783 $formats_nav .= ' parent ' . ($i+1);
4784 last;
4785 }
4786 }
4787 $formats_nav .= ': ' .
Jakub Narebski151602d2006-10-23 00:37:56 +02004788 $cgi->a({-href => href(action=>"commitdiff",
4789 hash=>$hash_parent)},
4790 esc_html($hash_parent_short)) .
4791 ')';
4792 } elsif (!$co{'parent'}) {
4793 # --root commitdiff
4794 $formats_nav .= ' (initial)';
4795 } elsif (scalar @{$co{'parents'}} == 1) {
4796 # single parent commit
4797 $formats_nav .=
4798 ' (parent: ' .
4799 $cgi->a({-href => href(action=>"commitdiff",
4800 hash=>$co{'parent'})},
4801 esc_html(substr($co{'parent'}, 0, 7))) .
4802 ')';
4803 } else {
4804 # merge commit
Jakub Narebskicd030c32007-06-08 13:33:28 +02004805 if ($hash_parent eq '--cc') {
4806 $formats_nav .= ' | ' .
4807 $cgi->a({-href => href(action=>"commitdiff",
4808 hash=>$hash, hash_parent=>'-c')},
4809 'combined');
4810 } else { # $hash_parent eq '-c'
4811 $formats_nav .= ' | ' .
4812 $cgi->a({-href => href(action=>"commitdiff",
4813 hash=>$hash, hash_parent=>'--cc')},
4814 'compact');
4815 }
Jakub Narebski151602d2006-10-23 00:37:56 +02004816 $formats_nav .=
4817 ' (merge: ' .
4818 join(' ', map {
4819 $cgi->a({-href => href(action=>"commitdiff",
4820 hash=>$_)},
4821 esc_html(substr($_, 0, 7)));
4822 } @{$co{'parents'}} ) .
4823 ')';
4824 }
4825 }
4826
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004827 my $hash_parent_param = $hash_parent;
Jakub Narebskicd030c32007-06-08 13:33:28 +02004828 if (!defined $hash_parent_param) {
4829 # --cc for multiple parents, --root for parentless
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004830 $hash_parent_param =
Jakub Narebskicd030c32007-06-08 13:33:28 +02004831 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02004832 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004833
4834 # read commitdiff
4835 my $fd;
4836 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02004837 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004838 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski45bd0c82006-10-30 22:29:06 +01004839 "--no-commit-id", "--patch-with-raw", "--full-index",
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004840 $hash_parent_param, $hash, "--"
Jakub Narebskieee08902006-08-24 00:15:14 +02004841 or die_error(undef, "Open git-diff-tree failed");
4842
Jakub Narebski04408c32006-11-18 23:35:38 +01004843 while (my $line = <$fd>) {
4844 chomp $line;
Jakub Narebskieee08902006-08-24 00:15:14 +02004845 # empty line ends raw part of diff-tree output
4846 last unless $line;
Jakub Narebski493e01d2007-05-07 01:10:06 +02004847 push @difftree, scalar parse_difftree_raw_line($line);
Jakub Narebskieee08902006-08-24 00:15:14 +02004848 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004849
Jakub Narebskieee08902006-08-24 00:15:14 +02004850 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004851 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskifb1dde42007-05-07 01:10:07 +02004852 '-p', $hash_parent_param, $hash, "--"
Jakub Narebskieee08902006-08-24 00:15:14 +02004853 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02004854
Jakub Narebskieee08902006-08-24 00:15:14 +02004855 } else {
4856 die_error(undef, "Unknown commitdiff format");
4857 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02004858
Kay Sievers11044292005-10-19 03:18:45 +02004859 # non-textual hash id's can be cached
4860 my $expires;
4861 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4862 $expires = "+1d";
4863 }
Kay Sievers09bd7892005-08-07 20:21:23 +02004864
Jakub Narebskieee08902006-08-24 00:15:14 +02004865 # write commit message
4866 if ($format eq 'html') {
4867 my $refs = git_get_references();
4868 my $ref = format_ref_marker($refs, $co{'id'});
Kay Sievers19806692005-08-07 20:26:27 +02004869
Jakub Narebskieee08902006-08-24 00:15:14 +02004870 git_header_html(undef, $expires);
4871 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4872 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02004873 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02004874 print "<div class=\"page_body\">\n";
Jakub Narebski82560982006-10-24 13:55:33 +02004875 if (@{$co{'comment'}} > 1) {
4876 print "<div class=\"log\">\n";
4877 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4878 print "</div>\n"; # class="log"
4879 }
Kay Sievers1b1cd422005-08-07 20:28:01 +02004880
Jakub Narebskieee08902006-08-24 00:15:14 +02004881 } elsif ($format eq 'plain') {
4882 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004883 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02004884 my $filename = basename($project) . "-$hash.patch";
4885
4886 print $cgi->header(
4887 -type => 'text/plain',
4888 -charset => 'utf-8',
4889 -expires => $expires,
Luben Tuikova2a3bf72006-09-28 16:51:43 -07004890 -content_disposition => 'inline; filename="' . "$filename" . '"');
Jakub Narebskieee08902006-08-24 00:15:14 +02004891 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4892 print <<TEXT;
Jakub Narebski59b9f612006-08-22 23:42:53 +02004893From: $co{'author'}
4894Date: $ad{'rfc2822'} ($ad{'tz_local'})
4895Subject: $co{'title'}
4896TEXT
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004897 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02004898 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02004899
Jakub Narebskieee08902006-08-24 00:15:14 +02004900 foreach my $line (@{$co{'comment'}}) {
4901 print "$line\n";
Kay Sievers19806692005-08-07 20:26:27 +02004902 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004903 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02004904 }
Jakub Narebskieee08902006-08-24 00:15:14 +02004905
4906 # write patch
4907 if ($format eq 'html') {
Jakub Narebskicd030c32007-06-08 13:33:28 +02004908 my $use_parents = !defined $hash_parent ||
4909 $hash_parent eq '-c' || $hash_parent eq '--cc';
4910 git_difftree_body(\@difftree, $hash,
4911 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebskib4657e72006-08-28 14:48:14 +02004912 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02004913
Jakub Narebskicd030c32007-06-08 13:33:28 +02004914 git_patchset_body($fd, \@difftree, $hash,
4915 $use_parents ? @{$co{'parents'}} : $hash_parent);
Jakub Narebski157e43b2006-08-24 19:34:36 +02004916 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02004917 print "</div>\n"; # class="page_body"
4918 git_footer_html();
4919
4920 } elsif ($format eq 'plain') {
4921 local $/ = undef;
4922 print <$fd>;
4923 close $fd
4924 or print "Reading git-diff-tree failed\n";
4925 }
4926}
4927
4928sub git_commitdiff_plain {
4929 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02004930}
4931
Kay Sievers09bd7892005-08-07 20:21:23 +02004932sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07004933 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02004934 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02004935 }
Jakub Narebski8be68352006-09-11 00:36:04 +02004936 if (!defined $page) {
4937 $page = 0;
4938 }
Luben Tuikov63433102006-07-23 13:31:15 -07004939 my $ftype;
Jakub Narebski847e01f2006-08-14 02:05:47 +02004940 my %co = parse_commit($hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02004941 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02004942 die_error(undef, "Unknown commit object");
Kay Sievers2ae100d2005-08-07 20:17:00 +02004943 }
Jakub Narebski8be68352006-09-11 00:36:04 +02004944
Jakub Narebski847e01f2006-08-14 02:05:47 +02004945 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02004946 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4947
Luben Tuikov93d5f062006-07-23 13:30:08 -07004948 if (!defined $hash && defined $file_name) {
4949 $hash = git_get_hash_by_path($hash_base, $file_name);
4950 }
Luben Tuikovcff07712006-07-23 13:28:55 -07004951 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07004952 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07004953 }
Kay Sievers10dba282005-08-07 20:25:27 +02004954
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004955 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
Dennis Stosberg25691fb2006-08-28 17:49:58 +02004956
Jakub Narebski8be68352006-09-11 00:36:04 +02004957 my $paging_nav = '';
4958 if ($page > 0) {
4959 $paging_nav .=
4960 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4961 file_name=>$file_name)},
4962 "first");
4963 $paging_nav .= " &sdot; " .
4964 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4965 file_name=>$file_name, page=>$page-1),
4966 -accesskey => "p", -title => "Alt-p"}, "prev");
4967 } else {
4968 $paging_nav .= "first";
4969 $paging_nav .= " &sdot; prev";
4970 }
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004971 if ($#commitlist >= 100) {
Jakub Narebski8be68352006-09-11 00:36:04 +02004972 $paging_nav .= " &sdot; " .
4973 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4974 file_name=>$file_name, page=>$page+1),
4975 -accesskey => "n", -title => "Alt-n"}, "next");
4976 } else {
4977 $paging_nav .= " &sdot; next";
4978 }
4979 my $next_link = '';
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004980 if ($#commitlist >= 100) {
Jakub Narebski8be68352006-09-11 00:36:04 +02004981 $next_link =
4982 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4983 file_name=>$file_name, page=>$page+1),
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004984 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski8be68352006-09-11 00:36:04 +02004985 }
Jakub Narebski581860e2006-08-14 02:09:08 +02004986
Jakub Narebski8be68352006-09-11 00:36:04 +02004987 git_header_html();
4988 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4989 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4990 git_print_page_path($file_name, $ftype, $hash_base);
4991
Robert Fitzsimonsa8b983b2006-12-24 14:31:48 +00004992 git_history_body(\@commitlist, 0, 99,
Jakub Narebski8be68352006-09-11 00:36:04 +02004993 $refs, $hash_base, $ftype, $next_link);
4994
Kay Sieversd51e9022005-08-07 20:16:07 +02004995 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02004996}
Kay Sievers19806692005-08-07 20:26:27 +02004997
4998sub git_search {
Robert Fitzsimons6be93512006-12-23 03:35:16 +00004999 my ($have_search) = gitweb_check_feature('search');
5000 if (!$have_search) {
5001 die_error('403 Permission denied', "Permission denied");
5002 }
Kay Sievers19806692005-08-07 20:26:27 +02005003 if (!defined $searchtext) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02005004 die_error(undef, "Text field empty");
Kay Sievers19806692005-08-07 20:26:27 +02005005 }
5006 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005007 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005008 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005009 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02005010 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02005011 die_error(undef, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02005012 }
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005013 if (!defined $page) {
5014 $page = 0;
5015 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02005016
Petr Baudis88ad7292006-10-24 05:15:46 +02005017 $searchtype ||= 'commit';
5018 if ($searchtype eq 'pickaxe') {
Jakub Narebski04f7a942006-09-11 00:29:27 +02005019 # pickaxe may take all resources of your box and run for several minutes
5020 # with every query - so decide by yourself how public you make this feature
5021 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5022 if (!$have_pickaxe) {
5023 die_error('403 Permission denied', "Permission denied");
5024 }
Kay Sieversc994d622005-08-07 20:27:18 +02005025 }
Petr Baudise7738552007-05-17 04:31:12 +02005026 if ($searchtype eq 'grep') {
5027 my ($have_grep) = gitweb_check_feature('grep');
5028 if (!$have_grep) {
5029 die_error('403 Permission denied', "Permission denied");
5030 }
5031 }
Petr Baudis88ad7292006-10-24 05:15:46 +02005032
Kay Sievers19806692005-08-07 20:26:27 +02005033 git_header_html();
Kay Sievers19806692005-08-07 20:26:27 +02005034
Petr Baudis88ad7292006-10-24 05:15:46 +02005035 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
Robert Fitzsimons8e574fb2006-12-23 03:35:14 +00005036 my $greptype;
5037 if ($searchtype eq 'commit') {
5038 $greptype = "--grep=";
5039 } elsif ($searchtype eq 'author') {
5040 $greptype = "--author=";
5041 } elsif ($searchtype eq 'committer') {
5042 $greptype = "--committer=";
5043 }
Jakub Narebski7e431ef2007-05-16 01:56:10 +02005044 $greptype .= $search_regexp;
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005045 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005046
5047 my $paging_nav = '';
5048 if ($page > 0) {
5049 $paging_nav .=
5050 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005051 searchtext=>$searchtext, searchtype=>$searchtype)},
5052 "first");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005053 $paging_nav .= " &sdot; " .
5054 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005055 searchtext=>$searchtext, searchtype=>$searchtype,
5056 page=>$page-1),
5057 -accesskey => "p", -title => "Alt-p"}, "prev");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005058 } else {
5059 $paging_nav .= "first";
5060 $paging_nav .= " &sdot; prev";
5061 }
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005062 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005063 $paging_nav .= " &sdot; " .
5064 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005065 searchtext=>$searchtext, searchtype=>$searchtype,
5066 page=>$page+1),
5067 -accesskey => "n", -title => "Alt-n"}, "next");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005068 } else {
5069 $paging_nav .= " &sdot; next";
5070 }
5071 my $next_link = '';
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005072 if ($#commitlist >= 100) {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005073 $next_link =
5074 $cgi->a({-href => href(action=>"search", hash=>$hash,
Jakub Narebskia23f0a72007-04-01 22:21:38 +02005075 searchtext=>$searchtext, searchtype=>$searchtype,
5076 page=>$page+1),
5077 -accesskey => "n", -title => "Alt-n"}, "next");
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005078 }
5079
5080 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5081 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Robert Fitzsimons5ad66082006-12-24 14:31:46 +00005082 git_search_grep_body(\@commitlist, 0, 99, $next_link);
Kay Sieversc994d622005-08-07 20:27:18 +02005083 }
5084
Petr Baudis88ad7292006-10-24 05:15:46 +02005085 if ($searchtype eq 'pickaxe') {
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005086 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5087 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5088
5089 print "<table cellspacing=\"0\">\n";
5090 my $alternate = 1;
Kay Sieversc994d622005-08-07 20:27:18 +02005091 $/ = "\n";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005092 my $git_command = git_cmd_str();
Petr Baudis4229aa52007-05-17 04:30:42 +02005093 my $searchqtext = $searchtext;
5094 $searchqtext =~ s/'/'\\''/;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005095 open my $fd, "-|", "$git_command rev-list $hash | " .
Petr Baudis4229aa52007-05-17 04:30:42 +02005096 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
Kay Sieversc994d622005-08-07 20:27:18 +02005097 undef %co;
5098 my @files;
5099 while (my $line = <$fd>) {
5100 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5101 my %set;
5102 $set{'file'} = $6;
5103 $set{'from_id'} = $3;
5104 $set{'to_id'} = $4;
5105 $set{'id'} = $set{'to_id'};
5106 if ($set{'id'} =~ m/0{40}/) {
5107 $set{'id'} = $set{'from_id'};
5108 }
5109 if ($set{'id'} =~ m/0{40}/) {
5110 next;
5111 }
5112 push @files, \%set;
Kay Sievers53b89d82005-08-12 22:12:58 +02005113 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
Kay Sieversc994d622005-08-07 20:27:18 +02005114 if (%co) {
5115 if ($alternate) {
5116 print "<tr class=\"dark\">\n";
5117 } else {
5118 print "<tr class=\"light\">\n";
5119 }
5120 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02005121 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01005122 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02005123 "<td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005124 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5125 -class => "list subject"},
Jakub Narebski63e42202006-08-22 12:38:59 +02005126 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02005127 while (my $setref = shift @files) {
5128 my %set = %$setref;
Jakub Narebski952c65f2006-08-22 16:52:50 +02005129 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5130 hash=>$set{'id'}, file_name=>$set{'file'}),
5131 -class => "list"},
Jakub Narebski403d0902006-11-08 11:48:56 +01005132 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
Kay Sieversc994d622005-08-07 20:27:18 +02005133 "<br/>\n";
5134 }
5135 print "</td>\n" .
5136 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02005137 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02005138 " | " .
5139 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02005140 print "</td>\n" .
5141 "</tr>\n";
5142 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005143 %co = parse_commit($1);
Kay Sieversc994d622005-08-07 20:27:18 +02005144 }
5145 }
5146 close $fd;
Robert Fitzsimons8dbc0fc2006-12-23 14:57:12 +00005147
5148 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02005149 }
Petr Baudise7738552007-05-17 04:31:12 +02005150
5151 if ($searchtype eq 'grep') {
5152 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5153 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5154
5155 print "<table cellspacing=\"0\">\n";
5156 my $alternate = 1;
5157 my $matches = 0;
5158 $/ = "\n";
5159 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5160 my $lastfile = '';
5161 while (my $line = <$fd>) {
5162 chomp $line;
5163 my ($file, $lno, $ltext, $binary);
5164 last if ($matches++ > 1000);
5165 if ($line =~ /^Binary file (.+) matches$/) {
5166 $file = $1;
5167 $binary = 1;
5168 } else {
5169 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5170 }
5171 if ($file ne $lastfile) {
5172 $lastfile and print "</td></tr>\n";
5173 if ($alternate++) {
5174 print "<tr class=\"dark\">\n";
5175 } else {
5176 print "<tr class=\"light\">\n";
5177 }
5178 print "<td class=\"list\">".
5179 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5180 file_name=>"$file"),
5181 -class => "list"}, esc_path($file));
5182 print "</td><td>\n";
5183 $lastfile = $file;
5184 }
5185 if ($binary) {
5186 print "<div class=\"binary\">Binary file</div>\n";
5187 } else {
5188 $ltext = untabify($ltext);
5189 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5190 $ltext = esc_html($1, -nbsp=>1);
5191 $ltext .= '<span class="match">';
5192 $ltext .= esc_html($2, -nbsp=>1);
5193 $ltext .= '</span>';
5194 $ltext .= esc_html($3, -nbsp=>1);
5195 } else {
5196 $ltext = esc_html($ltext, -nbsp=>1);
5197 }
5198 print "<div class=\"pre\">" .
5199 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5200 file_name=>"$file").'#l'.$lno,
5201 -class => "linenr"}, sprintf('%4i', $lno))
5202 . ' ' . $ltext . "</div>\n";
5203 }
5204 }
5205 if ($lastfile) {
5206 print "</td></tr>\n";
5207 if ($matches > 1000) {
5208 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5209 }
5210 } else {
5211 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5212 }
5213 close $fd;
5214
5215 print "</table>\n";
5216 }
Kay Sievers19806692005-08-07 20:26:27 +02005217 git_footer_html();
5218}
5219
Petr Baudis88ad7292006-10-24 05:15:46 +02005220sub git_search_help {
5221 git_header_html();
5222 git_print_page_nav('','', $hash,$hash,$hash);
5223 print <<EOT;
5224<dl>
5225<dt><b>commit</b></dt>
5226<dd>The commit messages and authorship information will be scanned for the given string.</dd>
Petr Baudise7738552007-05-17 04:31:12 +02005227EOT
5228 my ($have_grep) = gitweb_check_feature('grep');
5229 if ($have_grep) {
5230 print <<EOT;
5231<dt><b>grep</b></dt>
5232<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5233 a different one) are searched for the given
5234<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5235(POSIX extended) and the matches are listed. On large
5236trees, this search can take a while and put some strain on the server, so please use it with
5237some consideration.</dd>
5238EOT
5239 }
5240 print <<EOT;
Petr Baudis88ad7292006-10-24 05:15:46 +02005241<dt><b>author</b></dt>
5242<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5243<dt><b>committer</b></dt>
5244<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5245EOT
5246 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5247 if ($have_pickaxe) {
5248 print <<EOT;
5249<dt><b>pickaxe</b></dt>
5250<dd>All commits that caused the string to appear or disappear from any file (changes that
5251added, removed or "modified" the string) will be listed. This search can take a while and
5252takes a lot of strain on the server, so please use it wisely.</dd>
5253EOT
5254 }
5255 print "</dl>\n";
5256 git_footer_html();
5257}
5258
Kay Sievers19806692005-08-07 20:26:27 +02005259sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005260 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02005261 if (!defined $hash) {
5262 $hash = $head;
5263 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02005264 if (!defined $page) {
5265 $page = 0;
5266 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02005267 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02005268
Robert Fitzsimonsd2229842007-01-03 20:25:55 +00005269 my @commitlist = parse_commits($hash, 101, (100 * $page));
Kay Sieversea4a6df2005-08-07 20:26:49 +02005270
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005271 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005272 my $next_link = '';
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005273 if ($#commitlist >= 100) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005274 $next_link =
Martin Waitz756d2f02006-08-17 00:28:36 +02005275 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005276 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005277 }
5278
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005279 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02005280 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5281 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02005282
Robert Fitzsimons190d7fd2006-12-24 14:31:44 +00005283 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02005284
Kay Sievers19806692005-08-07 20:26:27 +02005285 git_footer_html();
5286}
Jakub Narebski717b8312006-07-31 21:22:15 +02005287
5288## ......................................................................
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005289## feeds (RSS, Atom; OPML)
Jakub Narebski717b8312006-07-31 21:22:15 +02005290
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005291sub git_feed {
5292 my $format = shift || 'atom';
5293 my ($have_blame) = gitweb_check_feature('blame');
5294
5295 # Atom: http://www.atomenabled.org/developers/syndication/
5296 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5297 if ($format ne 'rss' && $format ne 'atom') {
5298 die_error(undef, "Unknown web feed format");
5299 }
5300
5301 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5302 my $head = $hash || 'HEAD';
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005303 my @commitlist = parse_commits($head, 150);
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005304
5305 my %latest_commit;
5306 my %latest_date;
5307 my $content_type = "application/$format+xml";
5308 if (defined $cgi->http('HTTP_ACCEPT') &&
5309 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5310 # browser (feed reader) prefers text/xml
5311 $content_type = 'text/xml';
5312 }
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005313 if (defined($commitlist[0])) {
5314 %latest_commit = %{$commitlist[0]};
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005315 %latest_date = parse_date($latest_commit{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005316 print $cgi->header(
5317 -type => $content_type,
5318 -charset => 'utf-8',
5319 -last_modified => $latest_date{'rfc2822'});
5320 } else {
5321 print $cgi->header(
5322 -type => $content_type,
5323 -charset => 'utf-8');
5324 }
5325
5326 # Optimization: skip generating the body if client asks only
5327 # for Last-Modified date.
5328 return if ($cgi->request_method() eq 'HEAD');
5329
5330 # header variables
5331 my $title = "$site_name - $project/$action";
5332 my $feed_type = 'log';
5333 if (defined $hash) {
5334 $title .= " - '$hash'";
5335 $feed_type = 'branch log';
5336 if (defined $file_name) {
5337 $title .= " :: $file_name";
5338 $feed_type = 'history';
5339 }
5340 } elsif (defined $file_name) {
5341 $title .= " - $file_name";
5342 $feed_type = 'history';
5343 }
5344 $title .= " $feed_type";
5345 my $descr = git_get_project_description($project);
5346 if (defined $descr) {
5347 $descr = esc_html($descr);
5348 } else {
5349 $descr = "$project " .
5350 ($format eq 'rss' ? 'RSS' : 'Atom') .
5351 " feed";
5352 }
5353 my $owner = git_get_project_owner($project);
5354 $owner = esc_html($owner);
5355
5356 #header
5357 my $alt_url;
5358 if (defined $file_name) {
5359 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5360 } elsif (defined $hash) {
5361 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5362 } else {
5363 $alt_url = href(-full=>1, action=>"summary");
5364 }
5365 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5366 if ($format eq 'rss') {
5367 print <<XML;
Jakub Narebski59b9f612006-08-22 23:42:53 +02005368<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5369<channel>
Jakub Narebski59b9f612006-08-22 23:42:53 +02005370XML
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005371 print "<title>$title</title>\n" .
5372 "<link>$alt_url</link>\n" .
5373 "<description>$descr</description>\n" .
5374 "<language>en</language>\n";
5375 } elsif ($format eq 'atom') {
5376 print <<XML;
5377<feed xmlns="http://www.w3.org/2005/Atom">
5378XML
5379 print "<title>$title</title>\n" .
5380 "<subtitle>$descr</subtitle>\n" .
5381 '<link rel="alternate" type="text/html" href="' .
5382 $alt_url . '" />' . "\n" .
5383 '<link rel="self" type="' . $content_type . '" href="' .
5384 $cgi->self_url() . '" />' . "\n" .
5385 "<id>" . href(-full=>1) . "</id>\n" .
5386 # use project owner for feed author
5387 "<author><name>$owner</name></author>\n";
5388 if (defined $favicon) {
5389 print "<icon>" . esc_url($favicon) . "</icon>\n";
5390 }
5391 if (defined $logo_url) {
5392 # not twice as wide as tall: 72 x 27 pixels
Jakub Narebskie1147262006-12-04 14:09:43 +01005393 print "<logo>" . esc_url($logo) . "</logo>\n";
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005394 }
5395 if (! %latest_date) {
5396 # dummy date to keep the feed valid until commits trickle in:
5397 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5398 } else {
5399 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5400 }
5401 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005402
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005403 # contents
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005404 for (my $i = 0; $i <= $#commitlist; $i++) {
5405 my %co = %{$commitlist[$i]};
5406 my $commit = $co{'id'};
Jakub Narebski717b8312006-07-31 21:22:15 +02005407 # we read 150, we always show 30 and the ones more recent than 48 hours
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005408 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
Jakub Narebski717b8312006-07-31 21:22:15 +02005409 last;
5410 }
Jakub Narebski91fd2bf2006-11-25 15:54:34 +01005411 my %cd = parse_date($co{'author_epoch'});
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005412
5413 # get list of changed files
Robert Fitzsimonsb6093a52006-12-24 14:31:47 +00005414 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskic906b182007-05-19 02:47:51 +02005415 $co{'parent'} || "--root",
5416 $co{'id'}, "--", (defined $file_name ? $file_name : ())
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02005417 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02005418 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02005419 close $fd
5420 or next;
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005421
5422 # print element (entry, item)
5423 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5424 if ($format eq 'rss') {
5425 print "<item>\n" .
5426 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5427 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5428 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5429 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5430 "<link>$co_url</link>\n" .
5431 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5432 "<content:encoded>" .
5433 "<![CDATA[\n";
5434 } elsif ($format eq 'atom') {
5435 print "<entry>\n" .
5436 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5437 "<updated>$cd{'iso-8601'}</updated>\n" .
Jakub Narebskiab23c192006-11-25 15:54:33 +01005438 "<author>\n" .
5439 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5440 if ($co{'author_email'}) {
5441 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5442 }
5443 print "</author>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005444 # use committer for contributor
Jakub Narebskiab23c192006-11-25 15:54:33 +01005445 "<contributor>\n" .
5446 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5447 if ($co{'committer_email'}) {
5448 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5449 }
5450 print "</contributor>\n" .
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005451 "<published>$cd{'iso-8601'}</published>\n" .
5452 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5453 "<id>$co_url</id>\n" .
5454 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5455 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5456 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005457 my $comment = $co{'comment'};
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005458 print "<pre>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005459 foreach my $line (@$comment) {
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005460 $line = esc_html($line);
5461 print "$line\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005462 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005463 print "</pre><ul>\n";
5464 foreach my $difftree_line (@difftree) {
5465 my %difftree = parse_difftree_raw_line($difftree_line);
5466 next if !$difftree{'from_id'};
5467
5468 my $file = $difftree{'file'} || $difftree{'to_file'};
5469
5470 print "<li>" .
5471 "[" .
5472 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5473 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5474 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5475 file_name=>$file, file_parent=>$difftree{'from_file'}),
5476 -title => "diff"}, 'D');
5477 if ($have_blame) {
5478 print $cgi->a({-href => href(-full=>1, action=>"blame",
5479 file_name=>$file, hash_base=>$commit),
5480 -title => "blame"}, 'B');
Jakub Narebski717b8312006-07-31 21:22:15 +02005481 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005482 # if this is not a feed of a file history
5483 if (!defined $file_name || $file_name ne $file) {
5484 print $cgi->a({-href => href(-full=>1, action=>"history",
5485 file_name=>$file, hash=>$commit),
5486 -title => "history"}, 'H');
5487 }
5488 $file = esc_path($file);
5489 print "] ".
5490 "$file</li>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02005491 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005492 if ($format eq 'rss') {
5493 print "</ul>]]>\n" .
5494 "</content:encoded>\n" .
5495 "</item>\n";
5496 } elsif ($format eq 'atom') {
5497 print "</ul>\n</div>\n" .
5498 "</content>\n" .
5499 "</entry>\n";
5500 }
Jakub Narebski717b8312006-07-31 21:22:15 +02005501 }
Jakub Narebskiaf6feeb2006-11-19 15:05:22 +01005502
5503 # end of feed
5504 if ($format eq 'rss') {
5505 print "</channel>\n</rss>\n";
5506 } elsif ($format eq 'atom') {
5507 print "</feed>\n";
5508 }
5509}
5510
5511sub git_rss {
5512 git_feed('rss');
5513}
5514
5515sub git_atom {
5516 git_feed('atom');
Jakub Narebski717b8312006-07-31 21:22:15 +02005517}
5518
5519sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02005520 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02005521
5522 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02005523 print <<XML;
5524<?xml version="1.0" encoding="utf-8"?>
5525<opml version="1.0">
5526<head>
Petr Baudis8be28902006-10-24 05:18:39 +02005527 <title>$site_name OPML Export</title>
Jakub Narebski59b9f612006-08-22 23:42:53 +02005528</head>
5529<body>
5530<outline text="git RSS feeds">
5531XML
Jakub Narebski717b8312006-07-31 21:22:15 +02005532
5533 foreach my $pr (@list) {
5534 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02005535 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02005536 if (!defined $head) {
5537 next;
5538 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02005539 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02005540 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02005541 if (!%co) {
5542 next;
5543 }
5544
5545 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5546 my $rss = "$my_url?p=$proj{'path'};a=rss";
5547 my $html = "$my_url?p=$proj{'path'};a=summary";
5548 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5549 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02005550 print <<XML;
5551</outline>
5552</body>
5553</opml>
5554XML
Jakub Narebski717b8312006-07-31 21:22:15 +02005555}