blob: 0fb86385c94a72c08fce4bc0b2c54c1461ad2bc6 [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) {
Kay Sieversc91da262005-09-03 14:50:33 +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;
277 $path_info =~ s,(^/|/$),,gs;
278 $path_info = validate_input($path_info);
279 return if !$path_info;
Martin Waitzdd702352006-09-16 23:08:32 +0200280 $project = $path_info;
281 while ($project && !-e "$projectroot/$project/HEAD") {
282 $project =~ s,/*[^/]*$,,;
283 }
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200284 if (!$project ||
285 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
286 ($strict_export && !project_in_list($project))) {
287 undef $project;
288 return;
Martin Waitzdd702352006-09-16 23:08:32 +0200289 }
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200290 # do not change any parameters if an action is given using the query string
291 return if $action;
Martin Waitzdd702352006-09-16 23:08:32 +0200292 if ($path_info =~ m,^$project/([^/]+)/(.+)$,) {
293 # we got "project.git/branch/filename"
294 $action ||= "blob_plain";
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200295 $hash_base ||= validate_input($1);
296 $file_name ||= validate_input($2);
Martin Waitzdd702352006-09-16 23:08:32 +0200297 } elsif ($path_info =~ m,^$project/([^/]+)$,) {
298 # we got "project.git/branch"
299 $action ||= "shortlog";
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200300 $hash ||= validate_input($1);
Martin Waitzdd702352006-09-16 23:08:32 +0200301 }
302}
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200303evaluate_path_info();
Martin Waitzdd702352006-09-16 23:08:32 +0200304
Matthias Lederhofer645927c2006-09-17 15:29:48 +0200305# path to the current git repository
306our $git_dir;
307$git_dir = "$projectroot/$project" if $project;
Martin Waitzdd702352006-09-16 23:08:32 +0200308
Jakub Narebski717b8312006-07-31 21:22:15 +0200309# dispatch
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200310my %actions = (
311 "blame" => \&git_blame2,
312 "blobdiff" => \&git_blobdiff,
313 "blobdiff_plain" => \&git_blobdiff_plain,
314 "blob" => \&git_blob,
315 "blob_plain" => \&git_blob_plain,
316 "commitdiff" => \&git_commitdiff,
317 "commitdiff_plain" => \&git_commitdiff_plain,
318 "commit" => \&git_commit,
319 "heads" => \&git_heads,
320 "history" => \&git_history,
321 "log" => \&git_log,
322 "rss" => \&git_rss,
323 "search" => \&git_search,
324 "shortlog" => \&git_shortlog,
325 "summary" => \&git_summary,
326 "tag" => \&git_tag,
327 "tags" => \&git_tags,
328 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530329 "snapshot" => \&git_snapshot,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200330 # those below don't need $project
331 "opml" => \&git_opml,
332 "project_list" => \&git_project_list,
Jakub Narebskifc2b2be2006-09-15 04:56:03 +0200333 "project_index" => \&git_project_index,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200334);
335
Jakub Narebski77a153f2006-08-22 16:59:20 +0200336if (defined $project) {
337 $action ||= 'summary';
338} else {
339 $action ||= 'project_list';
340}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200341if (!defined($actions{$action})) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200342 die_error(undef, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200343}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200344$actions{$action}->();
345exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200346
Jakub Narebski717b8312006-07-31 21:22:15 +0200347## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200348## action links
349
350sub href(%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200351 my %params = @_;
352
353 my @mapping = (
Martin Waitz06a9d862006-08-16 00:23:50 +0200354 project => "p",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200355 action => "a",
Martin Waitz06a9d862006-08-16 00:23:50 +0200356 file_name => "f",
Martin Waitz5c95fab2006-08-17 00:28:38 +0200357 file_parent => "fp",
Martin Waitz06a9d862006-08-16 00:23:50 +0200358 hash => "h",
359 hash_parent => "hp",
360 hash_base => "hb",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200361 hash_parent_base => "hpb",
Martin Waitz06a9d862006-08-16 00:23:50 +0200362 page => "pg",
Jakub Narebskia1565c42006-09-15 19:30:34 +0200363 order => "o",
Martin Waitz06a9d862006-08-16 00:23:50 +0200364 searchtext => "s",
365 );
Jakub Narebski498fe002006-08-22 19:05:25 +0200366 my %mapping = @mapping;
Martin Waitz06a9d862006-08-16 00:23:50 +0200367
Jakub Narebskia1565c42006-09-15 19:30:34 +0200368 $params{'project'} = $project unless exists $params{'project'};
Martin Waitz06a9d862006-08-16 00:23:50 +0200369
Jakub Narebski498fe002006-08-22 19:05:25 +0200370 my @result = ();
371 for (my $i = 0; $i < @mapping; $i += 2) {
372 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
373 if (defined $params{$name}) {
374 push @result, $symbol . "=" . esc_param($params{$name});
375 }
376 }
377 return "$my_uri?" . join(';', @result);
Martin Waitz06a9d862006-08-16 00:23:50 +0200378}
379
380
381## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200382## validation, quoting/unquoting and escaping
383
384sub validate_input {
385 my $input = shift;
386
387 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
388 return $input;
389 }
390 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
391 return undef;
392 }
393 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
394 return undef;
395 }
396 return $input;
397}
398
Kay Sievers232ff552005-11-24 16:56:55 +0100399# quote unsafe chars, but keep the slash, even when it's not
400# correct, but quoted slashes look too horrible in bookmarks
401sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100402 my $str = shift;
Kay Sievers232ff552005-11-24 16:56:55 +0100403 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +0100404 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +0100405 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +0100406 return $str;
407}
408
Kay Sievers232ff552005-11-24 16:56:55 +0100409# replace invalid utf8 character with SUBSTITUTION sequence
Kay Sievers40c13812005-11-19 17:41:29 +0100410sub esc_html {
411 my $str = shift;
Kay Sievers40c13812005-11-19 17:41:29 +0100412 $str = decode("utf8", $str, Encode::FB_DEFAULT);
Kay Sievers10bb9032005-11-23 04:26:40 +0100413 $str = escapeHTML($str);
Jakub Narebski3dc13832006-07-30 15:02:27 +0200414 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
Kay Sievers40c13812005-11-19 17:41:29 +0100415 return $str;
416}
417
Kay Sievers232ff552005-11-24 16:56:55 +0100418# git may return quoted and escaped filenames
419sub unquote {
420 my $str = shift;
421 if ($str =~ m/^"(.*)"$/) {
422 $str = $1;
423 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
424 }
425 return $str;
426}
427
Jakub Narebskif16db172006-08-06 02:08:31 +0200428# escape tabs (convert tabs to spaces)
429sub untabify {
430 my $line = shift;
431
432 while ((my $pos = index($line, "\t")) != -1) {
433 if (my $count = (8 - ($pos % 8))) {
434 my $spaces = ' ' x $count;
435 $line =~ s/\t/$spaces/;
436 }
437 }
438
439 return $line;
440}
441
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200442sub project_in_list {
443 my $project = shift;
444 my @list = git_get_projects_list();
445 return @list && scalar(grep { $_->{'path'} eq $project } @list);
446}
447
Jakub Narebski717b8312006-07-31 21:22:15 +0200448## ----------------------------------------------------------------------
449## HTML aware string manipulation
450
451sub chop_str {
452 my $str = shift;
453 my $len = shift;
454 my $add_len = shift || 10;
455
456 # allow only $len chars, but don't cut a word if it would fit in $add_len
457 # if it doesn't fit, cut it if it's still longer than the dots we would add
458 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
459 my $body = $1;
460 my $tail = $2;
461 if (length($tail) > 4) {
462 $tail = " ...";
463 $body =~ s/&[^;]*$//; # remove chopped character entities
464 }
465 return "$body$tail";
466}
467
468## ----------------------------------------------------------------------
469## functions returning short strings
470
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000471# CSS class for given age value (in seconds)
472sub age_class {
473 my $age = shift;
474
475 if ($age < 60*60*2) {
476 return "age0";
477 } elsif ($age < 60*60*24*2) {
478 return "age1";
479 } else {
480 return "age2";
481 }
482}
483
Jakub Narebski717b8312006-07-31 21:22:15 +0200484# convert age in seconds to "nn units ago" string
485sub age_string {
486 my $age = shift;
487 my $age_str;
488
489 if ($age > 60*60*24*365*2) {
490 $age_str = (int $age/60/60/24/365);
491 $age_str .= " years ago";
492 } elsif ($age > 60*60*24*(365/12)*2) {
493 $age_str = int $age/60/60/24/(365/12);
494 $age_str .= " months ago";
495 } elsif ($age > 60*60*24*7*2) {
496 $age_str = int $age/60/60/24/7;
497 $age_str .= " weeks ago";
498 } elsif ($age > 60*60*24*2) {
499 $age_str = int $age/60/60/24;
500 $age_str .= " days ago";
501 } elsif ($age > 60*60*2) {
502 $age_str = int $age/60/60;
503 $age_str .= " hours ago";
504 } elsif ($age > 60*2) {
505 $age_str = int $age/60;
506 $age_str .= " min ago";
507 } elsif ($age > 2) {
508 $age_str = int $age;
509 $age_str .= " sec ago";
510 } else {
511 $age_str .= " right now";
512 }
513 return $age_str;
514}
515
516# convert file mode in octal to symbolic file mode string
517sub mode_str {
518 my $mode = oct shift;
519
520 if (S_ISDIR($mode & S_IFMT)) {
521 return 'drwxr-xr-x';
522 } elsif (S_ISLNK($mode)) {
523 return 'lrwxrwxrwx';
524 } elsif (S_ISREG($mode)) {
525 # git cares only about the executable bit
526 if ($mode & S_IXUSR) {
527 return '-rwxr-xr-x';
528 } else {
529 return '-rw-r--r--';
530 };
531 } else {
532 return '----------';
533 }
534}
535
536# convert file mode in octal to file type string
537sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +0200538 my $mode = shift;
539
540 if ($mode !~ m/^[0-7]+$/) {
541 return $mode;
542 } else {
543 $mode = oct $mode;
544 }
Jakub Narebski717b8312006-07-31 21:22:15 +0200545
546 if (S_ISDIR($mode & S_IFMT)) {
547 return "directory";
548 } elsif (S_ISLNK($mode)) {
549 return "symlink";
550 } elsif (S_ISREG($mode)) {
551 return "file";
552 } else {
553 return "unknown";
554 }
555}
556
557## ----------------------------------------------------------------------
558## functions returning short HTML fragments, or transforming HTML fragments
559## which don't beling to other sections
560
561# format line of commit message or tag comment
562sub format_log_line_html {
563 my $line = shift;
564
565 $line = esc_html($line);
566 $line =~ s/ /&nbsp;/g;
567 if ($line =~ m/([0-9a-fA-F]{40})/) {
568 my $hash_text = $1;
569 if (git_get_type($hash_text) eq "commit") {
Jakub Narebski952c65f2006-08-22 16:52:50 +0200570 my $link =
571 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
572 -class => "text"}, $hash_text);
Jakub Narebski717b8312006-07-31 21:22:15 +0200573 $line =~ s/$hash_text/$link/;
574 }
575 }
576 return $line;
577}
578
579# format marker of refs pointing to given object
Jakub Narebski847e01f2006-08-14 02:05:47 +0200580sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +0200581 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200582 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +0200583
584 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200585 foreach my $ref (@{$refs->{$id}}) {
586 my ($type, $name) = qw();
587 # e.g. tags/v2.6.11 or heads/next
588 if ($ref =~ m!^(.*?)s?/(.*)$!) {
589 $type = $1;
590 $name = $2;
591 } else {
592 $type = "ref";
593 $name = $ref;
594 }
595
596 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
597 }
598 }
599
600 if ($markers) {
601 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +0200602 } else {
603 return "";
604 }
605}
606
Jakub Narebski17d07442006-08-14 02:08:27 +0200607# format, perhaps shortened and with markers, title line
608sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200609 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +0200610 $extra = '' unless defined($extra);
611
612 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +0200613 return $cgi->a({-href => $href, -class => "list subject",
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200614 -title => $long},
Jakub Narebski17d07442006-08-14 02:08:27 +0200615 esc_html($short) . $extra);
616 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +0200617 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +0200618 esc_html($long) . $extra);
619 }
620}
621
Jakub Narebskieee08902006-08-24 00:15:14 +0200622sub format_diff_line {
623 my $line = shift;
624 my $char = substr($line, 0, 1);
625 my $diff_class = "";
626
627 chomp $line;
628
629 if ($char eq '+') {
630 $diff_class = " add";
631 } elsif ($char eq "-") {
632 $diff_class = " rem";
633 } elsif ($char eq "@") {
634 $diff_class = " chunk_header";
635 } elsif ($char eq "\\") {
Jakub Narebskiaf33ef22006-08-24 01:58:49 +0200636 $diff_class = " incomplete";
Jakub Narebskieee08902006-08-24 00:15:14 +0200637 }
638 $line = untabify($line);
639 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
640}
641
Jakub Narebski717b8312006-07-31 21:22:15 +0200642## ----------------------------------------------------------------------
643## git utility subroutines, invoking git commands
644
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200645# returns path to the core git executable and the --git-dir parameter as list
646sub git_cmd {
647 return $GIT, '--git-dir='.$git_dir;
648}
649
650# returns path to the core git executable and the --git-dir parameter as string
651sub git_cmd_str {
652 return join(' ', git_cmd());
653}
654
Jakub Narebski717b8312006-07-31 21:22:15 +0200655# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +0200656sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +0200657 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200658 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200659 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200660 $git_dir = "$projectroot/$project";
661 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +0200662 my $head = <$fd>;
663 close $fd;
664 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
665 $retval = $1;
666 }
667 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200668 if (defined $o_git_dir) {
669 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200670 }
671 return $retval;
672}
673
674# get type of given object
675sub git_get_type {
676 my $hash = shift;
677
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200678 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200679 my $type = <$fd>;
680 close $fd or return;
681 chomp $type;
682 return $type;
683}
684
685sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530686 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +0200687
688 return unless ($key);
689 $key =~ s/^gitweb\.//;
690 return if ($key =~ m/\W/);
691
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200692 my @x = (git_cmd(), 'repo-config');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530693 if (defined $type) { push @x, $type; }
694 push @x, "--get";
695 push @x, "gitweb.$key";
696 my $val = qx(@x);
697 chomp $val;
Jakub Narebski717b8312006-07-31 21:22:15 +0200698 return ($val);
699}
700
Jakub Narebski717b8312006-07-31 21:22:15 +0200701# get hash of given path at given ref
702sub git_get_hash_by_path {
703 my $base = shift;
704 my $path = shift || return undef;
705
706 my $tree = $base;
707
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200708 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200709 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +0200710 my $line = <$fd>;
711 close $fd or return undef;
712
713 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
714 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
715 return $3;
716}
717
718## ......................................................................
719## git utility functions, directly accessing git repository
720
Jakub Narebski847e01f2006-08-14 02:05:47 +0200721sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +0200722 my $path = shift;
723
724 open my $fd, "$projectroot/$path/description" or return undef;
725 my $descr = <$fd>;
726 close $fd;
727 chomp $descr;
728 return $descr;
729}
730
Jakub Narebskie79ca7c2006-08-16 14:50:34 +0200731sub git_get_project_url_list {
732 my $path = shift;
733
734 open my $fd, "$projectroot/$path/cloneurl" or return undef;
735 my @git_project_url_list = map { chomp; $_ } <$fd>;
736 close $fd;
737
738 return wantarray ? @git_project_url_list : \@git_project_url_list;
739}
740
Jakub Narebski847e01f2006-08-14 02:05:47 +0200741sub git_get_projects_list {
Jakub Narebski717b8312006-07-31 21:22:15 +0200742 my @list;
743
744 if (-d $projects_list) {
745 # search in directory
746 my $dir = $projects_list;
Jakub Narebskic0011ff2006-09-14 22:18:59 +0200747 my $pfxlen = length("$dir");
748
749 File::Find::find({
750 follow_fast => 1, # follow symbolic links
751 dangling_symlinks => 0, # ignore dangling symlinks, silently
752 wanted => sub {
753 # skip project-list toplevel, if we get it.
754 return if (m!^[/.]$!);
755 # only directories can be git repositories
756 return unless (-d $_);
757
758 my $subdir = substr($File::Find::name, $pfxlen + 1);
759 # we check related file in $projectroot
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200760 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
761 -e "$projectroot/$subdir/$export_ok")) {
Jakub Narebskic0011ff2006-09-14 22:18:59 +0200762 push @list, { path => $subdir };
763 $File::Find::prune = 1;
764 }
765 },
766 }, "$dir");
767
Jakub Narebski717b8312006-07-31 21:22:15 +0200768 } elsif (-f $projects_list) {
769 # read from file(url-encoded):
770 # 'git%2Fgit.git Linus+Torvalds'
771 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
772 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
773 open my ($fd), $projects_list or return undef;
774 while (my $line = <$fd>) {
775 chomp $line;
776 my ($path, $owner) = split ' ', $line;
777 $path = unescape($path);
778 $owner = unescape($owner);
779 if (!defined $path) {
780 next;
781 }
Matthias Lederhofer32f4aac2006-09-17 00:31:01 +0200782 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
783 -e "$projectroot/$path/$export_ok")) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200784 my $pr = {
785 path => $path,
786 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
787 };
788 push @list, $pr
789 }
790 }
791 close $fd;
792 }
793 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
794 return @list;
795}
796
Jakub Narebski1e0cf032006-08-14 02:10:06 +0200797sub git_get_project_owner {
798 my $project = shift;
799 my $owner;
800
801 return undef unless $project;
802
803 # read from file (url-encoded):
804 # 'git%2Fgit.git Linus+Torvalds'
805 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
806 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
807 if (-f $projects_list) {
808 open (my $fd , $projects_list);
809 while (my $line = <$fd>) {
810 chomp $line;
811 my ($pr, $ow) = split ' ', $line;
812 $pr = unescape($pr);
813 $ow = unescape($ow);
814 if ($pr eq $project) {
815 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
816 last;
817 }
818 }
819 close $fd;
820 }
821 if (!defined $owner) {
822 $owner = get_file_owner("$projectroot/$project");
823 }
824
825 return $owner;
826}
827
Jakub Narebski847e01f2006-08-14 02:05:47 +0200828sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +0200829 my $type = shift || "";
830 my %refs;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200831 my $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +0200832 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
833 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200834 if (-f "$projectroot/$project/info/refs") {
835 open $fd, "$projectroot/$project/info/refs"
836 or return;
837 } else {
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200838 open $fd, "-|", git_cmd(), "ls-remote", "."
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200839 or return;
840 }
841
Jakub Narebski717b8312006-07-31 21:22:15 +0200842 while (my $line = <$fd>) {
843 chomp $line;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200844 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200845 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200846 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +0200847 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200848 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +0200849 }
850 }
851 }
852 close $fd or return;
853 return \%refs;
854}
855
Jakub Narebski56a322f2006-08-24 19:41:23 +0200856sub git_get_rev_name_tags {
857 my $hash = shift || return undef;
858
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200859 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +0200860 or return;
861 my $name_rev = <$fd>;
862 close $fd;
863
864 if ($name_rev =~ m|^$hash tags/(.*)$|) {
865 return $1;
866 } else {
867 # catches also '$hash undefined' output
868 return undef;
869 }
870}
871
Jakub Narebski717b8312006-07-31 21:22:15 +0200872## ----------------------------------------------------------------------
873## parse to hash functions
874
Jakub Narebski847e01f2006-08-14 02:05:47 +0200875sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +0200876 my $epoch = shift;
877 my $tz = shift || "-0000";
878
879 my %date;
880 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
881 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
882 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
883 $date{'hour'} = $hour;
884 $date{'minute'} = $min;
885 $date{'mday'} = $mday;
886 $date{'day'} = $days[$wday];
887 $date{'month'} = $months[$mon];
Jakub Narebski952c65f2006-08-22 16:52:50 +0200888 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
889 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
890 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
891 $mday, $months[$mon], $hour ,$min;
Jakub Narebski717b8312006-07-31 21:22:15 +0200892
893 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
894 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
895 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
896 $date{'hour_local'} = $hour;
897 $date{'minute_local'} = $min;
898 $date{'tz_local'} = $tz;
899 return %date;
900}
901
Jakub Narebski847e01f2006-08-14 02:05:47 +0200902sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +0200903 my $tag_id = shift;
904 my %tag;
905 my @comment;
906
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200907 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200908 $tag{'id'} = $tag_id;
909 while (my $line = <$fd>) {
910 chomp $line;
911 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
912 $tag{'object'} = $1;
913 } elsif ($line =~ m/^type (.+)$/) {
914 $tag{'type'} = $1;
915 } elsif ($line =~ m/^tag (.+)$/) {
916 $tag{'name'} = $1;
917 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
918 $tag{'author'} = $1;
919 $tag{'epoch'} = $2;
920 $tag{'tz'} = $3;
921 } elsif ($line =~ m/--BEGIN/) {
922 push @comment, $line;
923 last;
924 } elsif ($line eq "") {
925 last;
926 }
927 }
928 push @comment, <$fd>;
929 $tag{'comment'} = \@comment;
930 close $fd or return;
931 if (!defined $tag{'name'}) {
932 return
933 };
934 return %tag
935}
936
Jakub Narebski847e01f2006-08-14 02:05:47 +0200937sub parse_commit {
Jakub Narebski717b8312006-07-31 21:22:15 +0200938 my $commit_id = shift;
939 my $commit_text = shift;
940
941 my @commit_lines;
942 my %co;
943
944 if (defined $commit_text) {
945 @commit_lines = @$commit_text;
946 } else {
947 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200948 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
Jakub Narebski952c65f2006-08-22 16:52:50 +0200949 or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200950 @commit_lines = split '\n', <$fd>;
951 close $fd or return;
952 $/ = "\n";
953 pop @commit_lines;
954 }
955 my $header = shift @commit_lines;
956 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
957 return;
958 }
959 ($co{'id'}, my @parents) = split ' ', $header;
960 $co{'parents'} = \@parents;
961 $co{'parent'} = $parents[0];
962 while (my $line = shift @commit_lines) {
963 last if $line eq "\n";
964 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
965 $co{'tree'} = $1;
966 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
967 $co{'author'} = $1;
968 $co{'author_epoch'} = $2;
969 $co{'author_tz'} = $3;
970 if ($co{'author'} =~ m/^([^<]+) </) {
971 $co{'author_name'} = $1;
972 } else {
973 $co{'author_name'} = $co{'author'};
974 }
975 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
976 $co{'committer'} = $1;
977 $co{'committer_epoch'} = $2;
978 $co{'committer_tz'} = $3;
979 $co{'committer_name'} = $co{'committer'};
980 $co{'committer_name'} =~ s/ <.*//;
981 }
982 }
983 if (!defined $co{'tree'}) {
984 return;
985 };
986
987 foreach my $title (@commit_lines) {
988 $title =~ s/^ //;
989 if ($title ne "") {
990 $co{'title'} = chop_str($title, 80, 5);
991 # remove leading stuff of merges to make the interesting part visible
992 if (length($title) > 50) {
993 $title =~ s/^Automatic //;
994 $title =~ s/^merge (of|with) /Merge ... /i;
995 if (length($title) > 50) {
996 $title =~ s/(http|rsync):\/\///;
997 }
998 if (length($title) > 50) {
999 $title =~ s/(master|www|rsync)\.//;
1000 }
1001 if (length($title) > 50) {
1002 $title =~ s/kernel.org:?//;
1003 }
1004 if (length($title) > 50) {
1005 $title =~ s/\/pub\/scm//;
1006 }
1007 }
1008 $co{'title_short'} = chop_str($title, 50, 5);
1009 last;
1010 }
1011 }
1012 # remove added spaces
1013 foreach my $line (@commit_lines) {
1014 $line =~ s/^ //;
1015 }
1016 $co{'comment'} = \@commit_lines;
1017
1018 my $age = time - $co{'committer_epoch'};
1019 $co{'age'} = $age;
1020 $co{'age_string'} = age_string($age);
1021 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1022 if ($age > 60*60*24*7*2) {
1023 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1024 $co{'age_string_age'} = $co{'age_string'};
1025 } else {
1026 $co{'age_string_date'} = $co{'age_string'};
1027 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1028 }
1029 return %co;
1030}
1031
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001032# parse ref from ref_file, given by ref_id, with given type
1033sub parse_ref {
1034 my $ref_file = shift;
1035 my $ref_id = shift;
1036 my $type = shift || git_get_type($ref_id);
1037 my %ref_item;
1038
1039 $ref_item{'type'} = $type;
1040 $ref_item{'id'} = $ref_id;
1041 $ref_item{'epoch'} = 0;
1042 $ref_item{'age'} = "unknown";
1043 if ($type eq "tag") {
1044 my %tag = parse_tag($ref_id);
1045 $ref_item{'comment'} = $tag{'comment'};
1046 if ($tag{'type'} eq "commit") {
1047 my %co = parse_commit($tag{'object'});
1048 $ref_item{'epoch'} = $co{'committer_epoch'};
1049 $ref_item{'age'} = $co{'age_string'};
1050 } elsif (defined($tag{'epoch'})) {
1051 my $age = time - $tag{'epoch'};
1052 $ref_item{'epoch'} = $tag{'epoch'};
1053 $ref_item{'age'} = age_string($age);
1054 }
1055 $ref_item{'reftype'} = $tag{'type'};
1056 $ref_item{'name'} = $tag{'name'};
1057 $ref_item{'refid'} = $tag{'object'};
1058 } elsif ($type eq "commit"){
1059 my %co = parse_commit($ref_id);
1060 $ref_item{'reftype'} = "commit";
1061 $ref_item{'name'} = $ref_file;
1062 $ref_item{'title'} = $co{'title'};
1063 $ref_item{'refid'} = $ref_id;
1064 $ref_item{'epoch'} = $co{'committer_epoch'};
1065 $ref_item{'age'} = $co{'age_string'};
1066 } else {
1067 $ref_item{'reftype'} = $type;
1068 $ref_item{'name'} = $ref_file;
1069 $ref_item{'refid'} = $ref_id;
1070 }
1071
1072 return %ref_item;
1073}
1074
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001075# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02001076sub parse_difftree_raw_line {
1077 my $line = shift;
1078 my %res;
1079
1080 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1081 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1082 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1083 $res{'from_mode'} = $1;
1084 $res{'to_mode'} = $2;
1085 $res{'from_id'} = $3;
1086 $res{'to_id'} = $4;
1087 $res{'status'} = $5;
1088 $res{'similarity'} = $6;
1089 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001090 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02001091 } else {
1092 $res{'file'} = unquote($7);
1093 }
1094 }
1095 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02001096 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1097 $res{'commit'} = $1;
1098 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02001099
1100 return wantarray ? %res : \%res;
1101}
1102
Jakub Narebskicb849b42006-08-31 00:32:15 +02001103# parse line of git-ls-tree output
1104sub parse_ls_tree_line ($;%) {
1105 my $line = shift;
1106 my %opts = @_;
1107 my %res;
1108
1109 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1110 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1111
1112 $res{'mode'} = $1;
1113 $res{'type'} = $2;
1114 $res{'hash'} = $3;
1115 if ($opts{'-z'}) {
1116 $res{'name'} = $4;
1117 } else {
1118 $res{'name'} = unquote($4);
1119 }
1120
1121 return wantarray ? %res : \%res;
1122}
1123
Jakub Narebski717b8312006-07-31 21:22:15 +02001124## ......................................................................
1125## parse to array of hashes functions
1126
Jakub Narebski847e01f2006-08-14 02:05:47 +02001127sub git_get_refs_list {
Jakub Narebski717b8312006-07-31 21:22:15 +02001128 my $ref_dir = shift;
1129 my @reflist;
1130
1131 my @refs;
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001132 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1133 or return;
1134 while (my $line = <$fd>) {
1135 chomp $line;
1136 if ($line =~ m/^([0-9a-fA-F]{40})\t$ref_dir\/?([^\^]+)$/) {
1137 push @refs, { hash => $1, name => $2 };
1138 } elsif ($line =~ m/^[0-9a-fA-F]{40}\t$ref_dir\/?(.*)\^\{\}$/ &&
1139 $1 eq $refs[-1]{'name'}) {
1140 # most likely a tag is followed by its peeled
1141 # (deref) one, and when that happens we know the
1142 # previous one was of type 'tag'.
1143 $refs[-1]{'type'} = "tag";
Jakub Narebski717b8312006-07-31 21:22:15 +02001144 }
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001145 }
1146 close $fd;
Junio C Hamano7a13b992006-07-31 19:18:34 -07001147
Jakub Narebskic83a77e2006-09-15 03:43:28 +02001148 foreach my $ref (@refs) {
1149 my $ref_file = $ref->{'name'};
1150 my $ref_id = $ref->{'hash'};
1151
1152 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001153 my %ref_item = parse_ref($ref_file, $ref_id, $type);
Jakub Narebski717b8312006-07-31 21:22:15 +02001154
1155 push @reflist, \%ref_item;
1156 }
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001157 # sort refs by age
Jakub Narebski717b8312006-07-31 21:22:15 +02001158 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1159 return \@reflist;
1160}
1161
1162## ----------------------------------------------------------------------
1163## filesystem-related functions
1164
1165sub get_file_owner {
1166 my $path = shift;
1167
1168 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1169 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1170 if (!defined $gcos) {
1171 return undef;
1172 }
1173 my $owner = $gcos;
1174 $owner =~ s/[,;].*$//;
1175 return decode("utf8", $owner, Encode::FB_DEFAULT);
1176}
1177
1178## ......................................................................
1179## mimetype related functions
1180
1181sub mimetype_guess_file {
1182 my $filename = shift;
1183 my $mimemap = shift;
1184 -r $mimemap or return undef;
1185
1186 my %mimemap;
1187 open(MIME, $mimemap) or return undef;
1188 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02001189 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02001190 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07001191 if (defined $exts) {
1192 my @exts = split(/\s+/, $exts);
1193 foreach my $ext (@exts) {
1194 $mimemap{$ext} = $mime;
1195 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001196 }
1197 }
1198 close(MIME);
1199
1200 $filename =~ /\.(.*?)$/;
1201 return $mimemap{$1};
1202}
1203
1204sub mimetype_guess {
1205 my $filename = shift;
1206 my $mime;
1207 $filename =~ /\./ or return undef;
1208
1209 if ($mimetypes_file) {
1210 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02001211 if ($file !~ m!^/!) { # if it is relative path
1212 # it is relative to project
1213 $file = "$projectroot/$project/$file";
1214 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001215 $mime = mimetype_guess_file($filename, $file);
1216 }
1217 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1218 return $mime;
1219}
1220
Jakub Narebski847e01f2006-08-14 02:05:47 +02001221sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02001222 my $fd = shift;
1223 my $filename = shift;
1224
1225 if ($filename) {
1226 my $mime = mimetype_guess($filename);
1227 $mime and return $mime;
1228 }
1229
1230 # just in case
1231 return $default_blob_plain_mimetype unless $fd;
1232
1233 if (-T $fd) {
1234 return 'text/plain' .
1235 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1236 } elsif (! $filename) {
1237 return 'application/octet-stream';
1238 } elsif ($filename =~ m/\.png$/i) {
1239 return 'image/png';
1240 } elsif ($filename =~ m/\.gif$/i) {
1241 return 'image/gif';
1242 } elsif ($filename =~ m/\.jpe?g$/i) {
1243 return 'image/jpeg';
1244 } else {
1245 return 'application/octet-stream';
1246 }
1247}
1248
1249## ======================================================================
1250## functions printing HTML: header, footer, error page
1251
Kay Sievers12a88f22005-08-07 20:02:47 +02001252sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02001253 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02001254 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001255
Alp Toker49da1da2006-07-11 21:10:26 +01001256 my $title = "$site_name git";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001257 if (defined $project) {
1258 $title .= " - $project";
1259 if (defined $action) {
1260 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00001261 if (defined $file_name) {
1262 $title .= " - $file_name";
1263 if ($action eq "tree" && $file_name !~ m|/$|) {
1264 $title .= "/";
1265 }
1266 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02001267 }
1268 }
Alp Tokerf6801d62006-07-11 11:19:34 +01001269 my $content_type;
1270 # require explicit support from the UA if we are to send the page as
1271 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1272 # we have to do this because MSIE sometimes globs '*/*', pretending to
1273 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02001274 if (defined $cgi->http('HTTP_ACCEPT') &&
1275 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1276 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01001277 $content_type = 'application/xhtml+xml';
1278 } else {
1279 $content_type = 'text/html';
1280 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02001281 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1282 -status=> $status, -expires => $expires);
Kay Sieversa59d4af2005-08-07 20:15:44 +02001283 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02001284<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02001285<!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 +02001286<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001287<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02001288<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02001289<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01001290<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001291<meta name="generator" content="gitweb/$version git/$git_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02001292<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02001293<title>$title</title>
Jakub Narebskiae20de52006-06-21 09:48:03 +02001294<link rel="stylesheet" type="text/css" href="$stylesheet"/>
Kay Sievers161332a2005-08-07 19:49:46 +02001295EOF
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001296 if (defined $project) {
1297 printf('<link rel="alternate" title="%s log" '.
1298 'href="%s" type="application/rss+xml"/>'."\n",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001299 esc_param($project), href(action=>"rss"));
Jakub Narebski9d0734a2006-09-15 11:11:33 +02001300 } else {
1301 printf('<link rel="alternate" title="%s projects list" '.
1302 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1303 $site_name, href(project=>undef, action=>"project_index"));
1304 printf('<link rel="alternate" title="%s projects logs" '.
1305 'href="%s" type="text/x-opml"/>'."\n",
1306 $site_name, href(project=>undef, action=>"opml"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001307 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02001308 if (defined $favicon) {
1309 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1310 }
Jakub Narebski10161352006-08-05 13:18:58 +02001311
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001312 print "</head>\n" .
1313 "<body>\n" .
Jakub Narebski10161352006-08-05 13:18:58 +02001314 "<div class=\"page_header\">\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001315 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
Martin Waitz281f2f62006-07-31 00:38:39 +02001316 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
Kay Sievers19806692005-08-07 20:26:27 +02001317 "</a>\n";
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +09001318 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001319 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001320 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02001321 if (defined $action) {
1322 print " / $action";
1323 }
Kay Sievers19806692005-08-07 20:26:27 +02001324 print "\n";
1325 if (!defined $searchtext) {
1326 $searchtext = "";
1327 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02001328 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03001329 if (defined $hash_base) {
1330 $search_hash = $hash_base;
1331 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02001332 $search_hash = $hash;
1333 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02001334 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02001335 }
Kay Sievers19806692005-08-07 20:26:27 +02001336 $cgi->param("a", "search");
Kay Sieversc39e47d2005-09-17 03:00:21 +02001337 $cgi->param("h", $search_hash);
Kay Sievers353347b2005-11-14 05:47:18 +01001338 print $cgi->startform(-method => "get", -action => $my_uri) .
Kay Sieversc994d622005-08-07 20:27:18 +02001339 "<div class=\"search\">\n" .
1340 $cgi->hidden(-name => "p") . "\n" .
1341 $cgi->hidden(-name => "a") . "\n" .
Kay Sieversc39e47d2005-09-17 03:00:21 +02001342 $cgi->hidden(-name => "h") . "\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001343 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1344 "</div>" .
1345 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001346 }
1347 print "</div>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02001348}
1349
Kay Sievers12a88f22005-08-07 20:02:47 +02001350sub git_footer_html {
Kay Sievers6191f8e2005-08-07 20:19:56 +02001351 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001352 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02001353 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02001354 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01001355 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02001356 }
Jakub Narebskia1565c42006-09-15 19:30:34 +02001357 print $cgi->a({-href => href(action=>"rss"),
1358 -class => "rss_logo"}, "RSS") . "\n";
Kay Sieversc994d622005-08-07 20:27:18 +02001359 } else {
Jakub Narebskia1565c42006-09-15 19:30:34 +02001360 print $cgi->a({-href => href(project=>undef, action=>"opml"),
Jakub Narebski9d0734a2006-09-15 11:11:33 +02001361 -class => "rss_logo"}, "OPML") . " ";
1362 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1363 -class => "rss_logo"}, "TXT") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02001364 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02001365 print "</div>\n" .
1366 "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02001367 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02001368}
1369
Kay Sievers061cc7c2005-08-07 20:15:57 +02001370sub die_error {
1371 my $status = shift || "403 Forbidden";
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02001372 my $error = shift || "Malformed query, file missing or permission denied";
Kay Sievers664f4cc2005-08-07 20:17:19 +02001373
Kay Sieversa59d4af2005-08-07 20:15:44 +02001374 git_header_html($status);
Jakub Narebski59b9f612006-08-22 23:42:53 +02001375 print <<EOF;
1376<div class="page_body">
1377<br /><br />
1378$status - $error
1379<br />
1380</div>
1381EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02001382 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02001383 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001384}
1385
Jakub Narebski717b8312006-07-31 21:22:15 +02001386## ----------------------------------------------------------------------
1387## functions printing or outputting HTML: navigation
1388
Jakub Narebski847e01f2006-08-14 02:05:47 +02001389sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001390 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1391 $extra = '' if !defined $extra; # pager or formats
1392
1393 my @navs = qw(summary shortlog log commit commitdiff tree);
1394 if ($suppress) {
1395 @navs = grep { $_ ne $suppress } @navs;
1396 }
1397
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001398 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001399 if (defined $head) {
1400 for (qw(commit commitdiff)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001401 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001402 }
1403 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1404 for (qw(shortlog log)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001405 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001406 }
1407 }
1408 }
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001409 $arg{tree}{hash} = $treehead if defined $treehead;
1410 $arg{tree}{hash_base} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001411
1412 print "<div class=\"page_nav\">\n" .
1413 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001414 map { $_ eq $current ?
1415 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1416 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02001417 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001418 "</div>\n";
1419}
1420
Jakub Narebski847e01f2006-08-14 02:05:47 +02001421sub format_paging_nav {
Jakub Narebski6855f422006-07-30 20:31:00 +02001422 my ($action, $hash, $head, $page, $nrevs) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02001423 my $paging_nav;
1424
1425
1426 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001427 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001428 } else {
1429 $paging_nav .= "HEAD";
1430 }
1431
1432 if ($page > 0) {
1433 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001434 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001435 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001436 } else {
1437 $paging_nav .= " &sdot; prev";
1438 }
1439
1440 if ($nrevs >= (100 * ($page+1)-1)) {
1441 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001442 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001443 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001444 } else {
1445 $paging_nav .= " &sdot; next";
1446 }
1447
1448 return $paging_nav;
1449}
1450
Jakub Narebski717b8312006-07-31 21:22:15 +02001451## ......................................................................
1452## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02001453
Jakub Narebski847e01f2006-08-14 02:05:47 +02001454sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02001455 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001456 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02001457
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001458 $args{action} = $action;
1459 $args{hash} = $hash if $hash;
1460 $args{hash_base} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02001461
1462 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001463 $cgi->a({-href => href(%args), -class => "title"},
1464 $title ? $title : $action) .
1465 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02001466}
1467
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001468#sub git_print_authorship (\%) {
1469sub git_print_authorship {
1470 my $co = shift;
1471
Jakub Narebskia44465c2006-08-28 23:17:31 +02001472 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001473 print "<div class=\"author_date\">" .
1474 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02001475 " [$ad{'rfc2822'}";
1476 if ($ad{'hour_local'} < 6) {
1477 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1478 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1479 } else {
1480 printf(" (%02d:%02d %s)",
1481 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1482 }
1483 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001484}
1485
Jakub Narebski717b8312006-07-31 21:22:15 +02001486sub git_print_page_path {
1487 my $name = shift;
1488 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001489 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01001490
Jakub Narebski717b8312006-07-31 21:22:15 +02001491 if (!defined $name) {
Jakub Narebski63e42202006-08-22 12:38:59 +02001492 print "<div class=\"page_path\">/</div>\n";
Jakub Narebski762c7202006-09-04 18:17:58 +02001493 } else {
1494 my @dirname = split '/', $name;
1495 my $basename = pop @dirname;
1496 my $fullname = '';
1497
Jakub Narebski63e42202006-08-22 12:38:59 +02001498 print "<div class=\"page_path\">";
Jakub Narebski762c7202006-09-04 18:17:58 +02001499 foreach my $dir (@dirname) {
1500 $fullname .= $dir . '/';
1501 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1502 hash_base=>$hb),
1503 -title => $fullname}, esc_html($dir));
1504 print "/";
1505 }
1506 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02001507 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02001508 hash_base=>$hb),
1509 -title => $name}, esc_html($basename));
1510 } elsif (defined $type && $type eq 'tree') {
1511 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1512 hash_base=>$hb),
1513 -title => $name}, esc_html($basename));
1514 print "/";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001515 } else {
Jakub Narebski762c7202006-09-04 18:17:58 +02001516 print esc_html($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001517 }
Jakub Narebski63e42202006-08-22 12:38:59 +02001518 print "<br/></div>\n";
Kay Sievers8ed23e12005-08-07 20:05:44 +02001519 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001520}
1521
Jakub Narebskib7f92532006-08-28 14:48:10 +02001522# sub git_print_log (\@;%) {
1523sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02001524 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001525 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001526
Jakub Narebskib7f92532006-08-28 14:48:10 +02001527 if ($opts{'-remove_title'}) {
1528 # remove title, i.e. first line of log
1529 shift @$log;
1530 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02001531 # remove leading empty lines
1532 while (defined $log->[0] && $log->[0] eq "") {
1533 shift @$log;
1534 }
1535
1536 # print log
1537 my $signoff = 0;
1538 my $empty = 0;
1539 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02001540 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1541 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02001542 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001543 if (! $opts{'-remove_signoff'}) {
1544 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1545 next;
1546 } else {
1547 # remove signoff lines
1548 next;
1549 }
1550 } else {
1551 $signoff = 0;
1552 }
1553
Jakub Narebskid16d0932006-08-17 11:21:23 +02001554 # print only one empty line
1555 # do not print empty line after signoff
1556 if ($line eq "") {
1557 next if ($empty || $signoff);
1558 $empty = 1;
1559 } else {
1560 $empty = 0;
1561 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02001562
1563 print format_log_line_html($line) . "<br/>\n";
1564 }
1565
1566 if ($opts{'-final_empty_line'}) {
1567 # end with single empty line
1568 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001569 }
1570}
1571
1572sub git_print_simplified_log {
1573 my $log = shift;
1574 my $remove_title = shift;
1575
Jakub Narebskib7f92532006-08-28 14:48:10 +02001576 git_print_log($log,
1577 -final_empty_line=> 1,
Jakub Narebskib7f92532006-08-28 14:48:10 +02001578 -remove_title => $remove_title);
Jakub Narebskid16d0932006-08-17 11:21:23 +02001579}
1580
Jakub Narebskifa702002006-08-31 00:35:07 +02001581# print tree entry (row of git_tree), but without encompassing <tr> element
1582sub git_print_tree_entry {
1583 my ($t, $basedir, $hash_base, $have_blame) = @_;
1584
1585 my %base_key = ();
1586 $base_key{hash_base} = $hash_base if defined $hash_base;
1587
1588 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1589 if ($t->{'type'} eq "blob") {
1590 print "<td class=\"list\">" .
1591 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1592 file_name=>"$basedir$t->{'name'}", %base_key),
1593 -class => "list"}, esc_html($t->{'name'})) .
1594 "</td>\n" .
1595 "<td class=\"link\">" .
1596 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1597 file_name=>"$basedir$t->{'name'}", %base_key)},
1598 "blob");
1599 if ($have_blame) {
1600 print " | " .
1601 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1602 file_name=>"$basedir$t->{'name'}", %base_key)},
1603 "blame");
1604 }
1605 if (defined $hash_base) {
1606 print " | " .
1607 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1608 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1609 "history");
1610 }
1611 print " | " .
1612 $cgi->a({-href => href(action=>"blob_plain",
1613 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1614 "raw") .
1615 "</td>\n";
1616
1617 } elsif ($t->{'type'} eq "tree") {
1618 print "<td class=\"list\">" .
1619 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1620 file_name=>"$basedir$t->{'name'}", %base_key)},
1621 esc_html($t->{'name'})) .
1622 "</td>\n" .
1623 "<td class=\"link\">" .
1624 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1625 file_name=>"$basedir$t->{'name'}", %base_key)},
1626 "tree");
1627 if (defined $hash_base) {
1628 print " | " .
1629 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1630 file_name=>"$basedir$t->{'name'}")},
1631 "history");
1632 }
1633 print "</td>\n";
1634 }
1635}
1636
Jakub Narebski717b8312006-07-31 21:22:15 +02001637## ......................................................................
1638## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02001639
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001640sub git_difftree_body {
Jakub Narebskieee08902006-08-24 00:15:14 +02001641 my ($difftree, $hash, $parent) = @_;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001642
1643 print "<div class=\"list_head\">\n";
1644 if ($#{$difftree} > 10) {
1645 print(($#{$difftree} + 1) . " files changed:\n");
1646 }
1647 print "</div>\n";
1648
1649 print "<table class=\"diff_tree\">\n";
1650 my $alternate = 0;
Jakub Narebskib4657e72006-08-28 14:48:14 +02001651 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001652 foreach my $line (@{$difftree}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001653 my %diff = parse_difftree_raw_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001654
1655 if ($alternate) {
1656 print "<tr class=\"dark\">\n";
1657 } else {
1658 print "<tr class=\"light\">\n";
1659 }
1660 $alternate ^= 1;
1661
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001662 my ($to_mode_oct, $to_mode_str, $to_file_type);
1663 my ($from_mode_oct, $from_mode_str, $from_file_type);
1664 if ($diff{'to_mode'} ne ('0' x 6)) {
1665 $to_mode_oct = oct $diff{'to_mode'};
1666 if (S_ISREG($to_mode_oct)) { # only for regular file
1667 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001668 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001669 $to_file_type = file_type($diff{'to_mode'});
1670 }
1671 if ($diff{'from_mode'} ne ('0' x 6)) {
1672 $from_mode_oct = oct $diff{'from_mode'};
1673 if (S_ISREG($to_mode_oct)) { # only for regular file
1674 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1675 }
1676 $from_file_type = file_type($diff{'from_mode'});
1677 }
1678
1679 if ($diff{'status'} eq "A") { # created
1680 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1681 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1682 $mode_chng .= "]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001683 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001684 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1685 hash_base=>$hash, file_name=>$diff{'file'}),
1686 -class => "list"}, esc_html($diff{'file'})) .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001687 "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001688 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001689 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001690 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1691 hash_base=>$hash, file_name=>$diff{'file'})},
Jakub Narebskib4657e72006-08-28 14:48:14 +02001692 "blob");
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001693 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001694 # link to patch
1695 $patchno++;
1696 print " | " .
1697 $cgi->a({-href => "#patch$patchno"}, "patch");
1698 }
1699 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001700
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001701 } elsif ($diff{'status'} eq "D") { # deleted
1702 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001703 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001704 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1705 hash_base=>$parent, file_name=>$diff{'file'}),
1706 -class => "list"}, esc_html($diff{'file'})) .
1707 "</td>\n" .
1708 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001709 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001710 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1711 hash_base=>$parent, file_name=>$diff{'file'})},
1712 "blob") .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001713 " | ";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001714 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001715 # link to patch
1716 $patchno++;
1717 print " | " .
1718 $cgi->a({-href => "#patch$patchno"}, "patch");
1719 }
1720 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski09052552006-08-26 23:33:58 +02001721 file_name=>$diff{'file'})},
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001722 "history") .
1723 "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001724
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001725 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001726 my $mode_chnge = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001727 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1728 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1729 if ($from_file_type != $to_file_type) {
1730 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001731 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001732 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1733 if ($from_mode_str && $to_mode_str) {
1734 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1735 } elsif ($to_mode_str) {
1736 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001737 }
1738 }
1739 $mode_chnge .= "]</span>\n";
1740 }
1741 print "<td>";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001742 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski420e92f2006-08-24 23:53:54 +02001743 print $cgi->a({-href => href(action=>"blobdiff",
1744 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1745 hash_base=>$hash, hash_parent_base=>$parent,
1746 file_name=>$diff{'file'}),
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001747 -class => "list"}, esc_html($diff{'file'}));
1748 } else { # only mode changed
1749 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1750 hash_base=>$hash, file_name=>$diff{'file'}),
1751 -class => "list"}, esc_html($diff{'file'}));
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001752 }
1753 print "</td>\n" .
1754 "<td>$mode_chnge</td>\n" .
1755 "<td class=\"link\">" .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001756 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1757 hash_base=>$hash, file_name=>$diff{'file'})},
1758 "blob");
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001759 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001760 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001761 # link to patch
1762 $patchno++;
1763 print " | " .
1764 $cgi->a({-href => "#patch$patchno"}, "patch");
1765 } else {
1766 print " | " .
1767 $cgi->a({-href => href(action=>"blobdiff",
1768 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1769 hash_base=>$hash, hash_parent_base=>$parent,
1770 file_name=>$diff{'file'})},
1771 "diff");
1772 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001773 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001774 print " | " .
1775 $cgi->a({-href => href(action=>"history",
1776 hash_base=>$hash, file_name=>$diff{'file'})},
1777 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001778 print "</td>\n";
1779
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001780 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1781 my %status_name = ('R' => 'moved', 'C' => 'copied');
1782 my $nstatus = $status_name{$diff{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001783 my $mode_chng = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001784 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1785 # mode also for directories, so we cannot use $to_mode_str
1786 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001787 }
1788 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001789 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1790 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1791 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1792 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1793 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1794 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1795 -class => "list"}, esc_html($diff{'from_file'})) .
1796 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001797 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001798 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1799 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1800 "blob");
1801 if ($diff{'to_id'} ne $diff{'from_id'}) {
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001802 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001803 # link to patch
1804 $patchno++;
1805 print " | " .
1806 $cgi->a({-href => "#patch$patchno"}, "patch");
1807 } else {
1808 print " | " .
1809 $cgi->a({-href => href(action=>"blobdiff",
1810 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1811 hash_base=>$hash, hash_parent_base=>$parent,
1812 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1813 "diff");
1814 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001815 }
1816 print "</td>\n";
1817
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001818 } # we should not encounter Unmerged (U) or Unknown (X) status
1819 print "</tr>\n";
1820 }
1821 print "</table>\n";
1822}
1823
Jakub Narebskieee08902006-08-24 00:15:14 +02001824sub git_patchset_body {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001825 my ($fd, $difftree, $hash, $hash_parent) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001826
1827 my $patch_idx = 0;
1828 my $in_header = 0;
1829 my $patch_found = 0;
Jakub Narebskife875852006-08-25 20:59:39 +02001830 my $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02001831
1832 print "<div class=\"patchset\">\n";
1833
Jakub Narebski157e43b2006-08-24 19:34:36 +02001834 LINE:
Jakub Narebskic8a99d72006-08-26 19:14:23 +02001835 while (my $patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001836 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02001837
1838 if ($patch_line =~ m/^diff /) { # "git diff" header
1839 # beginning of patch (in patchset)
1840 if ($patch_found) {
1841 # close previous patch
1842 print "</div>\n"; # class="patch"
1843 } else {
1844 # first patch in patchset
1845 $patch_found = 1;
1846 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02001847 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001848
Jakub Narebskife875852006-08-25 20:59:39 +02001849 if (ref($difftree->[$patch_idx]) eq "HASH") {
1850 $diffinfo = $difftree->[$patch_idx];
1851 } else {
1852 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1853 }
1854 $patch_idx++;
Jakub Narebskieee08902006-08-24 00:15:14 +02001855
1856 # for now, no extended header, hence we skip empty patches
1857 # companion to next LINE if $in_header;
Jakub Narebskife875852006-08-25 20:59:39 +02001858 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
Jakub Narebskieee08902006-08-24 00:15:14 +02001859 $in_header = 1;
1860 next LINE;
1861 }
1862
Jakub Narebskife875852006-08-25 20:59:39 +02001863 if ($diffinfo->{'status'} eq "A") { # added
1864 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001865 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001866 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1867 $diffinfo->{'to_id'}) . "(new)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001868 "</div>\n"; # class="diff_info"
1869
Jakub Narebskife875852006-08-25 20:59:39 +02001870 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1871 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001872 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001873 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1874 $diffinfo->{'from_id'}) . "(deleted)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001875 "</div>\n"; # class="diff_info"
1876
Jakub Narebskife875852006-08-25 20:59:39 +02001877 } elsif ($diffinfo->{'status'} eq "R" || # renamed
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001878 $diffinfo->{'status'} eq "C" || # copied
1879 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
Jakub Narebskieee08902006-08-24 00:15:14 +02001880 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001881 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001882 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001883 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1884 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001885 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001886 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001887 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001888 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1889 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001890 print "</div>\n"; # class="diff_info"
1891
1892 } else { # modified, mode changed, ...
1893 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001894 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001895 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001896 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1897 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001898 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001899 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001900 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001901 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1902 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001903 print "</div>\n"; # class="diff_info"
1904 }
1905
1906 #print "<div class=\"diff extended_header\">\n";
1907 $in_header = 1;
1908 next LINE;
1909 } # start of patch in patchset
1910
1911
1912 if ($in_header && $patch_line =~ m/^---/) {
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001913 #print "</div>\n"; # class="diff extended_header"
Jakub Narebskieee08902006-08-24 00:15:14 +02001914 $in_header = 0;
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001915
1916 my $file = $diffinfo->{'from_file'};
1917 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001918 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1919 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1920 -class => "list"}, esc_html($file));
1921 $patch_line =~ s|a/.*$|a/$file|g;
1922 print "<div class=\"diff from_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001923
1924 $patch_line = <$fd>;
1925 chomp $patch_line;
1926
1927 #$patch_line =~ m/^+++/;
1928 $file = $diffinfo->{'to_file'};
1929 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001930 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1931 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1932 -class => "list"}, esc_html($file));
1933 $patch_line =~ s|b/.*|b/$file|g;
1934 print "<div class=\"diff to_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001935
1936 next LINE;
Jakub Narebskieee08902006-08-24 00:15:14 +02001937 }
1938 next LINE if $in_header;
1939
1940 print format_diff_line($patch_line);
1941 }
1942 print "</div>\n" if $patch_found; # class="patch"
1943
1944 print "</div>\n"; # class="patchset"
1945}
1946
1947# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1948
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001949sub git_shortlog_body {
1950 # uses global variable $project
1951 my ($revlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301952
1953 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1954 my $have_snapshot = (defined $ctype && defined $suffix);
1955
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001956 $from = 0 unless defined $from;
1957 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1958
1959 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1960 my $alternate = 0;
1961 for (my $i = $from; $i <= $to; $i++) {
1962 my $commit = $revlist->[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02001963 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1964 my $ref = format_ref_marker($refs, $commit);
1965 my %co = parse_commit($commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001966 if ($alternate) {
1967 print "<tr class=\"dark\">\n";
1968 } else {
1969 print "<tr class=\"light\">\n";
1970 }
1971 $alternate ^= 1;
1972 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1973 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1974 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1975 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02001976 print format_subject_html($co{'title'}, $co{'title_short'},
1977 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001978 print "</td>\n" .
1979 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001980 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05301981 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1982 if ($have_snapshot) {
1983 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1984 }
1985 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001986 "</tr>\n";
1987 }
1988 if (defined $extra) {
1989 print "<tr>\n" .
1990 "<td colspan=\"4\">$extra</td>\n" .
1991 "</tr>\n";
1992 }
1993 print "</table>\n";
1994}
1995
Jakub Narebski581860e2006-08-14 02:09:08 +02001996sub git_history_body {
1997 # Warning: assumes constant type (blob or tree) during history
Jakub Narebski8be68352006-09-11 00:36:04 +02001998 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
1999
2000 $from = 0 unless defined $from;
2001 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
Jakub Narebski581860e2006-08-14 02:09:08 +02002002
2003 print "<table class=\"history\" cellspacing=\"0\">\n";
2004 my $alternate = 0;
Jakub Narebski8be68352006-09-11 00:36:04 +02002005 for (my $i = $from; $i <= $to; $i++) {
2006 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
Jakub Narebski581860e2006-08-14 02:09:08 +02002007 next;
2008 }
2009
2010 my $commit = $1;
2011 my %co = parse_commit($commit);
2012 if (!%co) {
2013 next;
2014 }
2015
2016 my $ref = format_ref_marker($refs, $commit);
2017
2018 if ($alternate) {
2019 print "<tr class=\"dark\">\n";
2020 } else {
2021 print "<tr class=\"light\">\n";
2022 }
2023 $alternate ^= 1;
2024 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2025 # shortlog uses chop_str($co{'author_name'}, 10)
2026 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2027 "<td>";
2028 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02002029 print format_subject_html($co{'title'}, $co{'title_short'},
2030 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02002031 print "</td>\n" .
2032 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002033 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2034 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2035 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
Jakub Narebski581860e2006-08-14 02:09:08 +02002036
2037 if ($ftype eq 'blob') {
2038 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2039 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2040 if (defined $blob_current && defined $blob_parent &&
2041 $blob_current ne $blob_parent) {
2042 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02002043 $cgi->a({-href => href(action=>"blobdiff",
2044 hash=>$blob_current, hash_parent=>$blob_parent,
2045 hash_base=>$hash_base, hash_parent_base=>$commit,
2046 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02002047 "diff to current");
2048 }
2049 }
2050 print "</td>\n" .
2051 "</tr>\n";
2052 }
2053 if (defined $extra) {
2054 print "<tr>\n" .
2055 "<td colspan=\"4\">$extra</td>\n" .
2056 "</tr>\n";
2057 }
2058 print "</table>\n";
2059}
2060
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002061sub git_tags_body {
2062 # uses global variable $project
2063 my ($taglist, $from, $to, $extra) = @_;
2064 $from = 0 unless defined $from;
2065 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2066
2067 print "<table class=\"tags\" cellspacing=\"0\">\n";
2068 my $alternate = 0;
2069 for (my $i = $from; $i <= $to; $i++) {
2070 my $entry = $taglist->[$i];
2071 my %tag = %$entry;
2072 my $comment_lines = $tag{'comment'};
2073 my $comment = shift @$comment_lines;
2074 my $comment_short;
2075 if (defined $comment) {
2076 $comment_short = chop_str($comment, 30, 5);
2077 }
2078 if ($alternate) {
2079 print "<tr class=\"dark\">\n";
2080 } else {
2081 print "<tr class=\"light\">\n";
2082 }
2083 $alternate ^= 1;
2084 print "<td><i>$tag{'age'}</i></td>\n" .
2085 "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002086 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002087 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002088 "</td>\n" .
2089 "<td>";
2090 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002091 print format_subject_html($comment, $comment_short,
2092 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002093 }
2094 print "</td>\n" .
2095 "<td class=\"selflink\">";
2096 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002097 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002098 } else {
2099 print "&nbsp;";
2100 }
2101 print "</td>\n" .
2102 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002103 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002104 if ($tag{'reftype'} eq "commit") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002105 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2106 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002107 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002108 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002109 }
2110 print "</td>\n" .
2111 "</tr>";
2112 }
2113 if (defined $extra) {
2114 print "<tr>\n" .
2115 "<td colspan=\"5\">$extra</td>\n" .
2116 "</tr>\n";
2117 }
2118 print "</table>\n";
2119}
2120
2121sub git_heads_body {
2122 # uses global variable $project
2123 my ($taglist, $head, $from, $to, $extra) = @_;
2124 $from = 0 unless defined $from;
2125 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2126
2127 print "<table class=\"heads\" cellspacing=\"0\">\n";
2128 my $alternate = 0;
2129 for (my $i = $from; $i <= $to; $i++) {
2130 my $entry = $taglist->[$i];
2131 my %tag = %$entry;
2132 my $curr = $tag{'id'} eq $head;
2133 if ($alternate) {
2134 print "<tr class=\"dark\">\n";
2135 } else {
2136 print "<tr class=\"light\">\n";
2137 }
2138 $alternate ^= 1;
2139 print "<td><i>$tag{'age'}</i></td>\n" .
2140 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002141 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002142 -class => "list name"},esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002143 "</td>\n" .
2144 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002145 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2146 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002147 "</td>\n" .
2148 "</tr>";
2149 }
2150 if (defined $extra) {
2151 print "<tr>\n" .
2152 "<td colspan=\"3\">$extra</td>\n" .
2153 "</tr>\n";
2154 }
2155 print "</table>\n";
2156}
2157
Jakub Narebski717b8312006-07-31 21:22:15 +02002158## ======================================================================
2159## ======================================================================
2160## actions
2161
Jakub Narebski717b8312006-07-31 21:22:15 +02002162sub git_project_list {
Jakub Narebski6326b602006-08-01 02:59:12 +02002163 my $order = $cgi->param('o');
2164 if (defined $order && $order !~ m/project|descr|owner|age/) {
Jakub Narebskie2860ea2006-08-05 13:15:24 +02002165 die_error(undef, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02002166 }
2167
Jakub Narebski847e01f2006-08-14 02:05:47 +02002168 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02002169 my @projects;
2170 if (!@list) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002171 die_error(undef, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02002172 }
2173 foreach my $pr (@list) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002174 my $head = git_get_head_hash($pr->{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02002175 if (!defined $head) {
2176 next;
2177 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002178 $git_dir = "$projectroot/$pr->{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02002179 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02002180 if (!%co) {
2181 next;
2182 }
2183 $pr->{'commit'} = \%co;
2184 if (!defined $pr->{'descr'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002185 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski717b8312006-07-31 21:22:15 +02002186 $pr->{'descr'} = chop_str($descr, 25, 5);
2187 }
2188 if (!defined $pr->{'owner'}) {
2189 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2190 }
2191 push @projects, $pr;
2192 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002193
Jakub Narebski717b8312006-07-31 21:22:15 +02002194 git_header_html();
2195 if (-f $home_text) {
2196 print "<div class=\"index_include\">\n";
2197 open (my $fd, $home_text);
2198 print <$fd>;
2199 close $fd;
2200 print "</div>\n";
2201 }
2202 print "<table class=\"project_list\">\n" .
2203 "<tr>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002204 $order ||= "project";
2205 if ($order eq "project") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002206 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2207 print "<th>Project</th>\n";
2208 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002209 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002210 $cgi->a({-href => href(project=>undef, order=>'project'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002211 -class => "header"}, "Project") .
2212 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002213 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002214 if ($order eq "descr") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002215 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2216 print "<th>Description</th>\n";
2217 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002218 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002219 $cgi->a({-href => href(project=>undef, order=>'descr'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002220 -class => "header"}, "Description") .
2221 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002222 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002223 if ($order eq "owner") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002224 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2225 print "<th>Owner</th>\n";
2226 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002227 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002228 $cgi->a({-href => href(project=>undef, order=>'owner'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002229 -class => "header"}, "Owner") .
2230 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002231 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002232 if ($order eq "age") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002233 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2234 print "<th>Last Change</th>\n";
2235 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002236 print "<th>" .
Jakub Narebskia1565c42006-09-15 19:30:34 +02002237 $cgi->a({-href => href(project=>undef, order=>'age'),
Jakub Narebski6326b602006-08-01 02:59:12 +02002238 -class => "header"}, "Last Change") .
2239 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002240 }
2241 print "<th></th>\n" .
2242 "</tr>\n";
2243 my $alternate = 0;
2244 foreach my $pr (@projects) {
2245 if ($alternate) {
2246 print "<tr class=\"dark\">\n";
2247 } else {
2248 print "<tr class=\"light\">\n";
2249 }
2250 $alternate ^= 1;
Martin Waitz756d2f02006-08-17 00:28:36 +02002251 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski6326b602006-08-01 02:59:12 +02002252 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002253 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2254 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002255 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2256 $pr->{'commit'}{'age_string'} . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002257 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02002258 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2259 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2260 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
Jakub Narebski717b8312006-07-31 21:22:15 +02002261 "</td>\n" .
2262 "</tr>\n";
2263 }
2264 print "</table>\n";
2265 git_footer_html();
2266}
2267
Jakub Narebskifc2b2be2006-09-15 04:56:03 +02002268sub git_project_index {
2269 my @projects = git_get_projects_list();
2270
2271 print $cgi->header(
2272 -type => 'text/plain',
2273 -charset => 'utf-8',
2274 -content_disposition => qq(inline; filename="index.aux"));
2275
2276 foreach my $pr (@projects) {
2277 if (!exists $pr->{'owner'}) {
2278 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2279 }
2280
2281 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2282 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2283 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2284 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2285 $path =~ s/ /\+/g;
2286 $owner =~ s/ /\+/g;
2287
2288 print "$path $owner\n";
2289 }
2290}
2291
Kay Sieversede5e102005-08-07 20:23:12 +02002292sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002293 my $descr = git_get_project_description($project) || "none";
2294 my $head = git_get_head_hash($project);
2295 my %co = parse_commit($head);
2296 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sieversede5e102005-08-07 20:23:12 +02002297
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002298 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002299
Jakub Narebski847e01f2006-08-14 02:05:47 +02002300 my $refs = git_get_references();
Kay Sieversede5e102005-08-07 20:23:12 +02002301 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002302 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002303
Kay Sievers19806692005-08-07 20:26:27 +02002304 print "<div class=\"title\">&nbsp;</div>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002305 print "<table cellspacing=\"0\">\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002306 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
Kay Sieversede5e102005-08-07 20:23:12 +02002307 "<tr><td>owner</td><td>$owner</td></tr>\n" .
Jakub Narebski19a87212006-08-15 23:03:17 +02002308 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002309 # use per project git URL list in $projectroot/$project/cloneurl
2310 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02002311 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002312 my @url_list = git_get_project_url_list($project);
2313 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2314 foreach my $git_url (@url_list) {
2315 next unless $git_url;
2316 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02002317 $url_tag = "";
2318 }
2319 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002320
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002321 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2322 git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002323 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002324 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversede5e102005-08-07 20:23:12 +02002325 close $fd;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002326 git_print_header_div('shortlog');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002327 git_shortlog_body(\@revlist, 0, 15, $refs,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002328 $cgi->a({-href => href(action=>"shortlog")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002329
Jakub Narebski847e01f2006-08-14 02:05:47 +02002330 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002331 if (defined @$taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002332 git_print_header_div('tags');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002333 git_tags_body($taglist, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002334 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002335 }
Kay Sievers0db37972005-08-07 20:24:35 +02002336
Jakub Narebski847e01f2006-08-14 02:05:47 +02002337 my $headlist = git_get_refs_list("refs/heads");
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002338 if (defined @$headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002339 git_print_header_div('heads');
Junio C Hamanob77aeb22006-07-31 19:12:18 -07002340 git_heads_body($headlist, $head, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002341 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02002342 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002343
Kay Sieversede5e102005-08-07 20:23:12 +02002344 git_footer_html();
2345}
2346
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002347sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002348 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002349 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002350 git_print_page_nav('','', $head,undef,$head);
2351 my %tag = parse_tag($hash);
2352 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002353 print "<div class=\"title_text\">\n" .
2354 "<table cellspacing=\"0\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002355 "<tr>\n" .
2356 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002357 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2358 $tag{'object'}) . "</td>\n" .
2359 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2360 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002361 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002362 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002363 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01002364 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002365 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2366 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2367 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002368 }
2369 print "</table>\n\n" .
2370 "</div>\n";
2371 print "<div class=\"page_body\">";
2372 my $comment = $tag{'comment'};
2373 foreach my $line (@$comment) {
Kay Sievers40c13812005-11-19 17:41:29 +01002374 print esc_html($line) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002375 }
2376 print "</div>\n";
2377 git_footer_html();
2378}
2379
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002380sub git_blame2 {
2381 my $fd;
2382 my $ftype;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302383
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302384 my ($have_blame) = gitweb_check_feature('blame');
2385 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302386 die_error('403 Permission denied', "Permission denied");
2387 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002388 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002389 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002390 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002391 my %co = parse_commit($hash_base)
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002392 or die_error(undef, "Reading commit failed");
2393 if (!defined $hash) {
2394 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2395 or die_error(undef, "Error looking up file");
2396 }
2397 $ftype = git_get_type($hash);
2398 if ($ftype !~ "blob") {
Jakub Narebskie484a2d2006-08-05 13:12:51 +02002399 die_error("400 Bad Request", "Object is not a blob");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002400 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002401 open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002402 or die_error(undef, "Open git-blame failed");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002403 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002404 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002405 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2406 "blob") .
2407 " | " .
2408 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2409 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002410 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2411 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002412 git_print_page_path($file_name, $ftype, $hash_base);
Luben Tuikov82f930d2006-08-04 15:09:59 -07002413 my @rev_color = (qw(light2 dark2));
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002414 my $num_colors = scalar(@rev_color);
2415 my $current_color = 0;
2416 my $last_rev;
Jakub Narebski59b9f612006-08-22 23:42:53 +02002417 print <<HTML;
2418<div class="page_body">
2419<table class="blame">
2420<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2421HTML
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002422 while (<$fd>) {
2423 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2424 my $full_rev = $1;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002425 my $rev = substr($full_rev, 0, 8);
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002426 my $lineno = $2;
2427 my $data = $3;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002428
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002429 if (!defined $last_rev) {
2430 $last_rev = $full_rev;
2431 } elsif ($last_rev ne $full_rev) {
2432 $last_rev = $full_rev;
2433 $current_color = ++$current_color % $num_colors;
2434 }
2435 print "<tr class=\"$rev_color[$current_color]\">\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002436 print "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002437 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2438 esc_html($rev)) . "</td>\n";
2439 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2440 esc_html($lineno) . "</a></td>\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002441 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2442 print "</tr>\n";
2443 }
2444 print "</table>\n";
2445 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002446 close $fd
2447 or print "Reading blob failed\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002448 git_footer_html();
2449}
2450
Florian Forstere34ef622006-06-11 17:45:19 +02002451sub git_blame {
2452 my $fd;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302453
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302454 my ($have_blame) = gitweb_check_feature('blame');
2455 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302456 die_error('403 Permission denied', "Permission denied");
2457 }
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002458 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002459 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002460 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002461 my %co = parse_commit($hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002462 or die_error(undef, "Reading commit failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002463 if (!defined $hash) {
2464 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002465 or die_error(undef, "Error lookup file");
Florian Forstere34ef622006-06-11 17:45:19 +02002466 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002467 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002468 or die_error(undef, "Open git-annotate failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002469 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002470 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002471 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2472 "blob") .
2473 " | " .
2474 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2475 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002476 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2477 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002478 git_print_page_path($file_name, 'blob', $hash_base);
Florian Forstere34ef622006-06-11 17:45:19 +02002479 print "<div class=\"page_body\">\n";
2480 print <<HTML;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002481<table class="blame">
Florian Forstere34ef622006-06-11 17:45:19 +02002482 <tr>
2483 <th>Commit</th>
2484 <th>Age</th>
2485 <th>Author</th>
2486 <th>Line</th>
2487 <th>Data</th>
2488 </tr>
2489HTML
2490 my @line_class = (qw(light dark));
2491 my $line_class_len = scalar (@line_class);
2492 my $line_class_num = $#line_class;
2493 while (my $line = <$fd>) {
2494 my $long_rev;
2495 my $short_rev;
2496 my $author;
2497 my $time;
2498 my $lineno;
2499 my $data;
2500 my $age;
2501 my $age_str;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002502 my $age_class;
Florian Forstere34ef622006-06-11 17:45:19 +02002503
2504 chomp $line;
2505 $line_class_num = ($line_class_num + 1) % $line_class_len;
2506
Jakub Narebski030b5202006-08-26 02:13:05 +02002507 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 +02002508 $long_rev = $1;
2509 $author = $2;
2510 $time = $3;
2511 $lineno = $4;
2512 $data = $5;
2513 } else {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002514 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
Florian Forstere34ef622006-06-11 17:45:19 +02002515 next;
2516 }
2517 $short_rev = substr ($long_rev, 0, 8);
2518 $age = time () - $time;
2519 $age_str = age_string ($age);
2520 $age_str =~ s/ /&nbsp;/g;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002521 $age_class = age_class($age);
Florian Forstere34ef622006-06-11 17:45:19 +02002522 $author = esc_html ($author);
2523 $author =~ s/ /&nbsp;/g;
Jakub Narebskif16db172006-08-06 02:08:31 +02002524
2525 $data = untabify($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002526 $data = esc_html ($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002527
2528 print <<HTML;
2529 <tr class="$line_class[$line_class_num]">
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002530 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002531 <td class="$age_class">$age_str</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002532 <td>$author</td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002533 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2534 <td class="pre">$data</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002535 </tr>
2536HTML
2537 } # while (my $line = <$fd>)
2538 print "</table>\n\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002539 close $fd
2540 or print "Reading blob failed.\n";
Florian Forstere34ef622006-06-11 17:45:19 +02002541 print "</div>";
2542 git_footer_html();
2543}
2544
Kay Sieversede5e102005-08-07 20:23:12 +02002545sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002546 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002547 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002548 git_print_page_nav('','', $head,undef,$head);
2549 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002550
Jakub Narebski847e01f2006-08-14 02:05:47 +02002551 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002552 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002553 git_tags_body($taglist);
Kay Sieversede5e102005-08-07 20:23:12 +02002554 }
Kay Sieversede5e102005-08-07 20:23:12 +02002555 git_footer_html();
2556}
2557
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002558sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002559 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002560 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002561 git_print_page_nav('','', $head,undef,$head);
2562 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002563
Jakub Narebski847e01f2006-08-14 02:05:47 +02002564 my $taglist = git_get_refs_list("refs/heads");
Kay Sievers0db37972005-08-07 20:24:35 +02002565 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002566 git_heads_body($taglist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02002567 }
Kay Sievers0db37972005-08-07 20:24:35 +02002568 git_footer_html();
2569}
2570
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002571sub git_blob_plain {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002572 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02002573
Luben Tuikovcff07712006-07-23 13:28:55 -07002574 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002575 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002576 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002577 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002578 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002579 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002580 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002581 }
Martin Waitz800764c2006-09-16 23:09:02 +02002582 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2583 # blobs defined by non-textual hash id's can be cached
2584 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002585 }
Martin Waitz800764c2006-09-16 23:09:02 +02002586
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002587 my $type = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002588 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicfd82662006-08-05 12:56:04 +02002589 or die_error(undef, "Couldn't cat $file_name, $hash");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002590
Jakub Narebski847e01f2006-08-14 02:05:47 +02002591 $type ||= blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002592
2593 # save as filename, even when no $file_name is given
2594 my $save_as = "$hash";
2595 if (defined $file_name) {
2596 $save_as = $file_name;
2597 } elsif ($type =~ m/^text\//) {
2598 $save_as .= '.txt';
2599 }
2600
Jakub Narebskif2e73302006-08-26 19:14:25 +02002601 print $cgi->header(
2602 -type => "$type",
2603 -expires=>$expires,
2604 -content_disposition => "inline; filename=\"$save_as\"");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002605 undef $/;
2606 binmode STDOUT, ':raw';
2607 print <$fd>;
2608 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2609 $/ = "\n";
2610 close $fd;
2611}
2612
Kay Sievers09bd7892005-08-07 20:21:23 +02002613sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002614 my $expires;
Jakub Narebskif2e73302006-08-26 19:14:25 +02002615
Luben Tuikovcff07712006-07-23 13:28:55 -07002616 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002617 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002618 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002619 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002620 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002621 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002622 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002623 }
Martin Waitz800764c2006-09-16 23:09:02 +02002624 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2625 # blobs defined by non-textual hash id's can be cached
2626 $expires = "+1d";
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002627 }
Martin Waitz800764c2006-09-16 23:09:02 +02002628
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302629 my ($have_blame) = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002630 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002631 or die_error(undef, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002632 my $mimetype = blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002633 if ($mimetype !~ m/^text\//) {
2634 close $fd;
2635 return git_blob_plain($mimetype);
2636 }
Jakub Narebskif2e73302006-08-26 19:14:25 +02002637 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002638 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02002639 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02002640 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02002641 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002642 $formats_nav .=
2643 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2644 hash=>$hash, file_name=>$file_name)},
2645 "blame") .
2646 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02002647 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002648 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02002649 $cgi->a({-href => href(action=>"blob_plain",
2650 hash=>$hash, file_name=>$file_name)},
2651 "plain") .
2652 " | " .
2653 $cgi->a({-href => href(action=>"blob",
2654 hash_base=>"HEAD", file_name=>$file_name)},
2655 "head");
Kay Sievers93129442005-10-17 03:27:54 +02002656 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002657 $formats_nav .=
2658 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
Kay Sievers93129442005-10-17 03:27:54 +02002659 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002660 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2661 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02002662 } else {
2663 print "<div class=\"page_nav\">\n" .
2664 "<br/><br/></div>\n" .
2665 "<div class=\"title\">$hash</div>\n";
2666 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002667 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002668 print "<div class=\"page_body\">\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002669 my $nr;
2670 while (my $line = <$fd>) {
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002671 chomp $line;
Kay Sievers161332a2005-08-07 19:49:46 +02002672 $nr++;
Jakub Narebskif16db172006-08-06 02:08:31 +02002673 $line = untabify($line);
Jakub Narebski952c65f2006-08-22 16:52:50 +02002674 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2675 $nr, $nr, $nr, esc_html($line);
Kay Sievers161332a2005-08-07 19:49:46 +02002676 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002677 close $fd
2678 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02002679 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002680 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002681}
2682
2683sub git_tree {
Kay Sieversb87d78d2005-08-07 20:21:04 +02002684 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002685 $hash = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02002686 if (defined $file_name) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002687 my $base = $hash_base || $hash;
Kay Sievers09bd7892005-08-07 20:21:23 +02002688 $hash = git_get_hash_by_path($base, $file_name, "tree");
2689 }
Kay Sievers10dba282005-08-07 20:25:27 +02002690 if (!defined $hash_base) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002691 $hash_base = $hash;
Kay Sievers10dba282005-08-07 20:25:27 +02002692 }
Kay Sieverse925f382005-08-07 20:23:35 +02002693 }
Kay Sievers232ff552005-11-24 16:56:55 +01002694 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002695 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002696 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002697 my @entries = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002698 close $fd or die_error(undef, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01002699 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02002700
Jakub Narebski847e01f2006-08-14 02:05:47 +02002701 my $refs = git_get_references();
2702 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02002703 git_header_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002704 my $base = "";
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302705 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02002706 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002707 git_print_page_nav('tree','', $hash_base);
2708 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02002709 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02002710 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02002711 print "<div class=\"page_nav\">\n";
2712 print "<br/><br/></div>\n";
2713 print "<div class=\"title\">$hash</div>\n";
2714 }
Kay Sievers09bd7892005-08-07 20:21:23 +02002715 if (defined $file_name) {
Kay Sievers232ff552005-11-24 16:56:55 +01002716 $base = esc_html("$file_name/");
Kay Sievers09bd7892005-08-07 20:21:23 +02002717 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002718 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sieversfbb592a2005-08-07 20:12:11 +02002719 print "<div class=\"page_body\">\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02002720 print "<table cellspacing=\"0\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002721 my $alternate = 0;
Kay Sievers161332a2005-08-07 19:49:46 +02002722 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02002723 my %t = parse_ls_tree_line($line, -z => 1);
2724
Kay Sieversbddec012005-08-07 20:25:42 +02002725 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02002726 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002727 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02002728 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002729 }
2730 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002731
Jakub Narebskifa702002006-08-31 00:35:07 +02002732 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2733
Kay Sievers42f7eb92005-08-07 20:21:46 +02002734 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002735 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02002736 print "</table>\n" .
2737 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002738 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002739}
2740
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302741sub git_snapshot {
2742
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302743 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2744 my $have_snapshot = (defined $ctype && defined $suffix);
2745 if (!$have_snapshot) {
2746 die_error('403 Permission denied', "Permission denied");
2747 }
2748
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302749 if (!defined $hash) {
2750 $hash = git_get_head_hash($project);
2751 }
2752
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302753 my $filename = basename($project) . "-$hash.tar.$suffix";
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302754
2755 print $cgi->header(-type => 'application/x-tar',
Jakub Narebski134a69412006-08-22 16:55:34 +02002756 -content_encoding => $ctype,
2757 -content_disposition => "inline; filename=\"$filename\"",
Jakub Narebski952c65f2006-08-22 16:52:50 +02002758 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302759
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002760 my $git_command = git_cmd_str();
2761 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302762 die_error(undef, "Execute git-tar-tree failed.");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302763 binmode STDOUT, ':raw';
2764 print <$fd>;
2765 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2766 close $fd;
2767
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302768}
2769
Kay Sievers09bd7892005-08-07 20:21:23 +02002770sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002771 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002772 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02002773 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02002774 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02002775 if (!defined $page) {
2776 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02002777 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002778 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02002779
2780 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002781 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002782 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002783 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversea4a6df2005-08-07 20:26:49 +02002784 close $fd;
2785
Jakub Narebski847e01f2006-08-14 02:05:47 +02002786 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002787
2788 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002789 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002790
Kay Sieversb87d78d2005-08-07 20:21:04 +02002791 if (!@revlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002792 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002793
Jakub Narebski847e01f2006-08-14 02:05:47 +02002794 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02002795 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002796 }
Kay Sieversc994d622005-08-07 20:27:18 +02002797 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2798 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02002799 my $ref = format_ref_marker($refs, $commit);
2800 my %co = parse_commit($commit);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002801 next if !%co;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002802 my %ad = parse_date($co{'author_epoch'});
2803 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02002804 "<span class=\"age\">$co{'age_string'}</span>" .
2805 esc_html($co{'title'}) . $ref,
2806 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02002807 print "<div class=\"title_text\">\n" .
2808 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002809 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002810 " | " .
2811 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Kay Sieverseb282402005-08-07 20:21:34 +02002812 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02002813 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002814 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02002815 "</div>\n";
2816
2817 print "<div class=\"log_body\">\n";
2818 git_print_simplified_log($co{'comment'});
Kay Sievers09bd7892005-08-07 20:21:23 +02002819 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002820 }
2821 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002822}
2823
2824sub git_commit {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002825 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02002826 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002827 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02002828 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002829 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2830 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02002831
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002832 my $parent = $co{'parent'};
2833 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07002834 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02002835 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002836 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002837 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002838 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002839 close $fd or die_error(undef, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02002840
2841 # non-textual hash id's can be cached
2842 my $expires;
2843 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2844 $expires = "+1d";
2845 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002846 my $refs = git_get_references();
2847 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302848
2849 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2850 my $have_snapshot = (defined $ctype && defined $suffix);
2851
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002852 my $formats_nav = '';
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002853 if (defined $file_name && defined $co{'parent'}) {
2854 my $parent = $co{'parent'};
Jakub Narebski952c65f2006-08-22 16:52:50 +02002855 $formats_nav .=
2856 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2857 "blame");
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002858 }
Jakub Narebski594e2122006-07-31 02:21:52 +02002859 git_header_html(undef, $expires);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002860 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
Jakub Narebski952c65f2006-08-22 16:52:50 +02002861 $hash, $co{'tree'}, $hash,
2862 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002863
Kay Sieversb87d78d2005-08-07 20:21:04 +02002864 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002865 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002866 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002867 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002868 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02002869 print "<div class=\"title_text\">\n" .
Kay Sieversb87d78d2005-08-07 20:21:04 +02002870 "<table cellspacing=\"0\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002871 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02002872 "<tr>" .
2873 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02002874 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002875 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2876 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02002877 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002878 printf(" (%02d:%02d %s)",
2879 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002880 }
Kay Sieversbddec012005-08-07 20:25:42 +02002881 print "</td>" .
2882 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002883 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002884 print "<tr><td></td><td> $cd{'rfc2822'}" .
2885 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2886 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002887 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002888 print "<tr>" .
2889 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002890 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002891 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2892 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02002893 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002894 "<td class=\"link\">" .
2895 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2896 "tree");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302897 if ($have_snapshot) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002898 print " | " .
2899 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302900 }
2901 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002902 "</tr>\n";
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002903 my $parents = $co{'parents'};
Kay Sievers3e029292005-08-07 20:05:15 +02002904 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02002905 print "<tr>" .
2906 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002907 "<td class=\"sha1\">" .
2908 $cgi->a({-href => href(action=>"commit", hash=>$par),
2909 class => "list"}, $par) .
2910 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002911 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002912 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002913 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02002914 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02002915 "</td>" .
2916 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02002917 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02002918 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02002919 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002920
Kay Sieversfbb592a2005-08-07 20:12:11 +02002921 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002922 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002923 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002924
Jakub Narebskieee08902006-08-24 00:15:14 +02002925 git_difftree_body(\@difftree, $hash, $parent);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002926
Kay Sievers12a88f22005-08-07 20:02:47 +02002927 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002928}
2929
2930sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002931 my $format = shift || 'html';
2932
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002933 my $fd;
2934 my @difftree;
2935 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002936 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002937
2938 # preparing $fd and %diffinfo for git_patchset_body
2939 # new style URI
2940 if (defined $hash_base && defined $hash_parent_base) {
2941 if (defined $file_name) {
2942 # read raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002943 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002944 "--", $file_name
2945 or die_error(undef, "Open git-diff-tree failed");
2946 @difftree = map { chomp; $_ } <$fd>;
2947 close $fd
2948 or die_error(undef, "Reading git-diff-tree failed");
2949 @difftree
2950 or die_error('404 Not Found', "Blob diff not found");
2951
Jakub Narebski0aea3372006-08-27 23:45:26 +02002952 } elsif (defined $hash &&
2953 $hash =~ /[0-9a-fA-F]{40}/) {
2954 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002955
2956 # read filtered raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002957 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002958 or die_error(undef, "Open git-diff-tree failed");
2959 @difftree =
2960 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
2961 # $hash == to_id
2962 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2963 map { chomp; $_ } <$fd>;
2964 close $fd
2965 or die_error(undef, "Reading git-diff-tree failed");
2966 @difftree
2967 or die_error('404 Not Found', "Blob diff not found");
2968
2969 } else {
2970 die_error('404 Not Found', "Missing one of the blob diff parameters");
2971 }
2972
2973 if (@difftree > 1) {
2974 die_error('404 Not Found', "Ambiguous blob diff specification");
2975 }
2976
2977 %diffinfo = parse_difftree_raw_line($difftree[0]);
2978 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2979 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
2980
2981 $hash_parent ||= $diffinfo{'from_id'};
2982 $hash ||= $diffinfo{'to_id'};
2983
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002984 # non-textual hash id's can be cached
2985 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2986 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2987 $expires = '+1d';
2988 }
2989
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002990 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002991 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02002992 '-p', $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002993 "--", $file_name
2994 or die_error(undef, "Open git-diff-tree failed");
2995 }
2996
2997 # old/legacy style URI
2998 if (!%diffinfo && # if new style URI failed
2999 defined $hash && defined $hash_parent) {
3000 # fake git-diff-tree raw output
3001 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3002 $diffinfo{'from_id'} = $hash_parent;
3003 $diffinfo{'to_id'} = $hash;
3004 if (defined $file_name) {
3005 if (defined $file_parent) {
3006 $diffinfo{'status'} = '2';
3007 $diffinfo{'from_file'} = $file_parent;
3008 $diffinfo{'to_file'} = $file_name;
3009 } else { # assume not renamed
3010 $diffinfo{'status'} = '1';
3011 $diffinfo{'from_file'} = $file_name;
3012 $diffinfo{'to_file'} = $file_name;
3013 }
3014 } else { # no filename given
3015 $diffinfo{'status'} = '2';
3016 $diffinfo{'from_file'} = $hash_parent;
3017 $diffinfo{'to_file'} = $hash;
3018 }
3019
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003020 # non-textual hash id's can be cached
3021 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3022 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3023 $expires = '+1d';
3024 }
3025
3026 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003027 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003028 or die_error(undef, "Open git-diff failed");
3029 } else {
3030 die_error('404 Not Found', "Missing one of the blob diff parameters")
3031 unless %diffinfo;
3032 }
3033
3034 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003035 if ($format eq 'html') {
3036 my $formats_nav =
3037 $cgi->a({-href => href(action=>"blobdiff_plain",
3038 hash=>$hash, hash_parent=>$hash_parent,
3039 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3040 file_name=>$file_name, file_parent=>$file_parent)},
3041 "plain");
3042 git_header_html(undef, $expires);
3043 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3044 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3045 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3046 } else {
3047 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3048 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3049 }
3050 if (defined $file_name) {
3051 git_print_page_path($file_name, "blob", $hash_base);
3052 } else {
3053 print "<div class=\"page_path\"></div>\n";
3054 }
3055
3056 } elsif ($format eq 'plain') {
3057 print $cgi->header(
3058 -type => 'text/plain',
3059 -charset => 'utf-8',
3060 -expires => $expires,
3061 -content_disposition => qq(inline; filename="${file_name}.patch"));
3062
3063 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3064
Kay Sievers09bd7892005-08-07 20:21:23 +02003065 } else {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003066 die_error(undef, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003067 }
3068
3069 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003070 if ($format eq 'html') {
3071 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003072
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003073 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3074 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02003075
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003076 print "</div>\n"; # class="page_body"
3077 git_footer_html();
3078
3079 } else {
3080 while (my $line = <$fd>) {
3081 $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
3082 $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
3083
3084 print $line;
3085
3086 last if $line =~ m!^\+\+\+!;
3087 }
3088 local $/ = undef;
3089 print <$fd>;
3090 close $fd;
3091 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003092}
3093
Kay Sievers19806692005-08-07 20:26:27 +02003094sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003095 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003096}
3097
Kay Sievers09bd7892005-08-07 20:21:23 +02003098sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02003099 my $format = shift || 'html';
Jakub Narebski847e01f2006-08-14 02:05:47 +02003100 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02003101 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003102 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02003103 }
Kay Sieversbddec012005-08-07 20:25:42 +02003104 if (!defined $hash_parent) {
Jakub Narebskib5ff2cf2006-08-06 16:14:25 +02003105 $hash_parent = $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02003106 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003107
3108 # read commitdiff
3109 my $fd;
3110 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02003111 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003112 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskieee08902006-08-24 00:15:14 +02003113 "--patch-with-raw", "--full-index", $hash_parent, $hash
3114 or die_error(undef, "Open git-diff-tree failed");
3115
3116 while (chomp(my $line = <$fd>)) {
3117 # empty line ends raw part of diff-tree output
3118 last unless $line;
3119 push @difftree, $line;
3120 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003121
Jakub Narebskieee08902006-08-24 00:15:14 +02003122 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003123 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003124 '-p', $hash_parent, $hash
Jakub Narebskieee08902006-08-24 00:15:14 +02003125 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02003126
Jakub Narebskieee08902006-08-24 00:15:14 +02003127 } else {
3128 die_error(undef, "Unknown commitdiff format");
3129 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003130
Kay Sievers11044292005-10-19 03:18:45 +02003131 # non-textual hash id's can be cached
3132 my $expires;
3133 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3134 $expires = "+1d";
3135 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003136
Jakub Narebskieee08902006-08-24 00:15:14 +02003137 # write commit message
3138 if ($format eq 'html') {
3139 my $refs = git_get_references();
3140 my $ref = format_ref_marker($refs, $co{'id'});
3141 my $formats_nav =
3142 $cgi->a({-href => href(action=>"commitdiff_plain",
3143 hash=>$hash, hash_parent=>$hash_parent)},
3144 "plain");
Kay Sievers19806692005-08-07 20:26:27 +02003145
Jakub Narebskieee08902006-08-24 00:15:14 +02003146 git_header_html(undef, $expires);
3147 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3148 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003149 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02003150 print "<div class=\"page_body\">\n";
3151 print "<div class=\"log\">\n";
3152 git_print_simplified_log($co{'comment'}, 1); # skip title
3153 print "</div>\n"; # class="log"
Kay Sievers1b1cd422005-08-07 20:28:01 +02003154
Jakub Narebskieee08902006-08-24 00:15:14 +02003155 } elsif ($format eq 'plain') {
3156 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003157 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02003158 my $filename = basename($project) . "-$hash.patch";
3159
3160 print $cgi->header(
3161 -type => 'text/plain',
3162 -charset => 'utf-8',
3163 -expires => $expires,
3164 -content_disposition => qq(inline; filename="$filename"));
3165 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3166 print <<TEXT;
Jakub Narebski59b9f612006-08-22 23:42:53 +02003167From: $co{'author'}
3168Date: $ad{'rfc2822'} ($ad{'tz_local'})
3169Subject: $co{'title'}
3170TEXT
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003171 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02003172 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003173
Jakub Narebskieee08902006-08-24 00:15:14 +02003174 foreach my $line (@{$co{'comment'}}) {
3175 print "$line\n";
Kay Sievers19806692005-08-07 20:26:27 +02003176 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003177 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02003178 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003179
3180 # write patch
3181 if ($format eq 'html') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003182 git_difftree_body(\@difftree, $hash, $hash_parent);
3183 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02003184
Jakub Narebski157e43b2006-08-24 19:34:36 +02003185 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3186 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02003187 print "</div>\n"; # class="page_body"
3188 git_footer_html();
3189
3190 } elsif ($format eq 'plain') {
3191 local $/ = undef;
3192 print <$fd>;
3193 close $fd
3194 or print "Reading git-diff-tree failed\n";
3195 }
3196}
3197
3198sub git_commitdiff_plain {
3199 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003200}
3201
Kay Sievers09bd7892005-08-07 20:21:23 +02003202sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07003203 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003204 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02003205 }
Jakub Narebski8be68352006-09-11 00:36:04 +02003206 if (!defined $page) {
3207 $page = 0;
3208 }
Luben Tuikov63433102006-07-23 13:31:15 -07003209 my $ftype;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003210 my %co = parse_commit($hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02003211 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003212 die_error(undef, "Unknown commit object");
Kay Sievers2ae100d2005-08-07 20:17:00 +02003213 }
Jakub Narebski8be68352006-09-11 00:36:04 +02003214
Jakub Narebski847e01f2006-08-14 02:05:47 +02003215 my $refs = git_get_references();
Jakub Narebski8be68352006-09-11 00:36:04 +02003216 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3217
Luben Tuikov93d5f062006-07-23 13:30:08 -07003218 if (!defined $hash && defined $file_name) {
3219 $hash = git_get_hash_by_path($hash_base, $file_name);
3220 }
Luben Tuikovcff07712006-07-23 13:28:55 -07003221 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07003222 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07003223 }
Kay Sievers10dba282005-08-07 20:25:27 +02003224
Junio C Hamanocdd40372006-06-30 18:54:32 -07003225 open my $fd, "-|",
Jakub Narebski8be68352006-09-11 00:36:04 +02003226 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3227 or die_error(undef, "Open git-rev-list-failed");
3228 my @revlist = map { chomp; $_ } <$fd>;
3229 close $fd
3230 or die_error(undef, "Reading git-rev-list failed");
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003231
Jakub Narebski8be68352006-09-11 00:36:04 +02003232 my $paging_nav = '';
3233 if ($page > 0) {
3234 $paging_nav .=
3235 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3236 file_name=>$file_name)},
3237 "first");
3238 $paging_nav .= " &sdot; " .
3239 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3240 file_name=>$file_name, page=>$page-1),
3241 -accesskey => "p", -title => "Alt-p"}, "prev");
3242 } else {
3243 $paging_nav .= "first";
3244 $paging_nav .= " &sdot; prev";
3245 }
3246 if ($#revlist >= (100 * ($page+1)-1)) {
3247 $paging_nav .= " &sdot; " .
3248 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3249 file_name=>$file_name, page=>$page+1),
3250 -accesskey => "n", -title => "Alt-n"}, "next");
3251 } else {
3252 $paging_nav .= " &sdot; next";
3253 }
3254 my $next_link = '';
3255 if ($#revlist >= (100 * ($page+1)-1)) {
3256 $next_link =
3257 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3258 file_name=>$file_name, page=>$page+1),
3259 -title => "Alt-n"}, "next");
3260 }
Jakub Narebski581860e2006-08-14 02:09:08 +02003261
Jakub Narebski8be68352006-09-11 00:36:04 +02003262 git_header_html();
3263 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3264 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3265 git_print_page_path($file_name, $ftype, $hash_base);
3266
3267 git_history_body(\@revlist, ($page * 100), $#revlist,
3268 $refs, $hash_base, $ftype, $next_link);
3269
Kay Sieversd51e9022005-08-07 20:16:07 +02003270 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02003271}
Kay Sievers19806692005-08-07 20:26:27 +02003272
3273sub git_search {
3274 if (!defined $searchtext) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003275 die_error(undef, "Text field empty");
Kay Sievers19806692005-08-07 20:26:27 +02003276 }
3277 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003278 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003279 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003280 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02003281 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003282 die_error(undef, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02003283 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02003284
Kay Sieversc994d622005-08-07 20:27:18 +02003285 my $commit_search = 1;
3286 my $author_search = 0;
3287 my $committer_search = 0;
3288 my $pickaxe_search = 0;
3289 if ($searchtext =~ s/^author\\://i) {
3290 $author_search = 1;
3291 } elsif ($searchtext =~ s/^committer\\://i) {
3292 $committer_search = 1;
3293 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3294 $commit_search = 0;
3295 $pickaxe_search = 1;
Jakub Narebski04f7a942006-09-11 00:29:27 +02003296
3297 # pickaxe may take all resources of your box and run for several minutes
3298 # with every query - so decide by yourself how public you make this feature
3299 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3300 if (!$have_pickaxe) {
3301 die_error('403 Permission denied', "Permission denied");
3302 }
Kay Sieversc994d622005-08-07 20:27:18 +02003303 }
Kay Sievers19806692005-08-07 20:26:27 +02003304 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003305 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3306 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Kay Sievers19806692005-08-07 20:26:27 +02003307
Kay Sievers19806692005-08-07 20:26:27 +02003308 print "<table cellspacing=\"0\">\n";
Kay Sievers19806692005-08-07 20:26:27 +02003309 my $alternate = 0;
Kay Sieversc994d622005-08-07 20:27:18 +02003310 if ($commit_search) {
3311 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003312 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
Kay Sieversc994d622005-08-07 20:27:18 +02003313 while (my $commit_text = <$fd>) {
3314 if (!grep m/$searchtext/i, $commit_text) {
Kay Sievers19806692005-08-07 20:26:27 +02003315 next;
3316 }
Kay Sieversc994d622005-08-07 20:27:18 +02003317 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3318 next;
Kay Sievers19806692005-08-07 20:26:27 +02003319 }
Kay Sieversc994d622005-08-07 20:27:18 +02003320 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3321 next;
3322 }
3323 my @commit_lines = split "\n", $commit_text;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003324 my %co = parse_commit(undef, \@commit_lines);
Kay Sieversc994d622005-08-07 20:27:18 +02003325 if (!%co) {
3326 next;
3327 }
3328 if ($alternate) {
3329 print "<tr class=\"dark\">\n";
3330 } else {
3331 print "<tr class=\"light\">\n";
3332 }
3333 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003334 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003335 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003336 "<td>" .
Jakub Narebski63e42202006-08-22 12:38:59 +02003337 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3338 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003339 my $comment = $co{'comment'};
3340 foreach my $line (@$comment) {
3341 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
Kay Sievers40c13812005-11-19 17:41:29 +01003342 my $lead = esc_html($1) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003343 $lead = chop_str($lead, 30, 10);
Kay Sievers40c13812005-11-19 17:41:29 +01003344 my $match = esc_html($2) || "";
3345 my $trail = esc_html($3) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003346 $trail = chop_str($trail, 30, 10);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00003347 my $text = "$lead<span class=\"match\">$match</span>$trail";
Kay Sieversc994d622005-08-07 20:27:18 +02003348 print chop_str($text, 80, 5) . "<br/>\n";
3349 }
3350 }
3351 print "</td>\n" .
3352 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003353 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003354 " | " .
3355 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003356 print "</td>\n" .
3357 "</tr>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003358 }
Kay Sieversc994d622005-08-07 20:27:18 +02003359 close $fd;
3360 }
3361
3362 if ($pickaxe_search) {
3363 $/ = "\n";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003364 my $git_command = git_cmd_str();
3365 open my $fd, "-|", "$git_command rev-list $hash | " .
3366 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
Kay Sieversc994d622005-08-07 20:27:18 +02003367 undef %co;
3368 my @files;
3369 while (my $line = <$fd>) {
3370 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3371 my %set;
3372 $set{'file'} = $6;
3373 $set{'from_id'} = $3;
3374 $set{'to_id'} = $4;
3375 $set{'id'} = $set{'to_id'};
3376 if ($set{'id'} =~ m/0{40}/) {
3377 $set{'id'} = $set{'from_id'};
3378 }
3379 if ($set{'id'} =~ m/0{40}/) {
3380 next;
3381 }
3382 push @files, \%set;
Kay Sievers53b89d82005-08-12 22:12:58 +02003383 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
Kay Sieversc994d622005-08-07 20:27:18 +02003384 if (%co) {
3385 if ($alternate) {
3386 print "<tr class=\"dark\">\n";
3387 } else {
3388 print "<tr class=\"light\">\n";
3389 }
3390 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003391 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003392 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003393 "<td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003394 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3395 -class => "list subject"},
Jakub Narebski63e42202006-08-22 12:38:59 +02003396 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003397 while (my $setref = shift @files) {
3398 my %set = %$setref;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003399 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3400 hash=>$set{'id'}, file_name=>$set{'file'}),
3401 -class => "list"},
3402 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
Kay Sieversc994d622005-08-07 20:27:18 +02003403 "<br/>\n";
3404 }
3405 print "</td>\n" .
3406 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003407 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003408 " | " .
3409 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003410 print "</td>\n" .
3411 "</tr>\n";
3412 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003413 %co = parse_commit($1);
Kay Sieversc994d622005-08-07 20:27:18 +02003414 }
3415 }
3416 close $fd;
Kay Sievers19806692005-08-07 20:26:27 +02003417 }
3418 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003419 git_footer_html();
3420}
3421
3422sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003423 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003424 if (!defined $hash) {
3425 $hash = $head;
3426 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02003427 if (!defined $page) {
3428 $page = 0;
3429 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003430 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02003431
3432 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003433 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003434 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02003435 my @revlist = map { chomp; $_ } <$fd>;
Kay Sievers19806692005-08-07 20:26:27 +02003436 close $fd;
Kay Sieversea4a6df2005-08-07 20:26:49 +02003437
Jakub Narebski847e01f2006-08-14 02:05:47 +02003438 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003439 my $next_link = '';
3440 if ($#revlist >= (100 * ($page+1)-1)) {
3441 $next_link =
Martin Waitz756d2f02006-08-17 00:28:36 +02003442 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003443 -title => "Alt-n"}, "next");
3444 }
3445
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003446
3447 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003448 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3449 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003450
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003451 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3452
Kay Sievers19806692005-08-07 20:26:27 +02003453 git_footer_html();
3454}
Jakub Narebski717b8312006-07-31 21:22:15 +02003455
3456## ......................................................................
3457## feeds (RSS, OPML)
3458
3459sub git_rss {
3460 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003461 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003462 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003463 my @revlist = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003464 close $fd or die_error(undef, "Reading git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003465 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003466 print <<XML;
3467<?xml version="1.0" encoding="utf-8"?>
3468<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3469<channel>
3470<title>$project $my_uri $my_url</title>
3471<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3472<description>$project log</description>
3473<language>en</language>
3474XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003475
3476 for (my $i = 0; $i <= $#revlist; $i++) {
3477 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02003478 my %co = parse_commit($commit);
Jakub Narebski717b8312006-07-31 21:22:15 +02003479 # we read 150, we always show 30 and the ones more recent than 48 hours
3480 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3481 last;
3482 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003483 my %cd = parse_date($co{'committer_epoch'});
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003484 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003485 $co{'parent'}, $co{'id'}
3486 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003487 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003488 close $fd
3489 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003490 print "<item>\n" .
3491 "<title>" .
3492 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3493 "</title>\n" .
3494 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3495 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3496 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3497 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3498 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3499 "<content:encoded>" .
3500 "<![CDATA[\n";
3501 my $comment = $co{'comment'};
3502 foreach my $line (@$comment) {
3503 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3504 print "$line<br/>\n";
3505 }
3506 print "<br/>\n";
3507 foreach my $line (@difftree) {
3508 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3509 next;
3510 }
3511 my $file = validate_input(unquote($7));
3512 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3513 print "$file<br/>\n";
3514 }
3515 print "]]>\n" .
3516 "</content:encoded>\n" .
3517 "</item>\n";
3518 }
3519 print "</channel></rss>";
3520}
3521
3522sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003523 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02003524
3525 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003526 print <<XML;
3527<?xml version="1.0" encoding="utf-8"?>
3528<opml version="1.0">
3529<head>
3530 <title>$site_name Git OPML Export</title>
3531</head>
3532<body>
3533<outline text="git RSS feeds">
3534XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003535
3536 foreach my $pr (@list) {
3537 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003538 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02003539 if (!defined $head) {
3540 next;
3541 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003542 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02003543 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02003544 if (!%co) {
3545 next;
3546 }
3547
3548 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3549 my $rss = "$my_url?p=$proj{'path'};a=rss";
3550 my $html = "$my_url?p=$proj{'path'};a=summary";
3551 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3552 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02003553 print <<XML;
3554</outline>
3555</body>
3556</opml>
3557XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003558}