blob: 5f597f71e05376268d55742f288888d30b93c240 [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
Dennis Stosberg4a87b432006-06-21 15:07:08 +020021our $cgi = new CGI;
Junio C Hamano06c084d2006-08-02 13:50:20 -070022our $version = "++GIT_VERSION++";
Dennis Stosberg4a87b432006-06-21 15:07:08 +020023our $my_url = $cgi->url();
24our $my_uri = $cgi->url(-absolute => 1);
Kay Sievers44ad2972005-08-07 19:59:24 +020025
Alp Tokere130dda2006-07-12 23:55:10 +010026# core git executable to use
27# this can just be "git" if your webserver has a sensible PATH
Junio C Hamano06c084d2006-08-02 13:50:20 -070028our $GIT = "++GIT_BINDIR++/git";
Jakub Narebski3f7f27102006-06-21 09:48:04 +020029
Kay Sieversb87d78d2005-08-07 20:21:04 +020030# absolute fs-path which will be prepended to the project path
Dennis Stosberg4a87b432006-06-21 15:07:08 +020031#our $projectroot = "/pub/scm";
Junio C Hamano06c084d2006-08-02 13:50:20 -070032our $projectroot = "++GITWEB_PROJECTROOT++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020033
Kay Sieversb87d78d2005-08-07 20:21:04 +020034# target of the home link on top of all pages
Martin Waitz6132b7e2006-08-17 00:28:39 +020035our $home_link = $my_uri || "/";
Kay Sieversb87d78d2005-08-07 20:21:04 +020036
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +090037# string of the home link on top of all pages
38our $home_link_str = "++GITWEB_HOME_LINK_STR++";
39
Alp Toker49da1da2006-07-11 21:10:26 +010040# name of your site or organization to appear in page titles
41# replace this with something more descriptive for clearer bookmarks
Junio C Hamano06c084d2006-08-02 13:50:20 -070042our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
Alp Toker49da1da2006-07-11 21:10:26 +010043
Kay Sievers8ab1da22005-08-07 20:22:53 +020044# html text to include at home page
Junio C Hamano06c084d2006-08-02 13:50:20 -070045our $home_text = "++GITWEB_HOMETEXT++";
Kay Sievers8ab1da22005-08-07 20:22:53 +020046
Jakub Narebskiaedd9422006-06-17 11:23:56 +020047# URI of default stylesheet
Junio C Hamano06c084d2006-08-02 13:50:20 -070048our $stylesheet = "++GITWEB_CSS++";
Martin Waitz281f2f62006-07-31 00:38:39 +020049# URI of GIT logo
Junio C Hamano06c084d2006-08-02 13:50:20 -070050our $logo = "++GITWEB_LOGO++";
Jakub Narebski0b5deba2006-09-04 20:32:13 +020051# URI of GIT favicon, assumed to be image/png type
52our $favicon = "++GITWEB_FAVICON++";
Jakub Narebskiaedd9422006-06-17 11:23:56 +020053
Kay Sievers09bd7892005-08-07 20:21:23 +020054# source of projects list
Junio C Hamano06c084d2006-08-02 13:50:20 -070055our $projects_list = "++GITWEB_LIST++";
Kay Sieversb87d78d2005-08-07 20:21:04 +020056
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +020057# show repository only if this file exists
58# (only effective if this variable evaluates to true)
59our $export_ok = "++GITWEB_EXPORT_OK++";
60
61# only allow viewing of repositories also shown on the overview page
62our $strict_export = "++GITWEB_STRICT_EXPORT++";
63
Jakub Narebski19a87212006-08-15 23:03:17 +020064# list of git base URLs used for URL to where fetch project from,
65# i.e. full URL is "$git_base_url/$project"
66our @git_base_url_list = ("++GITWEB_BASE_URL++");
67
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020068# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +020069our $default_blob_plain_mimetype = 'text/plain';
70our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020071
Petr Baudis2d007372006-06-18 00:01:06 +020072# file to use for guessing MIME types before trying /etc/mime.types
73# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +020074our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +020075
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053076# You define site-wide feature defaults here; override them with
77# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +020078our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +020079 # feature => {
80 # 'sub' => feature-sub (subroutine),
81 # 'override' => allow-override (boolean),
82 # 'default' => [ default options...] (array reference)}
83 #
84 # if feature is overridable (it means that allow-override has true value,
85 # then feature-sub will be called with default options as parameters;
86 # return value of feature-sub indicates if to enable specified feature
87 #
88 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053089
Jakub Narebski952c65f2006-08-22 16:52:50 +020090 'blame' => {
91 'sub' => \&feature_blame,
92 'override' => 0,
93 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053094
Jakub Narebski952c65f2006-08-22 16:52:50 +020095 'snapshot' => {
96 'sub' => \&feature_snapshot,
97 'override' => 0,
98 # => [content-encoding, suffix, program]
99 'default' => ['x-gzip', 'gz', 'gzip']},
Jakub Narebski04f7a942006-09-11 00:29:27 +0200100
101 'pickaxe' => {
102 'sub' => \&feature_pickaxe,
103 'override' => 0,
104 'default' => [1]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530105);
106
107sub gitweb_check_feature {
108 my ($name) = @_;
109 return undef unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200110 my ($sub, $override, @defaults) = (
111 $feature{$name}{'sub'},
112 $feature{$name}{'override'},
113 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530114 if (!$override) { return @defaults; }
115 return $sub->(@defaults);
116}
117
118# To enable system wide have in $GITWEB_CONFIG
Jakub Narebski17848fc2006-08-26 19:14:22 +0200119# $feature{'blame'}{'default'} = [1];
120# To have project specific config enable override in $GITWEB_CONFIG
121# $feature{'blame'}{'override'} = 1;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530122# and in project config gitweb.blame = 0|1;
123
124sub feature_blame {
125 my ($val) = git_get_project_config('blame', '--bool');
126
127 if ($val eq 'true') {
128 return 1;
129 } elsif ($val eq 'false') {
130 return 0;
131 }
132
133 return $_[0];
134}
135
136# To disable system wide have in $GITWEB_CONFIG
Jakub Narebski17848fc2006-08-26 19:14:22 +0200137# $feature{'snapshot'}{'default'} = [undef];
138# To have project specific config enable override in $GITWEB_CONFIG
139# $feature{'blame'}{'override'} = 1;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530140# and in project config gitweb.snapshot = none|gzip|bzip2
141
142sub feature_snapshot {
143 my ($ctype, $suffix, $command) = @_;
144
145 my ($val) = git_get_project_config('snapshot');
146
147 if ($val eq 'gzip') {
148 return ('x-gzip', 'gz', 'gzip');
149 } elsif ($val eq 'bzip2') {
150 return ('x-bzip2', 'bz2', 'bzip2');
151 } elsif ($val eq 'none') {
152 return ();
153 }
154
155 return ($ctype, $suffix, $command);
156}
157
Jakub Narebski04f7a942006-09-11 00:29:27 +0200158# To enable system wide have in $GITWEB_CONFIG
159# $feature{'pickaxe'}{'default'} = [1];
160# To have project specific config enable override in $GITWEB_CONFIG
161# $feature{'pickaxe'}{'override'} = 1;
162# and in project config gitweb.pickaxe = 0|1;
163
164sub feature_pickaxe {
165 my ($val) = git_get_project_config('pickaxe', '--bool');
166
167 if ($val eq 'true') {
168 return (1);
169 } elsif ($val eq 'false') {
170 return (0);
171 }
172
173 return ($_[0]);
174}
175
Jakub Narebski6bcf4b42006-08-27 23:49:36 +0200176# rename detection options for git-diff and git-diff-tree
177# - default is '-M', with the cost proportional to
178# (number of removed files) * (number of new files).
179# - more costly is '-C' (or '-C', '-M'), with the cost proportional to
180# (number of changed files + number of removed files) * (number of new files)
181# - even more costly is '-C', '--find-copies-harder' with cost
182# (number of files in the original tree) * (number of new files)
183# - one might want to include '-B' option, e.g. '-B', '-M'
184our @diff_opts = ('-M'); # taken from git_commit
185
Junio C Hamano06c084d2006-08-02 13:50:20 -0700186our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Dennis Stosberg4b5dc982006-08-29 09:19:02 +0200187do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400188
189# version of the core git binary
190our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
191
192$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400193
Jakub Narebski154b4d72006-08-05 12:55:20 +0200194# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200195# input validation and dispatch
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200196our $action = $cgi->param('a');
Kay Sievers09bd7892005-08-07 20:21:23 +0200197if (defined $action) {
Kay Sieversc91da262005-09-03 14:50:33 +0200198 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200199 die_error(undef, "Invalid action parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200200 }
Kay Sieversb87d78d2005-08-07 20:21:04 +0200201}
202
Martin Waitzdd702352006-09-16 23:08:32 +0200203our $project = $cgi->param('p');
Martin Waitz13d02162006-08-17 00:28:40 +0200204if (defined $project) {
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200205 if (!validate_input($project) ||
206 !(-d "$projectroot/$project") ||
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200207 !(-e "$projectroot/$project/HEAD") ||
208 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
209 ($strict_export && !project_in_list($project))) {
Matthias Lederhofer7939fe42006-09-17 00:30:27 +0200210 undef $project;
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200211 die_error(undef, "No such project");
Kay Sievers9cd3d982005-08-07 20:17:42 +0200212 }
Kay Sievers2ad93312005-08-07 20:14:48 +0200213}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200214
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200215our $file_name = $cgi->param('f');
Kay Sieversb87d78d2005-08-07 20:21:04 +0200216if (defined $file_name) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200217 if (!validate_input($file_name)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200218 die_error(undef, "Invalid file parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200219 }
Kay Sieversd51e9022005-08-07 20:16:07 +0200220}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200221
Martin Waitz5c95fab2006-08-17 00:28:38 +0200222our $file_parent = $cgi->param('fp');
223if (defined $file_parent) {
224 if (!validate_input($file_parent)) {
225 die_error(undef, "Invalid file parent parameter");
226 }
227}
228
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200229our $hash = $cgi->param('h');
Kay Sievers4fac5292005-08-07 20:27:38 +0200230if (defined $hash) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200231 if (!validate_input($hash)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200232 die_error(undef, "Invalid hash parameter");
Kay Sievers4fac5292005-08-07 20:27:38 +0200233 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200234}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200235
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200236our $hash_parent = $cgi->param('hp');
Kay Sieversc91da262005-09-03 14:50:33 +0200237if (defined $hash_parent) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200238 if (!validate_input($hash_parent)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200239 die_error(undef, "Invalid hash parent parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200240 }
Kay Sievers09bd7892005-08-07 20:21:23 +0200241}
242
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200243our $hash_base = $cgi->param('hb');
Kay Sieversc91da262005-09-03 14:50:33 +0200244if (defined $hash_base) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200245 if (!validate_input($hash_base)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200246 die_error(undef, "Invalid hash base parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200247 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200248}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200249
Jakub Narebski420e92f2006-08-24 23:53:54 +0200250our $hash_parent_base = $cgi->param('hpb');
251if (defined $hash_parent_base) {
252 if (!validate_input($hash_parent_base)) {
253 die_error(undef, "Invalid hash parent base parameter");
254 }
255}
256
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200257our $page = $cgi->param('pg');
Kay Sieversea4a6df2005-08-07 20:26:49 +0200258if (defined $page) {
Matthias Lederhoferac8e3f22006-09-17 13:52:45 +0200259 if ($page =~ m/[^0-9]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200260 die_error(undef, "Invalid page parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200261 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200262}
Kay Sievers823d5dc2005-08-07 19:57:58 +0200263
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200264our $searchtext = $cgi->param('s');
Kay Sievers19806692005-08-07 20:26:27 +0200265if (defined $searchtext) {
266 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200267 die_error(undef, "Invalid search parameter");
Kay Sievers19806692005-08-07 20:26:27 +0200268 }
269 $searchtext = quotemeta $searchtext;
270}
271
Martin Waitzdd702352006-09-16 23:08:32 +0200272# now read PATH_INFO and use it as alternative to parameters
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200273sub evaluate_path_info {
274 return if defined $project;
275 my $path_info = $ENV{"PATH_INFO"};
276 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200277 $path_info =~ s,^/+,,;
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200278 return if !$path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200279 # find which part of PATH_INFO is project
Martin Waitzdd702352006-09-16 23:08:32 +0200280 $project = $path_info;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200281 $project =~ s,/+$,,;
Martin Waitzdd702352006-09-16 23:08:32 +0200282 while ($project && !-e "$projectroot/$project/HEAD") {
283 $project =~ s,/*[^/]*$,,;
284 }
Jakub Narebskicd90e752006-09-20 00:49:51 +0200285 # validate project
286 $project = validate_input($project);
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200287 if (!$project ||
288 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
289 ($strict_export && !project_in_list($project))) {
290 undef $project;
291 return;
Martin Waitzdd702352006-09-16 23:08:32 +0200292 }
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200293 # do not change any parameters if an action is given using the query string
294 return if $action;
Jakub Narebskicd90e752006-09-20 00:49:51 +0200295 $path_info =~ s,^$project/*,,;
296 my ($refname, $pathname) = split(/:/, $path_info, 2);
297 if (defined $pathname) {
298 # we got "project.git/branch:filename" or "project.git/branch:dir/"
299 # we could use git_get_type(branch:pathname), but it needs $git_dir
300 $pathname =~ s,^/+,,;
301 if (!$pathname || substr($pathname, -1) eq "/") {
302 $action ||= "tree";
303 } else {
304 $action ||= "blob_plain";
305 }
306 $hash_base ||= validate_input($refname);
307 $file_name ||= validate_input($pathname);
308 } elsif (defined $refname) {
Martin Waitzdd702352006-09-16 23:08:32 +0200309 # we got "project.git/branch"
310 $action ||= "shortlog";
Jakub Narebskicd90e752006-09-20 00:49:51 +0200311 $hash ||= validate_input($refname);
Martin Waitzdd702352006-09-16 23:08:32 +0200312 }
313}
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200314evaluate_path_info();
Martin Waitzdd702352006-09-16 23:08:32 +0200315
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200316# path to the current git repository
317our $git_dir;
318$git_dir = "$projectroot/$project" if $project;
Martin Waitzdd702352006-09-16 23:08:32 +0200319
Jakub Narebski717b8312006-07-31 21:22:15 +0200320# dispatch
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200321my %actions = (
322 "blame" => \&git_blame2,
323 "blobdiff" => \&git_blobdiff,
324 "blobdiff_plain" => \&git_blobdiff_plain,
325 "blob" => \&git_blob,
326 "blob_plain" => \&git_blob_plain,
327 "commitdiff" => \&git_commitdiff,
328 "commitdiff_plain" => \&git_commitdiff_plain,
329 "commit" => \&git_commit,
330 "heads" => \&git_heads,
331 "history" => \&git_history,
332 "log" => \&git_log,
333 "rss" => \&git_rss,
334 "search" => \&git_search,
335 "shortlog" => \&git_shortlog,
336 "summary" => \&git_summary,
337 "tag" => \&git_tag,
338 "tags" => \&git_tags,
339 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530340 "snapshot" => \&git_snapshot,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200341 # those below don't need $project
342 "opml" => \&git_opml,
343 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200344 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200345);
346
Jakub Narebski77a153f2006-08-22 16:59:20 +0200347if (defined $project) {
348 $action ||= 'summary';
349} else {
350 $action ||= 'project_list';
351}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200352if (!defined($actions{$action})) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200353 die_error(undef, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200354}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200355$actions{$action}->();
356exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200357
Jakub Narebski717b8312006-07-31 21:22:15 +0200358## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200359## action links
360
361sub href(%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200362 my %params = @_;
363
364 my @mapping = (
Martin Waitz06a9d862006-08-16 00:23:50 +0200365 project => "p",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200366 action => "a",
Martin Waitz06a9d862006-08-16 00:23:50 +0200367 file_name => "f",
Martin Waitz5c95fab2006-08-17 00:28:38 +0200368 file_parent => "fp",
Martin Waitz06a9d862006-08-16 00:23:50 +0200369 hash => "h",
370 hash_parent => "hp",
371 hash_base => "hb",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200372 hash_parent_base => "hpb",
Martin Waitz06a9d862006-08-16 00:23:50 +0200373 page => "pg",
Jakub Narebskia1565c42006-09-15 19:30:34 +0200374 order => "o",
Martin Waitz06a9d862006-08-16 00:23:50 +0200375 searchtext => "s",
376 );
Jakub Narebski498fe002006-08-22 19:05:25 +0200377 my %mapping = @mapping;
Martin Waitz06a9d862006-08-16 00:23:50 +0200378
Jakub Narebskia1565c42006-09-15 19:30:34 +0200379 $params{'project'} = $project unless exists $params{'project'};
Martin Waitz06a9d862006-08-16 00:23:50 +0200380
Jakub Narebski498fe002006-08-22 19:05:25 +0200381 my @result = ();
382 for (my $i = 0; $i < @mapping; $i += 2) {
383 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
384 if (defined $params{$name}) {
385 push @result, $symbol . "=" . esc_param($params{$name});
386 }
387 }
388 return "$my_uri?" . join(';', @result);
Martin Waitz06a9d862006-08-16 00:23:50 +0200389}
390
391
392## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200393## validation, quoting/unquoting and escaping
394
395sub validate_input {
396 my $input = shift;
397
398 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
399 return $input;
400 }
401 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
402 return undef;
403 }
404 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
405 return undef;
406 }
407 return $input;
408}
409
Kay Sievers232ff552005-11-24 16:56:55 +0100410# quote unsafe chars, but keep the slash, even when it's not
411# correct, but quoted slashes look too horrible in bookmarks
412sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100413 my $str = shift;
Kay Sievers232ff552005-11-24 16:56:55 +0100414 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +0100415 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +0100416 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +0100417 return $str;
418}
419
Kay Sievers232ff552005-11-24 16:56:55 +0100420# replace invalid utf8 character with SUBSTITUTION sequence
Kay Sievers40c13812005-11-19 17:41:29 +0100421sub esc_html {
422 my $str = shift;
Kay Sievers40c13812005-11-19 17:41:29 +0100423 $str = decode("utf8", $str, Encode::FB_DEFAULT);
Kay Sievers10bb9032005-11-23 04:26:40 +0100424 $str = escapeHTML($str);
Jakub Narebski3dc13832006-07-30 15:02:27 +0200425 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
Kay Sievers40c13812005-11-19 17:41:29 +0100426 return $str;
427}
428
Kay Sievers232ff552005-11-24 16:56:55 +0100429# git may return quoted and escaped filenames
430sub unquote {
431 my $str = shift;
432 if ($str =~ m/^"(.*)"$/) {
433 $str = $1;
434 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
435 }
436 return $str;
437}
438
Jakub Narebskif16db172006-08-06 02:08:31 +0200439# escape tabs (convert tabs to spaces)
440sub untabify {
441 my $line = shift;
442
443 while ((my $pos = index($line, "\t")) != -1) {
444 if (my $count = (8 - ($pos % 8))) {
445 my $spaces = ' ' x $count;
446 $line =~ s/\t/$spaces/;
447 }
448 }
449
450 return $line;
451}
452
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200453sub project_in_list {
454 my $project = shift;
455 my @list = git_get_projects_list();
456 return @list && scalar(grep { $_->{'path'} eq $project } @list);
457}
458
Jakub Narebski717b8312006-07-31 21:22:15 +0200459## ----------------------------------------------------------------------
460## HTML aware string manipulation
461
462sub chop_str {
463 my $str = shift;
464 my $len = shift;
465 my $add_len = shift || 10;
466
467 # allow only $len chars, but don't cut a word if it would fit in $add_len
468 # if it doesn't fit, cut it if it's still longer than the dots we would add
469 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
470 my $body = $1;
471 my $tail = $2;
472 if (length($tail) > 4) {
473 $tail = " ...";
474 $body =~ s/&[^;]*$//; # remove chopped character entities
475 }
476 return "$body$tail";
477}
478
479## ----------------------------------------------------------------------
480## functions returning short strings
481
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000482# CSS class for given age value (in seconds)
483sub age_class {
484 my $age = shift;
485
486 if ($age < 60*60*2) {
487 return "age0";
488 } elsif ($age < 60*60*24*2) {
489 return "age1";
490 } else {
491 return "age2";
492 }
493}
494
Jakub Narebski717b8312006-07-31 21:22:15 +0200495# convert age in seconds to "nn units ago" string
496sub age_string {
497 my $age = shift;
498 my $age_str;
499
500 if ($age > 60*60*24*365*2) {
501 $age_str = (int $age/60/60/24/365);
502 $age_str .= " years ago";
503 } elsif ($age > 60*60*24*(365/12)*2) {
504 $age_str = int $age/60/60/24/(365/12);
505 $age_str .= " months ago";
506 } elsif ($age > 60*60*24*7*2) {
507 $age_str = int $age/60/60/24/7;
508 $age_str .= " weeks ago";
509 } elsif ($age > 60*60*24*2) {
510 $age_str = int $age/60/60/24;
511 $age_str .= " days ago";
512 } elsif ($age > 60*60*2) {
513 $age_str = int $age/60/60;
514 $age_str .= " hours ago";
515 } elsif ($age > 60*2) {
516 $age_str = int $age/60;
517 $age_str .= " min ago";
518 } elsif ($age > 2) {
519 $age_str = int $age;
520 $age_str .= " sec ago";
521 } else {
522 $age_str .= " right now";
523 }
524 return $age_str;
525}
526
527# convert file mode in octal to symbolic file mode string
528sub mode_str {
529 my $mode = oct shift;
530
531 if (S_ISDIR($mode & S_IFMT)) {
532 return 'drwxr-xr-x';
533 } elsif (S_ISLNK($mode)) {
534 return 'lrwxrwxrwx';
535 } elsif (S_ISREG($mode)) {
536 # git cares only about the executable bit
537 if ($mode & S_IXUSR) {
538 return '-rwxr-xr-x';
539 } else {
540 return '-rw-r--r--';
541 };
542 } else {
543 return '----------';
544 }
545}
546
547# convert file mode in octal to file type string
548sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +0200549 my $mode = shift;
550
551 if ($mode !~ m/^[0-7]+$/) {
552 return $mode;
553 } else {
554 $mode = oct $mode;
555 }
Jakub Narebski717b8312006-07-31 21:22:15 +0200556
557 if (S_ISDIR($mode & S_IFMT)) {
558 return "directory";
559 } elsif (S_ISLNK($mode)) {
560 return "symlink";
561 } elsif (S_ISREG($mode)) {
562 return "file";
563 } else {
564 return "unknown";
565 }
566}
567
568## ----------------------------------------------------------------------
569## functions returning short HTML fragments, or transforming HTML fragments
570## which don't beling to other sections
571
572# format line of commit message or tag comment
573sub format_log_line_html {
574 my $line = shift;
575
576 $line = esc_html($line);
577 $line =~ s/ /&nbsp;/g;
578 if ($line =~ m/([0-9a-fA-F]{40})/) {
579 my $hash_text = $1;
580 if (git_get_type($hash_text) eq "commit") {
Jakub Narebski952c65f2006-08-22 16:52:50 +0200581 my $link =
582 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
583 -class => "text"}, $hash_text);
Jakub Narebski717b8312006-07-31 21:22:15 +0200584 $line =~ s/$hash_text/$link/;
585 }
586 }
587 return $line;
588}
589
590# format marker of refs pointing to given object
Jakub Narebski847e01f2006-08-14 02:05:47 +0200591sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +0200592 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200593 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +0200594
595 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200596 foreach my $ref (@{$refs->{$id}}) {
597 my ($type, $name) = qw();
598 # e.g. tags/v2.6.11 or heads/next
599 if ($ref =~ m!^(.*?)s?/(.*)$!) {
600 $type = $1;
601 $name = $2;
602 } else {
603 $type = "ref";
604 $name = $ref;
605 }
606
607 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
608 }
609 }
610
611 if ($markers) {
612 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +0200613 } else {
614 return "";
615 }
616}
617
Jakub Narebski17d07442006-08-14 02:08:27 +0200618# format, perhaps shortened and with markers, title line
619sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200620 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +0200621 $extra = '' unless defined($extra);
622
623 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +0200624 return $cgi->a({-href => $href, -class => "list subject",
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200625 -title => $long},
Jakub Narebski17d07442006-08-14 02:08:27 +0200626 esc_html($short) . $extra);
627 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +0200628 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +0200629 esc_html($long) . $extra);
630 }
631}
632
Jakub Narebskieee08902006-08-24 00:15:14 +0200633sub format_diff_line {
634 my $line = shift;
635 my $char = substr($line, 0, 1);
636 my $diff_class = "";
637
638 chomp $line;
639
640 if ($char eq '+') {
641 $diff_class = " add";
642 } elsif ($char eq "-") {
643 $diff_class = " rem";
644 } elsif ($char eq "@") {
645 $diff_class = " chunk_header";
646 } elsif ($char eq "\\") {
Jakub Narebskiaf33ef22006-08-24 01:58:49 +0200647 $diff_class = " incomplete";
Jakub Narebskieee08902006-08-24 00:15:14 +0200648 }
649 $line = untabify($line);
650 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
651}
652
Jakub Narebski717b8312006-07-31 21:22:15 +0200653## ----------------------------------------------------------------------
654## git utility subroutines, invoking git commands
655
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200656# returns path to the core git executable and the --git-dir parameter as list
657sub git_cmd {
658 return $GIT, '--git-dir='.$git_dir;
659}
660
661# returns path to the core git executable and the --git-dir parameter as string
662sub git_cmd_str {
663 return join(' ', git_cmd());
664}
665
Jakub Narebski717b8312006-07-31 21:22:15 +0200666# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +0200667sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +0200668 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200669 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200670 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200671 $git_dir = "$projectroot/$project";
672 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +0200673 my $head = <$fd>;
674 close $fd;
675 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
676 $retval = $1;
677 }
678 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200679 if (defined $o_git_dir) {
680 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200681 }
682 return $retval;
683}
684
685# get type of given object
686sub git_get_type {
687 my $hash = shift;
688
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200689 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200690 my $type = <$fd>;
691 close $fd or return;
692 chomp $type;
693 return $type;
694}
695
696sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530697 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +0200698
699 return unless ($key);
700 $key =~ s/^gitweb\.//;
701 return if ($key =~ m/\W/);
702
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200703 my @x = (git_cmd(), 'repo-config');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530704 if (defined $type) { push @x, $type; }
705 push @x, "--get";
706 push @x, "gitweb.$key";
707 my $val = qx(@x);
708 chomp $val;
Jakub Narebski717b8312006-07-31 21:22:15 +0200709 return ($val);
710}
711
Jakub Narebski717b8312006-07-31 21:22:15 +0200712# get hash of given path at given ref
713sub git_get_hash_by_path {
714 my $base = shift;
715 my $path = shift || return undef;
716
717 my $tree = $base;
718
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200719 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200720 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +0200721 my $line = <$fd>;
722 close $fd or return undef;
723
724 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
725 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
726 return $3;
727}
728
729## ......................................................................
730## git utility functions, directly accessing git repository
731
Jakub Narebski847e01f2006-08-14 02:05:47 +0200732sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +0200733 my $path = shift;
734
735 open my $fd, "$projectroot/$path/description" or return undef;
736 my $descr = <$fd>;
737 close $fd;
738 chomp $descr;
739 return $descr;
740}
741
Jakub Narebskie79ca7c2006-08-16 14:50:34 +0200742sub git_get_project_url_list {
743 my $path = shift;
744
745 open my $fd, "$projectroot/$path/cloneurl" or return undef;
746 my @git_project_url_list = map { chomp; $_ } <$fd>;
747 close $fd;
748
749 return wantarray ? @git_project_url_list : \@git_project_url_list;
750}
751
Jakub Narebski847e01f2006-08-14 02:05:47 +0200752sub git_get_projects_list {
Jakub Narebski717b8312006-07-31 21:22:15 +0200753 my @list;
754
755 if (-d $projects_list) {
756 # search in directory
757 my $dir = $projects_list;
Jakub Narebskic0011ff2006-09-14 22:18:59 +0200758 my $pfxlen = length("$dir");
759
760 File::Find::find({
761 follow_fast => 1, # follow symbolic links
762 dangling_symlinks => 0, # ignore dangling symlinks, silently
763 wanted => sub {
764 # skip project-list toplevel, if we get it.
765 return if (m!^[/.]$!);
766 # only directories can be git repositories
767 return unless (-d $_);
768
769 my $subdir = substr($File::Find::name, $pfxlen + 1);
770 # we check related file in $projectroot
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200771 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
772 -e "$projectroot/$subdir/$export_ok")) {
Jakub Narebskic0011ff2006-09-14 22:18:59 +0200773 push @list, { path => $subdir };
774 $File::Find::prune = 1;
775 }
776 },
777 }, "$dir");
778
Jakub Narebski717b8312006-07-31 21:22:15 +0200779 } elsif (-f $projects_list) {
780 # read from file(url-encoded):
781 # 'git%2Fgit.git Linus+Torvalds'
782 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
783 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
784 open my ($fd), $projects_list or return undef;
785 while (my $line = <$fd>) {
786 chomp $line;
787 my ($path, $owner) = split ' ', $line;
788 $path = unescape($path);
789 $owner = unescape($owner);
790 if (!defined $path) {
791 next;
792 }
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200793 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
794 -e "$projectroot/$path/$export_ok")) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200795 my $pr = {
796 path => $path,
797 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
798 };
799 push @list, $pr
800 }
801 }
802 close $fd;
803 }
804 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
805 return @list;
806}
807
Jakub Narebski1e0cf032006-08-14 02:10:06 +0200808sub git_get_project_owner {
809 my $project = shift;
810 my $owner;
811
812 return undef unless $project;
813
814 # read from file (url-encoded):
815 # 'git%2Fgit.git Linus+Torvalds'
816 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
817 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
818 if (-f $projects_list) {
819 open (my $fd , $projects_list);
820 while (my $line = <$fd>) {
821 chomp $line;
822 my ($pr, $ow) = split ' ', $line;
823 $pr = unescape($pr);
824 $ow = unescape($ow);
825 if ($pr eq $project) {
826 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
827 last;
828 }
829 }
830 close $fd;
831 }
832 if (!defined $owner) {
833 $owner = get_file_owner("$projectroot/$project");
834 }
835
836 return $owner;
837}
838
Jakub Narebski847e01f2006-08-14 02:05:47 +0200839sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +0200840 my $type = shift || "";
841 my %refs;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200842 my $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +0200843 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
844 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200845 if (-f "$projectroot/$project/info/refs") {
846 open $fd, "$projectroot/$project/info/refs"
847 or return;
848 } else {
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200849 open $fd, "-|", git_cmd(), "ls-remote", "."
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200850 or return;
851 }
852
Jakub Narebski717b8312006-07-31 21:22:15 +0200853 while (my $line = <$fd>) {
854 chomp $line;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200855 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200856 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200857 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +0200858 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200859 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +0200860 }
861 }
862 }
863 close $fd or return;
864 return \%refs;
865}
866
Jakub Narebski56a322f2006-08-24 19:41:23 +0200867sub git_get_rev_name_tags {
868 my $hash = shift || return undef;
869
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200870 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +0200871 or return;
872 my $name_rev = <$fd>;
873 close $fd;
874
875 if ($name_rev =~ m|^$hash tags/(.*)$|) {
876 return $1;
877 } else {
878 # catches also '$hash undefined' output
879 return undef;
880 }
881}
882
Jakub Narebski717b8312006-07-31 21:22:15 +0200883## ----------------------------------------------------------------------
884## parse to hash functions
885
Jakub Narebski847e01f2006-08-14 02:05:47 +0200886sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +0200887 my $epoch = shift;
888 my $tz = shift || "-0000";
889
890 my %date;
891 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
892 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
893 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
894 $date{'hour'} = $hour;
895 $date{'minute'} = $min;
896 $date{'mday'} = $mday;
897 $date{'day'} = $days[$wday];
898 $date{'month'} = $months[$mon];
Jakub Narebski952c65f2006-08-22 16:52:50 +0200899 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
900 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
901 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
902 $mday, $months[$mon], $hour ,$min;
Jakub Narebski717b8312006-07-31 21:22:15 +0200903
904 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
905 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
906 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
907 $date{'hour_local'} = $hour;
908 $date{'minute_local'} = $min;
909 $date{'tz_local'} = $tz;
910 return %date;
911}
912
Jakub Narebski847e01f2006-08-14 02:05:47 +0200913sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +0200914 my $tag_id = shift;
915 my %tag;
916 my @comment;
917
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200918 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200919 $tag{'id'} = $tag_id;
920 while (my $line = <$fd>) {
921 chomp $line;
922 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
923 $tag{'object'} = $1;
924 } elsif ($line =~ m/^type (.+)$/) {
925 $tag{'type'} = $1;
926 } elsif ($line =~ m/^tag (.+)$/) {
927 $tag{'name'} = $1;
928 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
929 $tag{'author'} = $1;
930 $tag{'epoch'} = $2;
931 $tag{'tz'} = $3;
932 } elsif ($line =~ m/--BEGIN/) {
933 push @comment, $line;
934 last;
935 } elsif ($line eq "") {
936 last;
937 }
938 }
939 push @comment, <$fd>;
940 $tag{'comment'} = \@comment;
941 close $fd or return;
942 if (!defined $tag{'name'}) {
943 return
944 };
945 return %tag
946}
947
Jakub Narebski847e01f2006-08-14 02:05:47 +0200948sub parse_commit {
Jakub Narebski717b8312006-07-31 21:22:15 +0200949 my $commit_id = shift;
950 my $commit_text = shift;
951
952 my @commit_lines;
953 my %co;
954
955 if (defined $commit_text) {
956 @commit_lines = @$commit_text;
957 } else {
958 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200959 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
Jakub Narebski952c65f2006-08-22 16:52:50 +0200960 or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200961 @commit_lines = split '\n', <$fd>;
962 close $fd or return;
963 $/ = "\n";
964 pop @commit_lines;
965 }
966 my $header = shift @commit_lines;
967 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
968 return;
969 }
970 ($co{'id'}, my @parents) = split ' ', $header;
971 $co{'parents'} = \@parents;
972 $co{'parent'} = $parents[0];
973 while (my $line = shift @commit_lines) {
974 last if $line eq "\n";
975 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
976 $co{'tree'} = $1;
977 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
978 $co{'author'} = $1;
979 $co{'author_epoch'} = $2;
980 $co{'author_tz'} = $3;
981 if ($co{'author'} =~ m/^([^<]+) </) {
982 $co{'author_name'} = $1;
983 } else {
984 $co{'author_name'} = $co{'author'};
985 }
986 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
987 $co{'committer'} = $1;
988 $co{'committer_epoch'} = $2;
989 $co{'committer_tz'} = $3;
990 $co{'committer_name'} = $co{'committer'};
991 $co{'committer_name'} =~ s/ <.*//;
992 }
993 }
994 if (!defined $co{'tree'}) {
995 return;
996 };
997
998 foreach my $title (@commit_lines) {
999 $title =~ s/^ //;
1000 if ($title ne "") {
1001 $co{'title'} = chop_str($title, 80, 5);
1002 # remove leading stuff of merges to make the interesting part visible
1003 if (length($title) > 50) {
1004 $title =~ s/^Automatic //;
1005 $title =~ s/^merge (of|with) /Merge ... /i;
1006 if (length($title) > 50) {
1007 $title =~ s/(http|rsync):\/\///;
1008 }
1009 if (length($title) > 50) {
1010 $title =~ s/(master|www|rsync)\.//;
1011 }
1012 if (length($title) > 50) {
1013 $title =~ s/kernel.org:?//;
1014 }
1015 if (length($title) > 50) {
1016 $title =~ s/\/pub\/scm//;
1017 }
1018 }
1019 $co{'title_short'} = chop_str($title, 50, 5);
1020 last;
1021 }
1022 }
1023 # remove added spaces
1024 foreach my $line (@commit_lines) {
1025 $line =~ s/^ //;
1026 }
1027 $co{'comment'} = \@commit_lines;
1028
1029 my $age = time - $co{'committer_epoch'};
1030 $co{'age'} = $age;
1031 $co{'age_string'} = age_string($age);
1032 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1033 if ($age > 60*60*24*7*2) {
1034 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1035 $co{'age_string_age'} = $co{'age_string'};
1036 } else {
1037 $co{'age_string_date'} = $co{'age_string'};
1038 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1039 }
1040 return %co;
1041}
1042
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001043# parse ref from ref_file, given by ref_id, with given type
1044sub parse_ref {
1045 my $ref_file = shift;
1046 my $ref_id = shift;
1047 my $type = shift || git_get_type($ref_id);
1048 my %ref_item;
1049
1050 $ref_item{'type'} = $type;
1051 $ref_item{'id'} = $ref_id;
1052 $ref_item{'epoch'} = 0;
1053 $ref_item{'age'} = "unknown";
1054 if ($type eq "tag") {
1055 my %tag = parse_tag($ref_id);
1056 $ref_item{'comment'} = $tag{'comment'};
1057 if ($tag{'type'} eq "commit") {
1058 my %co = parse_commit($tag{'object'});
1059 $ref_item{'epoch'} = $co{'committer_epoch'};
1060 $ref_item{'age'} = $co{'age_string'};
1061 } elsif (defined($tag{'epoch'})) {
1062 my $age = time - $tag{'epoch'};
1063 $ref_item{'epoch'} = $tag{'epoch'};
1064 $ref_item{'age'} = age_string($age);
1065 }
1066 $ref_item{'reftype'} = $tag{'type'};
1067 $ref_item{'name'} = $tag{'name'};
1068 $ref_item{'refid'} = $tag{'object'};
1069 } elsif ($type eq "commit"){
1070 my %co = parse_commit($ref_id);
1071 $ref_item{'reftype'} = "commit";
1072 $ref_item{'name'} = $ref_file;
1073 $ref_item{'title'} = $co{'title'};
1074 $ref_item{'refid'} = $ref_id;
1075 $ref_item{'epoch'} = $co{'committer_epoch'};
1076 $ref_item{'age'} = $co{'age_string'};
1077 } else {
1078 $ref_item{'reftype'} = $type;
1079 $ref_item{'name'} = $ref_file;
1080 $ref_item{'refid'} = $ref_id;
1081 }
1082
1083 return %ref_item;
1084}
1085
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001086# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02001087sub parse_difftree_raw_line {
1088 my $line = shift;
1089 my %res;
1090
1091 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1092 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1093 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1094 $res{'from_mode'} = $1;
1095 $res{'to_mode'} = $2;
1096 $res{'from_id'} = $3;
1097 $res{'to_id'} = $4;
1098 $res{'status'} = $5;
1099 $res{'similarity'} = $6;
1100 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001101 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02001102 } else {
1103 $res{'file'} = unquote($7);
1104 }
1105 }
1106 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02001107 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1108 $res{'commit'} = $1;
1109 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02001110
1111 return wantarray ? %res : \%res;
1112}
1113
Jakub Narebskicb849b42006-08-31 00:32:15 +02001114# parse line of git-ls-tree output
1115sub parse_ls_tree_line ($;%) {
1116 my $line = shift;
1117 my %opts = @_;
1118 my %res;
1119
1120 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1121 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1122
1123 $res{'mode'} = $1;
1124 $res{'type'} = $2;
1125 $res{'hash'} = $3;
1126 if ($opts{'-z'}) {
1127 $res{'name'} = $4;
1128 } else {
1129 $res{'name'} = unquote($4);
1130 }
1131
1132 return wantarray ? %res : \%res;
1133}
1134
Jakub Narebski717b8312006-07-31 21:22:15 +02001135## ......................................................................
1136## parse to array of hashes functions
1137
Jakub Narebski847e01f2006-08-14 02:05:47 +02001138sub git_get_refs_list {
Jakub Narebski717b8312006-07-31 21:22:15 +02001139 my $ref_dir = shift;
1140 my @reflist;
1141
1142 my @refs;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001143 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1144 or return;
1145 while (my $line = <$fd>) {
1146 chomp $line;
1147 if ($line =~ m/^([0-9a-fA-F]{40})\t$ref_dir\/?([^\^]+)$/) {
1148 push @refs, { hash => $1, name => $2 };
1149 } elsif ($line =~ m/^[0-9a-fA-F]{40}\t$ref_dir\/?(.*)\^\{\}$/ &&
1150 $1 eq $refs[-1]{'name'}) {
1151 # most likely a tag is followed by its peeled
1152 # (deref) one, and when that happens we know the
1153 # previous one was of type 'tag'.
1154 $refs[-1]{'type'} = "tag";
Jakub Narebski717b8312006-07-31 21:22:15 +02001155 }
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001156 }
1157 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07001158
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001159 foreach my $ref (@refs) {
1160 my $ref_file = $ref->{'name'};
1161 my $ref_id = $ref->{'hash'};
1162
1163 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001164 my %ref_item = parse_ref($ref_file, $ref_id, $type);
Jakub Narebski717b8312006-07-31 21:22:15 +02001165
1166 push @reflist, \%ref_item;
1167 }
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001168 # sort refs by age
Jakub Narebski717b8312006-07-31 21:22:15 +02001169 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1170 return \@reflist;
1171}
1172
1173## ----------------------------------------------------------------------
1174## filesystem-related functions
1175
1176sub get_file_owner {
1177 my $path = shift;
1178
1179 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1180 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1181 if (!defined $gcos) {
1182 return undef;
1183 }
1184 my $owner = $gcos;
1185 $owner =~ s/[,;].*$//;
1186 return decode("utf8", $owner, Encode::FB_DEFAULT);
1187}
1188
1189## ......................................................................
1190## mimetype related functions
1191
1192sub mimetype_guess_file {
1193 my $filename = shift;
1194 my $mimemap = shift;
1195 -r $mimemap or return undef;
1196
1197 my %mimemap;
1198 open(MIME, $mimemap) or return undef;
1199 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02001200 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02001201 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07001202 if (defined $exts) {
1203 my @exts = split(/\s+/, $exts);
1204 foreach my $ext (@exts) {
1205 $mimemap{$ext} = $mime;
1206 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001207 }
1208 }
1209 close(MIME);
1210
Jakub Narebski80593192006-09-19 13:57:03 +02001211 $filename =~ /\.([^.]*)$/;
Jakub Narebski717b8312006-07-31 21:22:15 +02001212 return $mimemap{$1};
1213}
1214
1215sub mimetype_guess {
1216 my $filename = shift;
1217 my $mime;
1218 $filename =~ /\./ or return undef;
1219
1220 if ($mimetypes_file) {
1221 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02001222 if ($file !~ m!^/!) { # if it is relative path
1223 # it is relative to project
1224 $file = "$projectroot/$project/$file";
1225 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001226 $mime = mimetype_guess_file($filename, $file);
1227 }
1228 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1229 return $mime;
1230}
1231
Jakub Narebski847e01f2006-08-14 02:05:47 +02001232sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02001233 my $fd = shift;
1234 my $filename = shift;
1235
1236 if ($filename) {
1237 my $mime = mimetype_guess($filename);
1238 $mime and return $mime;
1239 }
1240
1241 # just in case
1242 return $default_blob_plain_mimetype unless $fd;
1243
1244 if (-T $fd) {
1245 return 'text/plain' .
1246 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1247 } elsif (! $filename) {
1248 return 'application/octet-stream';
1249 } elsif ($filename =~ m/\.png$/i) {
1250 return 'image/png';
1251 } elsif ($filename =~ m/\.gif$/i) {
1252 return 'image/gif';
1253 } elsif ($filename =~ m/\.jpe?g$/i) {
1254 return 'image/jpeg';
1255 } else {
1256 return 'application/octet-stream';
1257 }
1258}
1259
1260## ======================================================================
1261## functions printing HTML: header, footer, error page
1262
Kay Sievers12a88f22005-08-07 20:02:47 +02001263sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02001264 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02001265 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001266
Alp Toker49da1da2006-07-11 21:10:26 +01001267 my $title = "$site_name git";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001268 if (defined $project) {
1269 $title .= " - $project";
1270 if (defined $action) {
1271 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00001272 if (defined $file_name) {
1273 $title .= " - $file_name";
1274 if ($action eq "tree" && $file_name !~ m|/$|) {
1275 $title .= "/";
1276 }
1277 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02001278 }
1279 }
Alp Tokerf6801d62006-07-11 11:19:34 +01001280 my $content_type;
1281 # require explicit support from the UA if we are to send the page as
1282 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1283 # we have to do this because MSIE sometimes globs '*/*', pretending to
1284 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02001285 if (defined $cgi->http('HTTP_ACCEPT') &&
1286 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1287 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01001288 $content_type = 'application/xhtml+xml';
1289 } else {
1290 $content_type = 'text/html';
1291 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02001292 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1293 -status=> $status, -expires => $expires);
Kay Sieversa59d4af2005-08-07 20:15:44 +02001294 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02001295<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02001296<!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 +02001297<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001298<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02001299<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02001300<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01001301<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001302<meta name="generator" content="gitweb/$version git/$git_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02001303<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02001304<title>$title</title>
Jakub Narebskiae20de52006-06-21 09:48:03 +02001305<link rel="stylesheet" type="text/css" href="$stylesheet"/>
Kay Sievers161332a2005-08-07 19:49:46 +02001306EOF
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001307 if (defined $project) {
1308 printf('<link rel="alternate" title="%s log" '.
1309 'href="%s" type="application/rss+xml"/>'."\n",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001310 esc_param($project), href(action=>"rss"));
Jakub Narebski9d0734a2006-09-15 11:11:33 +02001311 } else {
1312 printf('<link rel="alternate" title="%s projects list" '.
1313 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1314 $site_name, href(project=>undef, action=>"project_index"));
1315 printf('<link rel="alternate" title="%s projects logs" '.
1316 'href="%s" type="text/x-opml"/>'."\n",
1317 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001318 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02001319 if (defined $favicon) {
1320 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1321 }
Jakub Narebski10161352006-08-05 13:18:58 +02001322
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001323 print "</head>\n" .
1324 "<body>\n" .
Jakub Narebski10161352006-08-05 13:18:58 +02001325 "<div class=\"page_header\">\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001326 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
Martin Waitz281f2f62006-07-31 00:38:39 +02001327 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
Kay Sievers19806692005-08-07 20:26:27 +02001328 "</a>\n";
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +09001329 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001330 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001331 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02001332 if (defined $action) {
1333 print " / $action";
1334 }
Kay Sievers19806692005-08-07 20:26:27 +02001335 print "\n";
1336 if (!defined $searchtext) {
1337 $searchtext = "";
1338 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02001339 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03001340 if (defined $hash_base) {
1341 $search_hash = $hash_base;
1342 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02001343 $search_hash = $hash;
1344 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02001345 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02001346 }
Kay Sievers19806692005-08-07 20:26:27 +02001347 $cgi->param("a", "search");
Kay Sieversc39e47d2005-09-17 03:00:21 +02001348 $cgi->param("h", $search_hash);
Kay Sievers353347b2005-11-14 05:47:18 +01001349 print $cgi->startform(-method => "get", -action => $my_uri) .
Kay Sieversc994d622005-08-07 20:27:18 +02001350 "<div class=\"search\">\n" .
1351 $cgi->hidden(-name => "p") . "\n" .
1352 $cgi->hidden(-name => "a") . "\n" .
Kay Sieversc39e47d2005-09-17 03:00:21 +02001353 $cgi->hidden(-name => "h") . "\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001354 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1355 "</div>" .
1356 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001357 }
1358 print "</div>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02001359}
1360
Kay Sievers12a88f22005-08-07 20:02:47 +02001361sub git_footer_html {
Kay Sievers6191f8e2005-08-07 20:19:56 +02001362 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001363 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02001364 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02001365 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01001366 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02001367 }
Jakub Narebskia1565c42006-09-15 19:30:34 +02001368 print $cgi->a({-href => href(action=>"rss"),
1369 -class => "rss_logo"}, "RSS") . "\n";
Kay Sieversc994d622005-08-07 20:27:18 +02001370 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02001371 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski9d0734a2006-09-15 11:11:33 +02001372 -class => "rss_logo"}, "OPML") . " ";
1373 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1374 -class => "rss_logo"}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02001375 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02001376 print "</div>\n" .
1377 "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02001378 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02001379}
1380
Kay Sievers061cc7c2005-08-07 20:15:57 +02001381sub die_error {
1382 my $status = shift || "403 Forbidden";
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02001383 my $error = shift || "Malformed query, file missing or permission denied";
Kay Sievers664f4cc2005-08-07 20:17:19 +02001384
Kay Sieversa59d4af2005-08-07 20:15:44 +02001385 git_header_html($status);
Jakub Narebski59b9f612006-08-22 23:42:53 +02001386 print <<EOF;
1387<div class="page_body">
1388<br /><br />
1389$status - $error
1390<br />
1391</div>
1392EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02001393 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02001394 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001395}
1396
Jakub Narebski717b8312006-07-31 21:22:15 +02001397## ----------------------------------------------------------------------
1398## functions printing or outputting HTML: navigation
1399
Jakub Narebski847e01f2006-08-14 02:05:47 +02001400sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001401 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1402 $extra = '' if !defined $extra; # pager or formats
1403
1404 my @navs = qw(summary shortlog log commit commitdiff tree);
1405 if ($suppress) {
1406 @navs = grep { $_ ne $suppress } @navs;
1407 }
1408
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001409 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001410 if (defined $head) {
1411 for (qw(commit commitdiff)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001412 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001413 }
1414 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1415 for (qw(shortlog log)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001416 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001417 }
1418 }
1419 }
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001420 $arg{tree}{hash} = $treehead if defined $treehead;
1421 $arg{tree}{hash_base} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001422
1423 print "<div class=\"page_nav\">\n" .
1424 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001425 map { $_ eq $current ?
1426 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1427 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02001428 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001429 "</div>\n";
1430}
1431
Jakub Narebski847e01f2006-08-14 02:05:47 +02001432sub format_paging_nav {
Jakub Narebski6855f422006-07-30 20:31:00 +02001433 my ($action, $hash, $head, $page, $nrevs) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02001434 my $paging_nav;
1435
1436
1437 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001438 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001439 } else {
1440 $paging_nav .= "HEAD";
1441 }
1442
1443 if ($page > 0) {
1444 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001445 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001446 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001447 } else {
1448 $paging_nav .= " &sdot; prev";
1449 }
1450
1451 if ($nrevs >= (100 * ($page+1)-1)) {
1452 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001453 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001454 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001455 } else {
1456 $paging_nav .= " &sdot; next";
1457 }
1458
1459 return $paging_nav;
1460}
1461
Jakub Narebski717b8312006-07-31 21:22:15 +02001462## ......................................................................
1463## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02001464
Jakub Narebski847e01f2006-08-14 02:05:47 +02001465sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02001466 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001467 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02001468
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001469 $args{action} = $action;
1470 $args{hash} = $hash if $hash;
1471 $args{hash_base} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02001472
1473 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001474 $cgi->a({-href => href(%args), -class => "title"},
1475 $title ? $title : $action) .
1476 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02001477}
1478
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001479#sub git_print_authorship (\%) {
1480sub git_print_authorship {
1481 my $co = shift;
1482
Jakub Narebskia44465c2006-08-28 23:17:31 +02001483 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001484 print "<div class=\"author_date\">" .
1485 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02001486 " [$ad{'rfc2822'}";
1487 if ($ad{'hour_local'} < 6) {
1488 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1489 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1490 } else {
1491 printf(" (%02d:%02d %s)",
1492 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1493 }
1494 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001495}
1496
Jakub Narebski717b8312006-07-31 21:22:15 +02001497sub git_print_page_path {
1498 my $name = shift;
1499 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001500 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01001501
Jakub Narebski717b8312006-07-31 21:22:15 +02001502 if (!defined $name) {
Jakub Narebski63e42202006-08-22 12:38:59 +02001503 print "<div class=\"page_path\">/</div>\n";
Jakub Narebski762c7202006-09-04 18:17:58 +02001504 } else {
1505 my @dirname = split '/', $name;
1506 my $basename = pop @dirname;
1507 my $fullname = '';
1508
Jakub Narebski63e42202006-08-22 12:38:59 +02001509 print "<div class=\"page_path\">";
Jakub Narebski762c7202006-09-04 18:17:58 +02001510 foreach my $dir (@dirname) {
1511 $fullname .= $dir . '/';
1512 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1513 hash_base=>$hb),
1514 -title => $fullname}, esc_html($dir));
1515 print "/";
1516 }
1517 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02001518 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02001519 hash_base=>$hb),
1520 -title => $name}, esc_html($basename));
1521 } elsif (defined $type && $type eq 'tree') {
1522 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1523 hash_base=>$hb),
1524 -title => $name}, esc_html($basename));
1525 print "/";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001526 } else {
Jakub Narebski762c7202006-09-04 18:17:58 +02001527 print esc_html($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001528 }
Jakub Narebski63e42202006-08-22 12:38:59 +02001529 print "<br/></div>\n";
Kay Sievers8ed23e12005-08-07 20:05:44 +02001530 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001531}
1532
Jakub Narebskib7f92532006-08-28 14:48:10 +02001533# sub git_print_log (\@;%) {
1534sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02001535 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001536 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001537
Jakub Narebskib7f92532006-08-28 14:48:10 +02001538 if ($opts{'-remove_title'}) {
1539 # remove title, i.e. first line of log
1540 shift @$log;
1541 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02001542 # remove leading empty lines
1543 while (defined $log->[0] && $log->[0] eq "") {
1544 shift @$log;
1545 }
1546
1547 # print log
1548 my $signoff = 0;
1549 my $empty = 0;
1550 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02001551 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1552 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02001553 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001554 if (! $opts{'-remove_signoff'}) {
1555 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1556 next;
1557 } else {
1558 # remove signoff lines
1559 next;
1560 }
1561 } else {
1562 $signoff = 0;
1563 }
1564
Jakub Narebskid16d0932006-08-17 11:21:23 +02001565 # print only one empty line
1566 # do not print empty line after signoff
1567 if ($line eq "") {
1568 next if ($empty || $signoff);
1569 $empty = 1;
1570 } else {
1571 $empty = 0;
1572 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02001573
1574 print format_log_line_html($line) . "<br/>\n";
1575 }
1576
1577 if ($opts{'-final_empty_line'}) {
1578 # end with single empty line
1579 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001580 }
1581}
1582
1583sub git_print_simplified_log {
1584 my $log = shift;
1585 my $remove_title = shift;
1586
Jakub Narebskib7f92532006-08-28 14:48:10 +02001587 git_print_log($log,
1588 -final_empty_line=> 1,
Jakub Narebskib7f92532006-08-28 14:48:10 +02001589 -remove_title => $remove_title);
Jakub Narebskid16d0932006-08-17 11:21:23 +02001590}
1591
Jakub Narebskifa702002006-08-31 00:35:07 +02001592# print tree entry (row of git_tree), but without encompassing <tr> element
1593sub git_print_tree_entry {
1594 my ($t, $basedir, $hash_base, $have_blame) = @_;
1595
1596 my %base_key = ();
1597 $base_key{hash_base} = $hash_base if defined $hash_base;
1598
1599 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1600 if ($t->{'type'} eq "blob") {
1601 print "<td class=\"list\">" .
1602 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1603 file_name=>"$basedir$t->{'name'}", %base_key),
1604 -class => "list"}, esc_html($t->{'name'})) .
1605 "</td>\n" .
1606 "<td class=\"link\">" .
1607 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1608 file_name=>"$basedir$t->{'name'}", %base_key)},
1609 "blob");
1610 if ($have_blame) {
1611 print " | " .
1612 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1613 file_name=>"$basedir$t->{'name'}", %base_key)},
1614 "blame");
1615 }
1616 if (defined $hash_base) {
1617 print " | " .
1618 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1619 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1620 "history");
1621 }
1622 print " | " .
1623 $cgi->a({-href => href(action=>"blob_plain",
1624 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1625 "raw") .
1626 "</td>\n";
1627
1628 } elsif ($t->{'type'} eq "tree") {
1629 print "<td class=\"list\">" .
1630 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1631 file_name=>"$basedir$t->{'name'}", %base_key)},
1632 esc_html($t->{'name'})) .
1633 "</td>\n" .
1634 "<td class=\"link\">" .
1635 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1636 file_name=>"$basedir$t->{'name'}", %base_key)},
1637 "tree");
1638 if (defined $hash_base) {
1639 print " | " .
1640 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1641 file_name=>"$basedir$t->{'name'}")},
1642 "history");
1643 }
1644 print "</td>\n";
1645 }
1646}
1647
Jakub Narebski717b8312006-07-31 21:22:15 +02001648## ......................................................................
1649## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02001650
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001651sub git_difftree_body {
Jakub Narebskieee08902006-08-24 00:15:14 +02001652 my ($difftree, $hash, $parent) = @_;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001653
1654 print "<div class=\"list_head\">\n";
1655 if ($#{$difftree} > 10) {
1656 print(($#{$difftree} + 1) . " files changed:\n");
1657 }
1658 print "</div>\n";
1659
1660 print "<table class=\"diff_tree\">\n";
1661 my $alternate = 0;
Jakub Narebskib4657e72006-08-28 14:48:14 +02001662 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001663 foreach my $line (@{$difftree}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001664 my %diff = parse_difftree_raw_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001665
1666 if ($alternate) {
1667 print "<tr class=\"dark\">\n";
1668 } else {
1669 print "<tr class=\"light\">\n";
1670 }
1671 $alternate ^= 1;
1672
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001673 my ($to_mode_oct, $to_mode_str, $to_file_type);
1674 my ($from_mode_oct, $from_mode_str, $from_file_type);
1675 if ($diff{'to_mode'} ne ('0' x 6)) {
1676 $to_mode_oct = oct $diff{'to_mode'};
1677 if (S_ISREG($to_mode_oct)) { # only for regular file
1678 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001679 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001680 $to_file_type = file_type($diff{'to_mode'});
1681 }
1682 if ($diff{'from_mode'} ne ('0' x 6)) {
1683 $from_mode_oct = oct $diff{'from_mode'};
1684 if (S_ISREG($to_mode_oct)) { # only for regular file
1685 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1686 }
1687 $from_file_type = file_type($diff{'from_mode'});
1688 }
1689
1690 if ($diff{'status'} eq "A") { # created
1691 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1692 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1693 $mode_chng .= "]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001694 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001695 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1696 hash_base=>$hash, file_name=>$diff{'file'}),
1697 -class => "list"}, esc_html($diff{'file'})) .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001698 "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001699 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001700 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001701 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1702 hash_base=>$hash, file_name=>$diff{'file'})},
Jakub Narebskib4657e72006-08-28 14:48:14 +02001703 "blob");
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001704 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001705 # link to patch
1706 $patchno++;
1707 print " | " .
1708 $cgi->a({-href => "#patch$patchno"}, "patch");
1709 }
1710 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001711
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001712 } elsif ($diff{'status'} eq "D") { # deleted
1713 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001714 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001715 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1716 hash_base=>$parent, file_name=>$diff{'file'}),
1717 -class => "list"}, esc_html($diff{'file'})) .
1718 "</td>\n" .
1719 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001720 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001721 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1722 hash_base=>$parent, file_name=>$diff{'file'})},
1723 "blob") .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001724 " | ";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001725 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001726 # link to patch
1727 $patchno++;
1728 print " | " .
1729 $cgi->a({-href => "#patch$patchno"}, "patch");
1730 }
1731 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski09052552006-08-26 23:33:58 +02001732 file_name=>$diff{'file'})},
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001733 "history") .
1734 "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001735
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001736 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001737 my $mode_chnge = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001738 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1739 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1740 if ($from_file_type != $to_file_type) {
1741 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001742 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001743 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1744 if ($from_mode_str && $to_mode_str) {
1745 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1746 } elsif ($to_mode_str) {
1747 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001748 }
1749 }
1750 $mode_chnge .= "]</span>\n";
1751 }
1752 print "<td>";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001753 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski420e92f2006-08-24 23:53:54 +02001754 print $cgi->a({-href => href(action=>"blobdiff",
1755 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1756 hash_base=>$hash, hash_parent_base=>$parent,
1757 file_name=>$diff{'file'}),
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001758 -class => "list"}, esc_html($diff{'file'}));
1759 } else { # only mode changed
1760 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1761 hash_base=>$hash, file_name=>$diff{'file'}),
1762 -class => "list"}, esc_html($diff{'file'}));
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001763 }
1764 print "</td>\n" .
1765 "<td>$mode_chnge</td>\n" .
1766 "<td class=\"link\">" .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001767 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1768 hash_base=>$hash, file_name=>$diff{'file'})},
1769 "blob");
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001770 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001771 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001772 # link to patch
1773 $patchno++;
1774 print " | " .
1775 $cgi->a({-href => "#patch$patchno"}, "patch");
1776 } else {
1777 print " | " .
1778 $cgi->a({-href => href(action=>"blobdiff",
1779 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1780 hash_base=>$hash, hash_parent_base=>$parent,
1781 file_name=>$diff{'file'})},
1782 "diff");
1783 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001784 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001785 print " | " .
1786 $cgi->a({-href => href(action=>"history",
1787 hash_base=>$hash, file_name=>$diff{'file'})},
1788 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001789 print "</td>\n";
1790
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001791 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1792 my %status_name = ('R' => 'moved', 'C' => 'copied');
1793 my $nstatus = $status_name{$diff{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001794 my $mode_chng = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001795 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1796 # mode also for directories, so we cannot use $to_mode_str
1797 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001798 }
1799 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001800 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1801 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1802 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1803 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1804 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1805 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1806 -class => "list"}, esc_html($diff{'from_file'})) .
1807 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001808 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001809 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1810 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1811 "blob");
1812 if ($diff{'to_id'} ne $diff{'from_id'}) {
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001813 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001814 # link to patch
1815 $patchno++;
1816 print " | " .
1817 $cgi->a({-href => "#patch$patchno"}, "patch");
1818 } else {
1819 print " | " .
1820 $cgi->a({-href => href(action=>"blobdiff",
1821 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1822 hash_base=>$hash, hash_parent_base=>$parent,
1823 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1824 "diff");
1825 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001826 }
1827 print "</td>\n";
1828
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001829 } # we should not encounter Unmerged (U) or Unknown (X) status
1830 print "</tr>\n";
1831 }
1832 print "</table>\n";
1833}
1834
Jakub Narebskieee08902006-08-24 00:15:14 +02001835sub git_patchset_body {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001836 my ($fd, $difftree, $hash, $hash_parent) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001837
1838 my $patch_idx = 0;
1839 my $in_header = 0;
1840 my $patch_found = 0;
Jakub Narebskife875852006-08-25 20:59:39 +02001841 my $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02001842
1843 print "<div class=\"patchset\">\n";
1844
Jakub Narebski157e43b2006-08-24 19:34:36 +02001845 LINE:
Jakub Narebskic8a99d72006-08-26 19:14:23 +02001846 while (my $patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001847 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02001848
1849 if ($patch_line =~ m/^diff /) { # "git diff" header
1850 # beginning of patch (in patchset)
1851 if ($patch_found) {
1852 # close previous patch
1853 print "</div>\n"; # class="patch"
1854 } else {
1855 # first patch in patchset
1856 $patch_found = 1;
1857 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02001858 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001859
Jakub Narebskife875852006-08-25 20:59:39 +02001860 if (ref($difftree->[$patch_idx]) eq "HASH") {
1861 $diffinfo = $difftree->[$patch_idx];
1862 } else {
1863 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1864 }
1865 $patch_idx++;
Jakub Narebskieee08902006-08-24 00:15:14 +02001866
1867 # for now, no extended header, hence we skip empty patches
1868 # companion to next LINE if $in_header;
Jakub Narebskife875852006-08-25 20:59:39 +02001869 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
Jakub Narebskieee08902006-08-24 00:15:14 +02001870 $in_header = 1;
1871 next LINE;
1872 }
1873
Jakub Narebskife875852006-08-25 20:59:39 +02001874 if ($diffinfo->{'status'} eq "A") { # added
1875 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001876 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001877 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1878 $diffinfo->{'to_id'}) . "(new)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001879 "</div>\n"; # class="diff_info"
1880
Jakub Narebskife875852006-08-25 20:59:39 +02001881 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1882 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001883 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001884 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1885 $diffinfo->{'from_id'}) . "(deleted)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001886 "</div>\n"; # class="diff_info"
1887
Jakub Narebskife875852006-08-25 20:59:39 +02001888 } elsif ($diffinfo->{'status'} eq "R" || # renamed
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001889 $diffinfo->{'status'} eq "C" || # copied
1890 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
Jakub Narebskieee08902006-08-24 00:15:14 +02001891 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001892 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001893 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001894 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1895 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001896 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001897 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001898 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001899 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1900 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001901 print "</div>\n"; # class="diff_info"
1902
1903 } else { # modified, mode changed, ...
1904 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001905 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001906 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001907 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1908 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001909 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001910 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001911 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001912 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1913 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001914 print "</div>\n"; # class="diff_info"
1915 }
1916
1917 #print "<div class=\"diff extended_header\">\n";
1918 $in_header = 1;
1919 next LINE;
1920 } # start of patch in patchset
1921
1922
1923 if ($in_header && $patch_line =~ m/^---/) {
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001924 #print "</div>\n"; # class="diff extended_header"
Jakub Narebskieee08902006-08-24 00:15:14 +02001925 $in_header = 0;
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001926
1927 my $file = $diffinfo->{'from_file'};
1928 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001929 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1930 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1931 -class => "list"}, esc_html($file));
1932 $patch_line =~ s|a/.*$|a/$file|g;
1933 print "<div class=\"diff from_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001934
1935 $patch_line = <$fd>;
1936 chomp $patch_line;
1937
1938 #$patch_line =~ m/^+++/;
1939 $file = $diffinfo->{'to_file'};
1940 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001941 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1942 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1943 -class => "list"}, esc_html($file));
1944 $patch_line =~ s|b/.*|b/$file|g;
1945 print "<div class=\"diff to_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001946
1947 next LINE;
Jakub Narebskieee08902006-08-24 00:15:14 +02001948 }
1949 next LINE if $in_header;
1950
1951 print format_diff_line($patch_line);
1952 }
1953 print "</div>\n" if $patch_found; # class="patch"
1954
1955 print "</div>\n"; # class="patchset"
1956}
1957
1958# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1959
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001960sub git_shortlog_body {
1961 # uses global variable $project
1962 my ($revlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301963
1964 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1965 my $have_snapshot = (defined $ctype && defined $suffix);
1966
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001967 $from = 0 unless defined $from;
1968 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1969
1970 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1971 my $alternate = 0;
1972 for (my $i = $from; $i <= $to; $i++) {
1973 my $commit = $revlist->[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02001974 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1975 my $ref = format_ref_marker($refs, $commit);
1976 my %co = parse_commit($commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001977 if ($alternate) {
1978 print "<tr class=\"dark\">\n";
1979 } else {
1980 print "<tr class=\"light\">\n";
1981 }
1982 $alternate ^= 1;
1983 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1984 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1985 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1986 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02001987 print format_subject_html($co{'title'}, $co{'title_short'},
1988 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001989 print "</td>\n" .
1990 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001991 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05301992 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1993 if ($have_snapshot) {
1994 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1995 }
1996 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001997 "</tr>\n";
1998 }
1999 if (defined $extra) {
2000 print "<tr>\n" .
2001 "<td colspan=\"4\">$extra</td>\n" .
2002 "</tr>\n";
2003 }
2004 print "</table>\n";
2005}
2006
Jakub Narebski581860e2006-08-14 02:09:08 +02002007sub git_history_body {
2008 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski8be68352006-09-11 00:36:04 +02002009 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2010
2011 $from = 0 unless defined $from;
2012 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02002013
2014 print "<table class=\"history\" cellspacing=\"0\">\n";
2015 my $alternate = 0;
Jakub Narebski8be68352006-09-11 00:36:04 +02002016 for (my $i = $from; $i <= $to; $i++) {
2017 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
Jakub Narebski581860e2006-08-14 02:09:08 +02002018 next;
2019 }
2020
2021 my $commit = $1;
2022 my %co = parse_commit($commit);
2023 if (!%co) {
2024 next;
2025 }
2026
2027 my $ref = format_ref_marker($refs, $commit);
2028
2029 if ($alternate) {
2030 print "<tr class=\"dark\">\n";
2031 } else {
2032 print "<tr class=\"light\">\n";
2033 }
2034 $alternate ^= 1;
2035 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2036 # shortlog uses chop_str($co{'author_name'}, 10)
2037 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2038 "<td>";
2039 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02002040 print format_subject_html($co{'title'}, $co{'title_short'},
2041 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02002042 print "</td>\n" .
2043 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002044 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2045 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2046 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
Jakub Narebski581860e2006-08-14 02:09:08 +02002047
2048 if ($ftype eq 'blob') {
2049 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2050 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2051 if (defined $blob_current && defined $blob_parent &&
2052 $blob_current ne $blob_parent) {
2053 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02002054 $cgi->a({-href => href(action=>"blobdiff",
2055 hash=>$blob_current, hash_parent=>$blob_parent,
2056 hash_base=>$hash_base, hash_parent_base=>$commit,
2057 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02002058 "diff to current");
2059 }
2060 }
2061 print "</td>\n" .
2062 "</tr>\n";
2063 }
2064 if (defined $extra) {
2065 print "<tr>\n" .
2066 "<td colspan=\"4\">$extra</td>\n" .
2067 "</tr>\n";
2068 }
2069 print "</table>\n";
2070}
2071
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002072sub git_tags_body {
2073 # uses global variable $project
2074 my ($taglist, $from, $to, $extra) = @_;
2075 $from = 0 unless defined $from;
2076 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2077
2078 print "<table class=\"tags\" cellspacing=\"0\">\n";
2079 my $alternate = 0;
2080 for (my $i = $from; $i <= $to; $i++) {
2081 my $entry = $taglist->[$i];
2082 my %tag = %$entry;
2083 my $comment_lines = $tag{'comment'};
2084 my $comment = shift @$comment_lines;
2085 my $comment_short;
2086 if (defined $comment) {
2087 $comment_short = chop_str($comment, 30, 5);
2088 }
2089 if ($alternate) {
2090 print "<tr class=\"dark\">\n";
2091 } else {
2092 print "<tr class=\"light\">\n";
2093 }
2094 $alternate ^= 1;
2095 print "<td><i>$tag{'age'}</i></td>\n" .
2096 "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002097 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002098 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002099 "</td>\n" .
2100 "<td>";
2101 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002102 print format_subject_html($comment, $comment_short,
2103 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002104 }
2105 print "</td>\n" .
2106 "<td class=\"selflink\">";
2107 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002108 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002109 } else {
2110 print "&nbsp;";
2111 }
2112 print "</td>\n" .
2113 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002114 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002115 if ($tag{'reftype'} eq "commit") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002116 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2117 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002118 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002119 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002120 }
2121 print "</td>\n" .
2122 "</tr>";
2123 }
2124 if (defined $extra) {
2125 print "<tr>\n" .
2126 "<td colspan=\"5\">$extra</td>\n" .
2127 "</tr>\n";
2128 }
2129 print "</table>\n";
2130}
2131
2132sub git_heads_body {
2133 # uses global variable $project
2134 my ($taglist, $head, $from, $to, $extra) = @_;
2135 $from = 0 unless defined $from;
2136 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2137
2138 print "<table class=\"heads\" cellspacing=\"0\">\n";
2139 my $alternate = 0;
2140 for (my $i = $from; $i <= $to; $i++) {
2141 my $entry = $taglist->[$i];
2142 my %tag = %$entry;
2143 my $curr = $tag{'id'} eq $head;
2144 if ($alternate) {
2145 print "<tr class=\"dark\">\n";
2146 } else {
2147 print "<tr class=\"light\">\n";
2148 }
2149 $alternate ^= 1;
2150 print "<td><i>$tag{'age'}</i></td>\n" .
2151 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002152 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002153 -class => "list name"},esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002154 "</td>\n" .
2155 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002156 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2157 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002158 "</td>\n" .
2159 "</tr>";
2160 }
2161 if (defined $extra) {
2162 print "<tr>\n" .
2163 "<td colspan=\"3\">$extra</td>\n" .
2164 "</tr>\n";
2165 }
2166 print "</table>\n";
2167}
2168
Jakub Narebski717b8312006-07-31 21:22:15 +02002169## ======================================================================
2170## ======================================================================
2171## actions
2172
Jakub Narebski717b8312006-07-31 21:22:15 +02002173sub git_project_list {
Jakub Narebski6326b602006-08-01 02:59:12 +02002174 my $order = $cgi->param('o');
2175 if (defined $order && $order !~ m/project|descr|owner|age/) {
Jakub Narebskie2860ea2006-08-05 13:15:24 +02002176 die_error(undef, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02002177 }
2178
Jakub Narebski847e01f2006-08-14 02:05:47 +02002179 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02002180 my @projects;
2181 if (!@list) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002182 die_error(undef, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02002183 }
2184 foreach my $pr (@list) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002185 my $head = git_get_head_hash($pr->{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02002186 if (!defined $head) {
2187 next;
2188 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002189 $git_dir = "$projectroot/$pr->{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02002190 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02002191 if (!%co) {
2192 next;
2193 }
2194 $pr->{'commit'} = \%co;
2195 if (!defined $pr->{'descr'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002196 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski717b8312006-07-31 21:22:15 +02002197 $pr->{'descr'} = chop_str($descr, 25, 5);
2198 }
2199 if (!defined $pr->{'owner'}) {
2200 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2201 }
2202 push @projects, $pr;
2203 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002204
Jakub Narebski717b8312006-07-31 21:22:15 +02002205 git_header_html();
2206 if (-f $home_text) {
2207 print "<div class=\"index_include\">\n";
2208 open (my $fd, $home_text);
2209 print <$fd>;
2210 close $fd;
2211 print "</div>\n";
2212 }
2213 print "<table class=\"project_list\">\n" .
2214 "<tr>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002215 $order ||= "project";
2216 if ($order eq "project") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002217 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2218 print "<th>Project</th>\n";
2219 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002220 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002221 $cgi->a({-href => href(project=>undef, order=>'project'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002222 -class => "header"}, "Project") .
2223 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002224 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002225 if ($order eq "descr") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002226 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2227 print "<th>Description</th>\n";
2228 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002229 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002230 $cgi->a({-href => href(project=>undef, order=>'descr'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002231 -class => "header"}, "Description") .
2232 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002233 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002234 if ($order eq "owner") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002235 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2236 print "<th>Owner</th>\n";
2237 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002238 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002239 $cgi->a({-href => href(project=>undef, order=>'owner'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002240 -class => "header"}, "Owner") .
2241 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002242 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002243 if ($order eq "age") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002244 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2245 print "<th>Last Change</th>\n";
2246 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002247 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002248 $cgi->a({-href => href(project=>undef, order=>'age'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002249 -class => "header"}, "Last Change") .
2250 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002251 }
2252 print "<th></th>\n" .
2253 "</tr>\n";
2254 my $alternate = 0;
2255 foreach my $pr (@projects) {
2256 if ($alternate) {
2257 print "<tr class=\"dark\">\n";
2258 } else {
2259 print "<tr class=\"light\">\n";
2260 }
2261 $alternate ^= 1;
Martin Waitz756d2f02006-08-17 00:28:36 +02002262 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski6326b602006-08-01 02:59:12 +02002263 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002264 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2265 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002266 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2267 $pr->{'commit'}{'age_string'} . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002268 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02002269 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2270 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2271 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
Jakub Narebski717b8312006-07-31 21:22:15 +02002272 "</td>\n" .
2273 "</tr>\n";
2274 }
2275 print "</table>\n";
2276 git_footer_html();
2277}
2278
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02002279sub git_project_index {
2280 my @projects = git_get_projects_list();
2281
2282 print $cgi->header(
2283 -type => 'text/plain',
2284 -charset => 'utf-8',
2285 -content_disposition => qq(inline; filename="index.aux"));
2286
2287 foreach my $pr (@projects) {
2288 if (!exists $pr->{'owner'}) {
2289 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2290 }
2291
2292 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2293 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2294 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2295 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2296 $path =~ s/ /\+/g;
2297 $owner =~ s/ /\+/g;
2298
2299 print "$path $owner\n";
2300 }
2301}
2302
Kay Sieversede5e102005-08-07 20:23:12 +02002303sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002304 my $descr = git_get_project_description($project) || "none";
2305 my $head = git_get_head_hash($project);
2306 my %co = parse_commit($head);
2307 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sieversede5e102005-08-07 20:23:12 +02002308
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002309 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002310
Jakub Narebski847e01f2006-08-14 02:05:47 +02002311 my $refs = git_get_references();
Kay Sieversede5e102005-08-07 20:23:12 +02002312 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002313 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002314
Kay Sievers19806692005-08-07 20:26:27 +02002315 print "<div class=\"title\">&nbsp;</div>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002316 print "<table cellspacing=\"0\">\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002317 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
Kay Sieversede5e102005-08-07 20:23:12 +02002318 "<tr><td>owner</td><td>$owner</td></tr>\n" .
Jakub Narebski19a87212006-08-15 23:03:17 +02002319 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002320 # use per project git URL list in $projectroot/$project/cloneurl
2321 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02002322 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002323 my @url_list = git_get_project_url_list($project);
2324 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2325 foreach my $git_url (@url_list) {
2326 next unless $git_url;
2327 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02002328 $url_tag = "";
2329 }
2330 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002331
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002332 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2333 git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002334 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002335 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversede5e102005-08-07 20:23:12 +02002336 close $fd;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002337 git_print_header_div('shortlog');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002338 git_shortlog_body(\@revlist, 0, 15, $refs,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002339 $cgi->a({-href => href(action=>"shortlog")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002340
Jakub Narebski847e01f2006-08-14 02:05:47 +02002341 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002342 if (defined @$taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002343 git_print_header_div('tags');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002344 git_tags_body($taglist, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002345 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002346 }
Kay Sievers0db37972005-08-07 20:24:35 +02002347
Jakub Narebski847e01f2006-08-14 02:05:47 +02002348 my $headlist = git_get_refs_list("refs/heads");
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002349 if (defined @$headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002350 git_print_header_div('heads');
Junio C Hamanob77aeb22006-07-31 19:12:18 -07002351 git_heads_body($headlist, $head, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002352 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02002353 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002354
Kay Sieversede5e102005-08-07 20:23:12 +02002355 git_footer_html();
2356}
2357
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002358sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002359 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002360 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002361 git_print_page_nav('','', $head,undef,$head);
2362 my %tag = parse_tag($hash);
2363 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002364 print "<div class=\"title_text\">\n" .
2365 "<table cellspacing=\"0\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002366 "<tr>\n" .
2367 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002368 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2369 $tag{'object'}) . "</td>\n" .
2370 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2371 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002372 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002373 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002374 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01002375 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002376 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2377 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2378 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002379 }
2380 print "</table>\n\n" .
2381 "</div>\n";
2382 print "<div class=\"page_body\">";
2383 my $comment = $tag{'comment'};
2384 foreach my $line (@$comment) {
Kay Sievers40c13812005-11-19 17:41:29 +01002385 print esc_html($line) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002386 }
2387 print "</div>\n";
2388 git_footer_html();
2389}
2390
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002391sub git_blame2 {
2392 my $fd;
2393 my $ftype;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302394
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302395 my ($have_blame) = gitweb_check_feature('blame');
2396 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302397 die_error('403 Permission denied', "Permission denied");
2398 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002399 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002400 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002401 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002402 my %co = parse_commit($hash_base)
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002403 or die_error(undef, "Reading commit failed");
2404 if (!defined $hash) {
2405 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2406 or die_error(undef, "Error looking up file");
2407 }
2408 $ftype = git_get_type($hash);
2409 if ($ftype !~ "blob") {
Jakub Narebskie484a2d2006-08-05 13:12:51 +02002410 die_error("400 Bad Request", "Object is not a blob");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002411 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002412 open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002413 or die_error(undef, "Open git-blame failed");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002414 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002415 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002416 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2417 "blob") .
2418 " | " .
2419 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2420 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002421 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2422 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002423 git_print_page_path($file_name, $ftype, $hash_base);
Luben Tuikov82f930d2006-08-04 15:09:59 -07002424 my @rev_color = (qw(light2 dark2));
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002425 my $num_colors = scalar(@rev_color);
2426 my $current_color = 0;
2427 my $last_rev;
Jakub Narebski59b9f612006-08-22 23:42:53 +02002428 print <<HTML;
2429<div class="page_body">
2430<table class="blame">
2431<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2432HTML
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002433 while (<$fd>) {
2434 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2435 my $full_rev = $1;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002436 my $rev = substr($full_rev, 0, 8);
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002437 my $lineno = $2;
2438 my $data = $3;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002439
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002440 if (!defined $last_rev) {
2441 $last_rev = $full_rev;
2442 } elsif ($last_rev ne $full_rev) {
2443 $last_rev = $full_rev;
2444 $current_color = ++$current_color % $num_colors;
2445 }
2446 print "<tr class=\"$rev_color[$current_color]\">\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002447 print "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002448 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2449 esc_html($rev)) . "</td>\n";
2450 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2451 esc_html($lineno) . "</a></td>\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002452 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2453 print "</tr>\n";
2454 }
2455 print "</table>\n";
2456 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002457 close $fd
2458 or print "Reading blob failed\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002459 git_footer_html();
2460}
2461
Florian Forstere34ef622006-06-11 17:45:19 +02002462sub git_blame {
2463 my $fd;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302464
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302465 my ($have_blame) = gitweb_check_feature('blame');
2466 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302467 die_error('403 Permission denied', "Permission denied");
2468 }
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002469 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002470 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002471 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002472 my %co = parse_commit($hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002473 or die_error(undef, "Reading commit failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002474 if (!defined $hash) {
2475 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002476 or die_error(undef, "Error lookup file");
Florian Forstere34ef622006-06-11 17:45:19 +02002477 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002478 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002479 or die_error(undef, "Open git-annotate failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002480 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002481 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002482 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2483 "blob") .
2484 " | " .
2485 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2486 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002487 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2488 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002489 git_print_page_path($file_name, 'blob', $hash_base);
Florian Forstere34ef622006-06-11 17:45:19 +02002490 print "<div class=\"page_body\">\n";
2491 print <<HTML;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002492<table class="blame">
Florian Forstere34ef622006-06-11 17:45:19 +02002493 <tr>
2494 <th>Commit</th>
2495 <th>Age</th>
2496 <th>Author</th>
2497 <th>Line</th>
2498 <th>Data</th>
2499 </tr>
2500HTML
2501 my @line_class = (qw(light dark));
2502 my $line_class_len = scalar (@line_class);
2503 my $line_class_num = $#line_class;
2504 while (my $line = <$fd>) {
2505 my $long_rev;
2506 my $short_rev;
2507 my $author;
2508 my $time;
2509 my $lineno;
2510 my $data;
2511 my $age;
2512 my $age_str;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002513 my $age_class;
Florian Forstere34ef622006-06-11 17:45:19 +02002514
2515 chomp $line;
2516 $line_class_num = ($line_class_num + 1) % $line_class_len;
2517
Jakub Narebski030b5202006-08-26 02:13:05 +02002518 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 +02002519 $long_rev = $1;
2520 $author = $2;
2521 $time = $3;
2522 $lineno = $4;
2523 $data = $5;
2524 } else {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002525 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
Florian Forstere34ef622006-06-11 17:45:19 +02002526 next;
2527 }
2528 $short_rev = substr ($long_rev, 0, 8);
2529 $age = time () - $time;
2530 $age_str = age_string ($age);
2531 $age_str =~ s/ /&nbsp;/g;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002532 $age_class = age_class($age);
Florian Forstere34ef622006-06-11 17:45:19 +02002533 $author = esc_html ($author);
2534 $author =~ s/ /&nbsp;/g;
Jakub Narebskif16db172006-08-06 02:08:31 +02002535
2536 $data = untabify($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002537 $data = esc_html ($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002538
2539 print <<HTML;
2540 <tr class="$line_class[$line_class_num]">
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002541 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002542 <td class="$age_class">$age_str</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002543 <td>$author</td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002544 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2545 <td class="pre">$data</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002546 </tr>
2547HTML
2548 } # while (my $line = <$fd>)
2549 print "</table>\n\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002550 close $fd
2551 or print "Reading blob failed.\n";
Florian Forstere34ef622006-06-11 17:45:19 +02002552 print "</div>";
2553 git_footer_html();
2554}
2555
Kay Sieversede5e102005-08-07 20:23:12 +02002556sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002557 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002558 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002559 git_print_page_nav('','', $head,undef,$head);
2560 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002561
Jakub Narebski847e01f2006-08-14 02:05:47 +02002562 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002563 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002564 git_tags_body($taglist);
Kay Sieversede5e102005-08-07 20:23:12 +02002565 }
Kay Sieversede5e102005-08-07 20:23:12 +02002566 git_footer_html();
2567}
2568
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002569sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002570 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002571 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002572 git_print_page_nav('','', $head,undef,$head);
2573 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002574
Jakub Narebski847e01f2006-08-14 02:05:47 +02002575 my $taglist = git_get_refs_list("refs/heads");
Kay Sievers0db37972005-08-07 20:24:35 +02002576 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002577 git_heads_body($taglist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02002578 }
Kay Sievers0db37972005-08-07 20:24:35 +02002579 git_footer_html();
2580}
2581
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002582sub git_blob_plain {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002583 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02002584
Luben Tuikovcff07712006-07-23 13:28:55 -07002585 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002586 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002587 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002588 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002589 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002590 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002591 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002592 }
Martin Waitz800764c2006-09-16 23:09:02 +02002593 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2594 # blobs defined by non-textual hash id's can be cached
2595 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002596 }
Martin Waitz800764c2006-09-16 23:09:02 +02002597
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002598 my $type = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002599 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicfd82662006-08-05 12:56:04 +02002600 or die_error(undef, "Couldn't cat $file_name, $hash");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002601
Jakub Narebski847e01f2006-08-14 02:05:47 +02002602 $type ||= blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002603
2604 # save as filename, even when no $file_name is given
2605 my $save_as = "$hash";
2606 if (defined $file_name) {
2607 $save_as = $file_name;
2608 } elsif ($type =~ m/^text\//) {
2609 $save_as .= '.txt';
2610 }
2611
Jakub Narebskif2e73302006-08-26 19:14:25 +02002612 print $cgi->header(
2613 -type => "$type",
2614 -expires=>$expires,
2615 -content_disposition => "inline; filename=\"$save_as\"");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002616 undef $/;
2617 binmode STDOUT, ':raw';
2618 print <$fd>;
2619 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2620 $/ = "\n";
2621 close $fd;
2622}
2623
Kay Sievers09bd7892005-08-07 20:21:23 +02002624sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002625 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02002626
Luben Tuikovcff07712006-07-23 13:28:55 -07002627 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002628 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002629 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002630 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002631 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002632 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002633 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002634 }
Martin Waitz800764c2006-09-16 23:09:02 +02002635 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2636 # blobs defined by non-textual hash id's can be cached
2637 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002638 }
Martin Waitz800764c2006-09-16 23:09:02 +02002639
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302640 my ($have_blame) = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002641 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002642 or die_error(undef, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002643 my $mimetype = blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002644 if ($mimetype !~ m/^text\//) {
2645 close $fd;
2646 return git_blob_plain($mimetype);
2647 }
Jakub Narebskif2e73302006-08-26 19:14:25 +02002648 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002649 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02002650 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02002651 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02002652 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002653 $formats_nav .=
2654 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2655 hash=>$hash, file_name=>$file_name)},
2656 "blame") .
2657 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02002658 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002659 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02002660 $cgi->a({-href => href(action=>"blob_plain",
2661 hash=>$hash, file_name=>$file_name)},
2662 "plain") .
2663 " | " .
2664 $cgi->a({-href => href(action=>"blob",
2665 hash_base=>"HEAD", file_name=>$file_name)},
2666 "head");
Kay Sievers93129442005-10-17 03:27:54 +02002667 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002668 $formats_nav .=
2669 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
Kay Sievers93129442005-10-17 03:27:54 +02002670 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002671 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2672 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02002673 } else {
2674 print "<div class=\"page_nav\">\n" .
2675 "<br/><br/></div>\n" .
2676 "<div class=\"title\">$hash</div>\n";
2677 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002678 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002679 print "<div class=\"page_body\">\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002680 my $nr;
2681 while (my $line = <$fd>) {
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002682 chomp $line;
Kay Sievers161332a2005-08-07 19:49:46 +02002683 $nr++;
Jakub Narebskif16db172006-08-06 02:08:31 +02002684 $line = untabify($line);
Jakub Narebski952c65f2006-08-22 16:52:50 +02002685 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2686 $nr, $nr, $nr, esc_html($line);
Kay Sievers161332a2005-08-07 19:49:46 +02002687 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002688 close $fd
2689 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02002690 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002691 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002692}
2693
2694sub git_tree {
Kay Sieversb87d78d2005-08-07 20:21:04 +02002695 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002696 $hash = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02002697 if (defined $file_name) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002698 my $base = $hash_base || $hash;
Kay Sievers09bd7892005-08-07 20:21:23 +02002699 $hash = git_get_hash_by_path($base, $file_name, "tree");
2700 }
Kay Sievers10dba282005-08-07 20:25:27 +02002701 if (!defined $hash_base) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002702 $hash_base = $hash;
Kay Sievers10dba282005-08-07 20:25:27 +02002703 }
Kay Sieverse925f382005-08-07 20:23:35 +02002704 }
Kay Sievers232ff552005-11-24 16:56:55 +01002705 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002706 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002707 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002708 my @entries = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002709 close $fd or die_error(undef, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01002710 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02002711
Jakub Narebski847e01f2006-08-14 02:05:47 +02002712 my $refs = git_get_references();
2713 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02002714 git_header_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002715 my $base = "";
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302716 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02002717 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002718 git_print_page_nav('tree','', $hash_base);
2719 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02002720 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02002721 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02002722 print "<div class=\"page_nav\">\n";
2723 print "<br/><br/></div>\n";
2724 print "<div class=\"title\">$hash</div>\n";
2725 }
Kay Sievers09bd7892005-08-07 20:21:23 +02002726 if (defined $file_name) {
Kay Sievers232ff552005-11-24 16:56:55 +01002727 $base = esc_html("$file_name/");
Kay Sievers09bd7892005-08-07 20:21:23 +02002728 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002729 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sieversfbb592a2005-08-07 20:12:11 +02002730 print "<div class=\"page_body\">\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02002731 print "<table cellspacing=\"0\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002732 my $alternate = 0;
Kay Sievers161332a2005-08-07 19:49:46 +02002733 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02002734 my %t = parse_ls_tree_line($line, -z => 1);
2735
Kay Sieversbddec012005-08-07 20:25:42 +02002736 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02002737 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002738 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02002739 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002740 }
2741 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002742
Jakub Narebskifa702002006-08-31 00:35:07 +02002743 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2744
Kay Sievers42f7eb92005-08-07 20:21:46 +02002745 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002746 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02002747 print "</table>\n" .
2748 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002749 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002750}
2751
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302752sub git_snapshot {
2753
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302754 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2755 my $have_snapshot = (defined $ctype && defined $suffix);
2756 if (!$have_snapshot) {
2757 die_error('403 Permission denied', "Permission denied");
2758 }
2759
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302760 if (!defined $hash) {
2761 $hash = git_get_head_hash($project);
2762 }
2763
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302764 my $filename = basename($project) . "-$hash.tar.$suffix";
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302765
2766 print $cgi->header(-type => 'application/x-tar',
Jakub Narebski134a69412006-08-22 16:55:34 +02002767 -content_encoding => $ctype,
2768 -content_disposition => "inline; filename=\"$filename\"",
Jakub Narebski952c65f2006-08-22 16:52:50 +02002769 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302770
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002771 my $git_command = git_cmd_str();
2772 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302773 die_error(undef, "Execute git-tar-tree failed.");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302774 binmode STDOUT, ':raw';
2775 print <$fd>;
2776 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2777 close $fd;
2778
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302779}
2780
Kay Sievers09bd7892005-08-07 20:21:23 +02002781sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002782 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002783 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02002784 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02002785 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02002786 if (!defined $page) {
2787 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02002788 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002789 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02002790
2791 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002792 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002793 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002794 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversea4a6df2005-08-07 20:26:49 +02002795 close $fd;
2796
Jakub Narebski847e01f2006-08-14 02:05:47 +02002797 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002798
2799 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002800 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002801
Kay Sieversb87d78d2005-08-07 20:21:04 +02002802 if (!@revlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002803 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002804
Jakub Narebski847e01f2006-08-14 02:05:47 +02002805 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02002806 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002807 }
Kay Sieversc994d622005-08-07 20:27:18 +02002808 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2809 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02002810 my $ref = format_ref_marker($refs, $commit);
2811 my %co = parse_commit($commit);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002812 next if !%co;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002813 my %ad = parse_date($co{'author_epoch'});
2814 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02002815 "<span class=\"age\">$co{'age_string'}</span>" .
2816 esc_html($co{'title'}) . $ref,
2817 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02002818 print "<div class=\"title_text\">\n" .
2819 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002820 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002821 " | " .
2822 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Kay Sieverseb282402005-08-07 20:21:34 +02002823 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02002824 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002825 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02002826 "</div>\n";
2827
2828 print "<div class=\"log_body\">\n";
2829 git_print_simplified_log($co{'comment'});
Kay Sievers09bd7892005-08-07 20:21:23 +02002830 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002831 }
2832 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002833}
2834
2835sub git_commit {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002836 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02002837 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002838 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02002839 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002840 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2841 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02002842
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002843 my $parent = $co{'parent'};
2844 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07002845 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02002846 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002847 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002848 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002849 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002850 close $fd or die_error(undef, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02002851
2852 # non-textual hash id's can be cached
2853 my $expires;
2854 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2855 $expires = "+1d";
2856 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002857 my $refs = git_get_references();
2858 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302859
2860 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2861 my $have_snapshot = (defined $ctype && defined $suffix);
2862
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002863 my $formats_nav = '';
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002864 if (defined $file_name && defined $co{'parent'}) {
2865 my $parent = $co{'parent'};
Jakub Narebski952c65f2006-08-22 16:52:50 +02002866 $formats_nav .=
2867 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2868 "blame");
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002869 }
Jakub Narebski594e2122006-07-31 02:21:52 +02002870 git_header_html(undef, $expires);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002871 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
Jakub Narebski952c65f2006-08-22 16:52:50 +02002872 $hash, $co{'tree'}, $hash,
2873 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002874
Kay Sieversb87d78d2005-08-07 20:21:04 +02002875 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002876 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002877 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002878 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002879 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02002880 print "<div class=\"title_text\">\n" .
Kay Sieversb87d78d2005-08-07 20:21:04 +02002881 "<table cellspacing=\"0\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002882 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02002883 "<tr>" .
2884 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02002885 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002886 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2887 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02002888 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002889 printf(" (%02d:%02d %s)",
2890 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002891 }
Kay Sieversbddec012005-08-07 20:25:42 +02002892 print "</td>" .
2893 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002894 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002895 print "<tr><td></td><td> $cd{'rfc2822'}" .
2896 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2897 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002898 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002899 print "<tr>" .
2900 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002901 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002902 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2903 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02002904 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002905 "<td class=\"link\">" .
2906 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2907 "tree");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302908 if ($have_snapshot) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002909 print " | " .
2910 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302911 }
2912 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002913 "</tr>\n";
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002914 my $parents = $co{'parents'};
Kay Sievers3e029292005-08-07 20:05:15 +02002915 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02002916 print "<tr>" .
2917 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002918 "<td class=\"sha1\">" .
2919 $cgi->a({-href => href(action=>"commit", hash=>$par),
2920 class => "list"}, $par) .
2921 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002922 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002923 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002924 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02002925 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02002926 "</td>" .
2927 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02002928 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02002929 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02002930 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002931
Kay Sieversfbb592a2005-08-07 20:12:11 +02002932 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002933 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002934 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002935
Jakub Narebskieee08902006-08-24 00:15:14 +02002936 git_difftree_body(\@difftree, $hash, $parent);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002937
Kay Sievers12a88f22005-08-07 20:02:47 +02002938 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002939}
2940
2941sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002942 my $format = shift || 'html';
2943
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002944 my $fd;
2945 my @difftree;
2946 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002947 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002948
2949 # preparing $fd and %diffinfo for git_patchset_body
2950 # new style URI
2951 if (defined $hash_base && defined $hash_parent_base) {
2952 if (defined $file_name) {
2953 # read raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002954 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002955 "--", $file_name
2956 or die_error(undef, "Open git-diff-tree failed");
2957 @difftree = map { chomp; $_ } <$fd>;
2958 close $fd
2959 or die_error(undef, "Reading git-diff-tree failed");
2960 @difftree
2961 or die_error('404 Not Found', "Blob diff not found");
2962
Jakub Narebski0aea3372006-08-27 23:45:26 +02002963 } elsif (defined $hash &&
2964 $hash =~ /[0-9a-fA-F]{40}/) {
2965 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002966
2967 # read filtered raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002968 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002969 or die_error(undef, "Open git-diff-tree failed");
2970 @difftree =
2971 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
2972 # $hash == to_id
2973 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2974 map { chomp; $_ } <$fd>;
2975 close $fd
2976 or die_error(undef, "Reading git-diff-tree failed");
2977 @difftree
2978 or die_error('404 Not Found', "Blob diff not found");
2979
2980 } else {
2981 die_error('404 Not Found', "Missing one of the blob diff parameters");
2982 }
2983
2984 if (@difftree > 1) {
2985 die_error('404 Not Found', "Ambiguous blob diff specification");
2986 }
2987
2988 %diffinfo = parse_difftree_raw_line($difftree[0]);
2989 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2990 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
2991
2992 $hash_parent ||= $diffinfo{'from_id'};
2993 $hash ||= $diffinfo{'to_id'};
2994
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002995 # non-textual hash id's can be cached
2996 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2997 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2998 $expires = '+1d';
2999 }
3000
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003001 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003002 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003003 '-p', $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003004 "--", $file_name
3005 or die_error(undef, "Open git-diff-tree failed");
3006 }
3007
3008 # old/legacy style URI
3009 if (!%diffinfo && # if new style URI failed
3010 defined $hash && defined $hash_parent) {
3011 # fake git-diff-tree raw output
3012 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3013 $diffinfo{'from_id'} = $hash_parent;
3014 $diffinfo{'to_id'} = $hash;
3015 if (defined $file_name) {
3016 if (defined $file_parent) {
3017 $diffinfo{'status'} = '2';
3018 $diffinfo{'from_file'} = $file_parent;
3019 $diffinfo{'to_file'} = $file_name;
3020 } else { # assume not renamed
3021 $diffinfo{'status'} = '1';
3022 $diffinfo{'from_file'} = $file_name;
3023 $diffinfo{'to_file'} = $file_name;
3024 }
3025 } else { # no filename given
3026 $diffinfo{'status'} = '2';
3027 $diffinfo{'from_file'} = $hash_parent;
3028 $diffinfo{'to_file'} = $hash;
3029 }
3030
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003031 # non-textual hash id's can be cached
3032 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3033 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3034 $expires = '+1d';
3035 }
3036
3037 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003038 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003039 or die_error(undef, "Open git-diff failed");
3040 } else {
3041 die_error('404 Not Found', "Missing one of the blob diff parameters")
3042 unless %diffinfo;
3043 }
3044
3045 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003046 if ($format eq 'html') {
3047 my $formats_nav =
3048 $cgi->a({-href => href(action=>"blobdiff_plain",
3049 hash=>$hash, hash_parent=>$hash_parent,
3050 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3051 file_name=>$file_name, file_parent=>$file_parent)},
3052 "plain");
3053 git_header_html(undef, $expires);
3054 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3055 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3056 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3057 } else {
3058 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3059 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3060 }
3061 if (defined $file_name) {
3062 git_print_page_path($file_name, "blob", $hash_base);
3063 } else {
3064 print "<div class=\"page_path\"></div>\n";
3065 }
3066
3067 } elsif ($format eq 'plain') {
3068 print $cgi->header(
3069 -type => 'text/plain',
3070 -charset => 'utf-8',
3071 -expires => $expires,
3072 -content_disposition => qq(inline; filename="${file_name}.patch"));
3073
3074 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3075
Kay Sievers09bd7892005-08-07 20:21:23 +02003076 } else {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003077 die_error(undef, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003078 }
3079
3080 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003081 if ($format eq 'html') {
3082 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003083
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003084 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3085 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003086
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003087 print "</div>\n"; # class="page_body"
3088 git_footer_html();
3089
3090 } else {
3091 while (my $line = <$fd>) {
3092 $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
3093 $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
3094
3095 print $line;
3096
3097 last if $line =~ m!^\+\+\+!;
3098 }
3099 local $/ = undef;
3100 print <$fd>;
3101 close $fd;
3102 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003103}
3104
Kay Sievers19806692005-08-07 20:26:27 +02003105sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003106 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003107}
3108
Kay Sievers09bd7892005-08-07 20:21:23 +02003109sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02003110 my $format = shift || 'html';
Jakub Narebski847e01f2006-08-14 02:05:47 +02003111 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02003112 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003113 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02003114 }
Kay Sieversbddec012005-08-07 20:25:42 +02003115 if (!defined $hash_parent) {
Jakub Narebskib5ff2cf2006-08-06 16:14:25 +02003116 $hash_parent = $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02003117 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003118
3119 # read commitdiff
3120 my $fd;
3121 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02003122 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003123 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskieee08902006-08-24 00:15:14 +02003124 "--patch-with-raw", "--full-index", $hash_parent, $hash
3125 or die_error(undef, "Open git-diff-tree failed");
3126
3127 while (chomp(my $line = <$fd>)) {
3128 # empty line ends raw part of diff-tree output
3129 last unless $line;
3130 push @difftree, $line;
3131 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003132
Jakub Narebskieee08902006-08-24 00:15:14 +02003133 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003134 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003135 '-p', $hash_parent, $hash
Jakub Narebskieee08902006-08-24 00:15:14 +02003136 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02003137
Jakub Narebskieee08902006-08-24 00:15:14 +02003138 } else {
3139 die_error(undef, "Unknown commitdiff format");
3140 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003141
Kay Sievers11044292005-10-19 03:18:45 +02003142 # non-textual hash id's can be cached
3143 my $expires;
3144 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3145 $expires = "+1d";
3146 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003147
Jakub Narebskieee08902006-08-24 00:15:14 +02003148 # write commit message
3149 if ($format eq 'html') {
3150 my $refs = git_get_references();
3151 my $ref = format_ref_marker($refs, $co{'id'});
3152 my $formats_nav =
3153 $cgi->a({-href => href(action=>"commitdiff_plain",
3154 hash=>$hash, hash_parent=>$hash_parent)},
3155 "plain");
Kay Sievers19806692005-08-07 20:26:27 +02003156
Jakub Narebskieee08902006-08-24 00:15:14 +02003157 git_header_html(undef, $expires);
3158 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3159 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003160 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02003161 print "<div class=\"page_body\">\n";
3162 print "<div class=\"log\">\n";
3163 git_print_simplified_log($co{'comment'}, 1); # skip title
3164 print "</div>\n"; # class="log"
Kay Sievers1b1cd422005-08-07 20:28:01 +02003165
Jakub Narebskieee08902006-08-24 00:15:14 +02003166 } elsif ($format eq 'plain') {
3167 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003168 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02003169 my $filename = basename($project) . "-$hash.patch";
3170
3171 print $cgi->header(
3172 -type => 'text/plain',
3173 -charset => 'utf-8',
3174 -expires => $expires,
3175 -content_disposition => qq(inline; filename="$filename"));
3176 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3177 print <<TEXT;
Jakub Narebski59b9f612006-08-22 23:42:53 +02003178From: $co{'author'}
3179Date: $ad{'rfc2822'} ($ad{'tz_local'})
3180Subject: $co{'title'}
3181TEXT
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003182 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02003183 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003184
Jakub Narebskieee08902006-08-24 00:15:14 +02003185 foreach my $line (@{$co{'comment'}}) {
3186 print "$line\n";
Kay Sievers19806692005-08-07 20:26:27 +02003187 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003188 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02003189 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003190
3191 # write patch
3192 if ($format eq 'html') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003193 git_difftree_body(\@difftree, $hash, $hash_parent);
3194 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02003195
Jakub Narebski157e43b2006-08-24 19:34:36 +02003196 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3197 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02003198 print "</div>\n"; # class="page_body"
3199 git_footer_html();
3200
3201 } elsif ($format eq 'plain') {
3202 local $/ = undef;
3203 print <$fd>;
3204 close $fd
3205 or print "Reading git-diff-tree failed\n";
3206 }
3207}
3208
3209sub git_commitdiff_plain {
3210 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003211}
3212
Kay Sievers09bd7892005-08-07 20:21:23 +02003213sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07003214 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003215 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02003216 }
Jakub Narebski8be68352006-09-11 00:36:04 +02003217 if (!defined $page) {
3218 $page = 0;
3219 }
Luben Tuikov63433102006-07-23 13:31:15 -07003220 my $ftype;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003221 my %co = parse_commit($hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02003222 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003223 die_error(undef, "Unknown commit object");
Kay Sievers2ae100d2005-08-07 20:17:00 +02003224 }
Jakub Narebski8be68352006-09-11 00:36:04 +02003225
Jakub Narebski847e01f2006-08-14 02:05:47 +02003226 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02003227 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3228
Luben Tuikov93d5f062006-07-23 13:30:08 -07003229 if (!defined $hash && defined $file_name) {
3230 $hash = git_get_hash_by_path($hash_base, $file_name);
3231 }
Luben Tuikovcff07712006-07-23 13:28:55 -07003232 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07003233 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07003234 }
Kay Sievers10dba282005-08-07 20:25:27 +02003235
Junio C Hamanocdd40372006-06-30 18:54:32 -07003236 open my $fd, "-|",
Jakub Narebski8be68352006-09-11 00:36:04 +02003237 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3238 or die_error(undef, "Open git-rev-list-failed");
3239 my @revlist = map { chomp; $_ } <$fd>;
3240 close $fd
3241 or die_error(undef, "Reading git-rev-list failed");
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003242
Jakub Narebski8be68352006-09-11 00:36:04 +02003243 my $paging_nav = '';
3244 if ($page > 0) {
3245 $paging_nav .=
3246 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3247 file_name=>$file_name)},
3248 "first");
3249 $paging_nav .= " &sdot; " .
3250 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3251 file_name=>$file_name, page=>$page-1),
3252 -accesskey => "p", -title => "Alt-p"}, "prev");
3253 } else {
3254 $paging_nav .= "first";
3255 $paging_nav .= " &sdot; prev";
3256 }
3257 if ($#revlist >= (100 * ($page+1)-1)) {
3258 $paging_nav .= " &sdot; " .
3259 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3260 file_name=>$file_name, page=>$page+1),
3261 -accesskey => "n", -title => "Alt-n"}, "next");
3262 } else {
3263 $paging_nav .= " &sdot; next";
3264 }
3265 my $next_link = '';
3266 if ($#revlist >= (100 * ($page+1)-1)) {
3267 $next_link =
3268 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3269 file_name=>$file_name, page=>$page+1),
3270 -title => "Alt-n"}, "next");
3271 }
Jakub Narebski581860e2006-08-14 02:09:08 +02003272
Jakub Narebski8be68352006-09-11 00:36:04 +02003273 git_header_html();
3274 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3275 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3276 git_print_page_path($file_name, $ftype, $hash_base);
3277
3278 git_history_body(\@revlist, ($page * 100), $#revlist,
3279 $refs, $hash_base, $ftype, $next_link);
3280
Kay Sieversd51e9022005-08-07 20:16:07 +02003281 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02003282}
Kay Sievers19806692005-08-07 20:26:27 +02003283
3284sub git_search {
3285 if (!defined $searchtext) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003286 die_error(undef, "Text field empty");
Kay Sievers19806692005-08-07 20:26:27 +02003287 }
3288 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003289 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003290 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003291 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02003292 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003293 die_error(undef, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02003294 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02003295
Kay Sieversc994d622005-08-07 20:27:18 +02003296 my $commit_search = 1;
3297 my $author_search = 0;
3298 my $committer_search = 0;
3299 my $pickaxe_search = 0;
3300 if ($searchtext =~ s/^author\\://i) {
3301 $author_search = 1;
3302 } elsif ($searchtext =~ s/^committer\\://i) {
3303 $committer_search = 1;
3304 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3305 $commit_search = 0;
3306 $pickaxe_search = 1;
Jakub Narebski04f7a942006-09-11 00:29:27 +02003307
3308 # pickaxe may take all resources of your box and run for several minutes
3309 # with every query - so decide by yourself how public you make this feature
3310 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3311 if (!$have_pickaxe) {
3312 die_error('403 Permission denied', "Permission denied");
3313 }
Kay Sieversc994d622005-08-07 20:27:18 +02003314 }
Kay Sievers19806692005-08-07 20:26:27 +02003315 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003316 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3317 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Kay Sievers19806692005-08-07 20:26:27 +02003318
Kay Sievers19806692005-08-07 20:26:27 +02003319 print "<table cellspacing=\"0\">\n";
Kay Sievers19806692005-08-07 20:26:27 +02003320 my $alternate = 0;
Kay Sieversc994d622005-08-07 20:27:18 +02003321 if ($commit_search) {
3322 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003323 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
Kay Sieversc994d622005-08-07 20:27:18 +02003324 while (my $commit_text = <$fd>) {
3325 if (!grep m/$searchtext/i, $commit_text) {
Kay Sievers19806692005-08-07 20:26:27 +02003326 next;
3327 }
Kay Sieversc994d622005-08-07 20:27:18 +02003328 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3329 next;
Kay Sievers19806692005-08-07 20:26:27 +02003330 }
Kay Sieversc994d622005-08-07 20:27:18 +02003331 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3332 next;
3333 }
3334 my @commit_lines = split "\n", $commit_text;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003335 my %co = parse_commit(undef, \@commit_lines);
Kay Sieversc994d622005-08-07 20:27:18 +02003336 if (!%co) {
3337 next;
3338 }
3339 if ($alternate) {
3340 print "<tr class=\"dark\">\n";
3341 } else {
3342 print "<tr class=\"light\">\n";
3343 }
3344 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003345 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003346 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003347 "<td>" .
Jakub Narebski63e42202006-08-22 12:38:59 +02003348 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3349 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003350 my $comment = $co{'comment'};
3351 foreach my $line (@$comment) {
3352 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
Kay Sievers40c13812005-11-19 17:41:29 +01003353 my $lead = esc_html($1) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003354 $lead = chop_str($lead, 30, 10);
Kay Sievers40c13812005-11-19 17:41:29 +01003355 my $match = esc_html($2) || "";
3356 my $trail = esc_html($3) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003357 $trail = chop_str($trail, 30, 10);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00003358 my $text = "$lead<span class=\"match\">$match</span>$trail";
Kay Sieversc994d622005-08-07 20:27:18 +02003359 print chop_str($text, 80, 5) . "<br/>\n";
3360 }
3361 }
3362 print "</td>\n" .
3363 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003364 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003365 " | " .
3366 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003367 print "</td>\n" .
3368 "</tr>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003369 }
Kay Sieversc994d622005-08-07 20:27:18 +02003370 close $fd;
3371 }
3372
3373 if ($pickaxe_search) {
3374 $/ = "\n";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003375 my $git_command = git_cmd_str();
3376 open my $fd, "-|", "$git_command rev-list $hash | " .
3377 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
Kay Sieversc994d622005-08-07 20:27:18 +02003378 undef %co;
3379 my @files;
3380 while (my $line = <$fd>) {
3381 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3382 my %set;
3383 $set{'file'} = $6;
3384 $set{'from_id'} = $3;
3385 $set{'to_id'} = $4;
3386 $set{'id'} = $set{'to_id'};
3387 if ($set{'id'} =~ m/0{40}/) {
3388 $set{'id'} = $set{'from_id'};
3389 }
3390 if ($set{'id'} =~ m/0{40}/) {
3391 next;
3392 }
3393 push @files, \%set;
Kay Sievers53b89d82005-08-12 22:12:58 +02003394 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
Kay Sieversc994d622005-08-07 20:27:18 +02003395 if (%co) {
3396 if ($alternate) {
3397 print "<tr class=\"dark\">\n";
3398 } else {
3399 print "<tr class=\"light\">\n";
3400 }
3401 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003402 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003403 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003404 "<td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003405 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3406 -class => "list subject"},
Jakub Narebski63e42202006-08-22 12:38:59 +02003407 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003408 while (my $setref = shift @files) {
3409 my %set = %$setref;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003410 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3411 hash=>$set{'id'}, file_name=>$set{'file'}),
3412 -class => "list"},
3413 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
Kay Sieversc994d622005-08-07 20:27:18 +02003414 "<br/>\n";
3415 }
3416 print "</td>\n" .
3417 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003418 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003419 " | " .
3420 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003421 print "</td>\n" .
3422 "</tr>\n";
3423 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003424 %co = parse_commit($1);
Kay Sieversc994d622005-08-07 20:27:18 +02003425 }
3426 }
3427 close $fd;
Kay Sievers19806692005-08-07 20:26:27 +02003428 }
3429 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003430 git_footer_html();
3431}
3432
3433sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003434 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003435 if (!defined $hash) {
3436 $hash = $head;
3437 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02003438 if (!defined $page) {
3439 $page = 0;
3440 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003441 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02003442
3443 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003444 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003445 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02003446 my @revlist = map { chomp; $_ } <$fd>;
Kay Sievers19806692005-08-07 20:26:27 +02003447 close $fd;
Kay Sieversea4a6df2005-08-07 20:26:49 +02003448
Jakub Narebski847e01f2006-08-14 02:05:47 +02003449 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003450 my $next_link = '';
3451 if ($#revlist >= (100 * ($page+1)-1)) {
3452 $next_link =
Martin Waitz756d2f02006-08-17 00:28:36 +02003453 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003454 -title => "Alt-n"}, "next");
3455 }
3456
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003457
3458 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003459 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3460 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003461
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003462 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3463
Kay Sievers19806692005-08-07 20:26:27 +02003464 git_footer_html();
3465}
Jakub Narebski717b8312006-07-31 21:22:15 +02003466
3467## ......................................................................
3468## feeds (RSS, OPML)
3469
3470sub git_rss {
3471 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003472 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003473 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003474 my @revlist = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003475 close $fd or die_error(undef, "Reading git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003476 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003477 print <<XML;
3478<?xml version="1.0" encoding="utf-8"?>
3479<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3480<channel>
3481<title>$project $my_uri $my_url</title>
3482<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3483<description>$project log</description>
3484<language>en</language>
3485XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003486
3487 for (my $i = 0; $i <= $#revlist; $i++) {
3488 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02003489 my %co = parse_commit($commit);
Jakub Narebski717b8312006-07-31 21:22:15 +02003490 # we read 150, we always show 30 and the ones more recent than 48 hours
3491 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3492 last;
3493 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003494 my %cd = parse_date($co{'committer_epoch'});
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003495 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003496 $co{'parent'}, $co{'id'}
3497 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003498 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003499 close $fd
3500 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003501 print "<item>\n" .
3502 "<title>" .
3503 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3504 "</title>\n" .
3505 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3506 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3507 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3508 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3509 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3510 "<content:encoded>" .
3511 "<![CDATA[\n";
3512 my $comment = $co{'comment'};
3513 foreach my $line (@$comment) {
3514 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3515 print "$line<br/>\n";
3516 }
3517 print "<br/>\n";
3518 foreach my $line (@difftree) {
3519 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3520 next;
3521 }
3522 my $file = validate_input(unquote($7));
3523 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3524 print "$file<br/>\n";
3525 }
3526 print "]]>\n" .
3527 "</content:encoded>\n" .
3528 "</item>\n";
3529 }
3530 print "</channel></rss>";
3531}
3532
3533sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003534 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02003535
3536 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003537 print <<XML;
3538<?xml version="1.0" encoding="utf-8"?>
3539<opml version="1.0">
3540<head>
3541 <title>$site_name Git OPML Export</title>
3542</head>
3543<body>
3544<outline text="git RSS feeds">
3545XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003546
3547 foreach my $pr (@list) {
3548 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003549 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02003550 if (!defined $head) {
3551 next;
3552 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003553 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02003554 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02003555 if (!%co) {
3556 next;
3557 }
3558
3559 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3560 my $rss = "$my_url?p=$proj{'path'};a=rss";
3561 my $html = "$my_url?p=$proj{'path'};a=summary";
3562 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3563 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02003564 print <<XML;
3565</outline>
3566</body>
3567</opml>
3568XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003569}