blob: 53e3478e4f81fd8b88708df6206b6159dae00a7f [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
Jakub Narebski19a87212006-08-15 23:03:17 +020057# list of git base URLs used for URL to where fetch project from,
58# i.e. full URL is "$git_base_url/$project"
59our @git_base_url_list = ("++GITWEB_BASE_URL++");
60
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020061# default blob_plain mimetype and default charset for text/plain blob
Dennis Stosberg4a87b432006-06-21 15:07:08 +020062our $default_blob_plain_mimetype = 'text/plain';
63our $default_text_plain_charset = undef;
Jakub Narebskif5aa79d2006-06-17 13:32:15 +020064
Petr Baudis2d007372006-06-18 00:01:06 +020065# file to use for guessing MIME types before trying /etc/mime.types
66# (relative to the current git repository)
Dennis Stosberg4a87b432006-06-21 15:07:08 +020067our $mimetypes_file = undef;
Petr Baudis2d007372006-06-18 00:01:06 +020068
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053069# You define site-wide feature defaults here; override them with
70# $GITWEB_CONFIG as necessary.
Jakub Narebski952c65f2006-08-22 16:52:50 +020071our %feature = (
Jakub Narebski17848fc2006-08-26 19:14:22 +020072 # feature => {
73 # 'sub' => feature-sub (subroutine),
74 # 'override' => allow-override (boolean),
75 # 'default' => [ default options...] (array reference)}
76 #
77 # if feature is overridable (it means that allow-override has true value,
78 # then feature-sub will be called with default options as parameters;
79 # return value of feature-sub indicates if to enable specified feature
80 #
81 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053082
Jakub Narebski952c65f2006-08-22 16:52:50 +020083 'blame' => {
84 'sub' => \&feature_blame,
85 'override' => 0,
86 'default' => [0]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053087
Jakub Narebski952c65f2006-08-22 16:52:50 +020088 'snapshot' => {
89 'sub' => \&feature_snapshot,
90 'override' => 0,
91 # => [content-encoding, suffix, program]
92 'default' => ['x-gzip', 'gz', 'gzip']},
Jakub Narebski04f7a942006-09-11 00:29:27 +020093
94 'pickaxe' => {
95 'sub' => \&feature_pickaxe,
96 'override' => 0,
97 'default' => [1]},
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +053098);
99
100sub gitweb_check_feature {
101 my ($name) = @_;
102 return undef unless exists $feature{$name};
Jakub Narebski952c65f2006-08-22 16:52:50 +0200103 my ($sub, $override, @defaults) = (
104 $feature{$name}{'sub'},
105 $feature{$name}{'override'},
106 @{$feature{$name}{'default'}});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530107 if (!$override) { return @defaults; }
108 return $sub->(@defaults);
109}
110
111# To enable system wide have in $GITWEB_CONFIG
Jakub Narebski17848fc2006-08-26 19:14:22 +0200112# $feature{'blame'}{'default'} = [1];
113# To have project specific config enable override in $GITWEB_CONFIG
114# $feature{'blame'}{'override'} = 1;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530115# and in project config gitweb.blame = 0|1;
116
117sub feature_blame {
118 my ($val) = git_get_project_config('blame', '--bool');
119
120 if ($val eq 'true') {
121 return 1;
122 } elsif ($val eq 'false') {
123 return 0;
124 }
125
126 return $_[0];
127}
128
129# To disable system wide have in $GITWEB_CONFIG
Jakub Narebski17848fc2006-08-26 19:14:22 +0200130# $feature{'snapshot'}{'default'} = [undef];
131# To have project specific config enable override in $GITWEB_CONFIG
132# $feature{'blame'}{'override'} = 1;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530133# and in project config gitweb.snapshot = none|gzip|bzip2
134
135sub feature_snapshot {
136 my ($ctype, $suffix, $command) = @_;
137
138 my ($val) = git_get_project_config('snapshot');
139
140 if ($val eq 'gzip') {
141 return ('x-gzip', 'gz', 'gzip');
142 } elsif ($val eq 'bzip2') {
143 return ('x-bzip2', 'bz2', 'bzip2');
144 } elsif ($val eq 'none') {
145 return ();
146 }
147
148 return ($ctype, $suffix, $command);
149}
150
Jakub Narebski04f7a942006-09-11 00:29:27 +0200151# To enable system wide have in $GITWEB_CONFIG
152# $feature{'pickaxe'}{'default'} = [1];
153# To have project specific config enable override in $GITWEB_CONFIG
154# $feature{'pickaxe'}{'override'} = 1;
155# and in project config gitweb.pickaxe = 0|1;
156
157sub feature_pickaxe {
158 my ($val) = git_get_project_config('pickaxe', '--bool');
159
160 if ($val eq 'true') {
161 return (1);
162 } elsif ($val eq 'false') {
163 return (0);
164 }
165
166 return ($_[0]);
167}
168
Jakub Narebski6bcf4b42006-08-27 23:49:36 +0200169# rename detection options for git-diff and git-diff-tree
170# - default is '-M', with the cost proportional to
171# (number of removed files) * (number of new files).
172# - more costly is '-C' (or '-C', '-M'), with the cost proportional to
173# (number of changed files + number of removed files) * (number of new files)
174# - even more costly is '-C', '--find-copies-harder' with cost
175# (number of files in the original tree) * (number of new files)
176# - one might want to include '-B' option, e.g. '-B', '-M'
177our @diff_opts = ('-M'); # taken from git_commit
178
Junio C Hamano06c084d2006-08-02 13:50:20 -0700179our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
Dennis Stosberg4b5dc982006-08-29 09:19:02 +0200180do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400181
182# version of the core git binary
183our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
184
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200185# path to the current git repository
186our $git_dir;
187
Jeff Kingc8d138a2006-08-02 15:23:34 -0400188$projects_list ||= $projectroot;
Jeff Kingc8d138a2006-08-02 15:23:34 -0400189
Jakub Narebski154b4d72006-08-05 12:55:20 +0200190# ======================================================================
Kay Sievers09bd7892005-08-07 20:21:23 +0200191# input validation and dispatch
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200192our $action = $cgi->param('a');
Kay Sievers09bd7892005-08-07 20:21:23 +0200193if (defined $action) {
Kay Sieversc91da262005-09-03 14:50:33 +0200194 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200195 die_error(undef, "Invalid action parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200196 }
Kay Sieversb87d78d2005-08-07 20:21:04 +0200197}
198
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200199our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
Matthias Lederhoferdd04c422006-08-06 13:25:41 +0200200if (defined $project) {
201 $project =~ s|^/||;
202 $project =~ s|/$||;
Martin Waitz13d02162006-08-17 00:28:40 +0200203 $project = undef unless $project;
Matthias Lederhoferdd04c422006-08-06 13:25:41 +0200204}
Martin Waitz13d02162006-08-17 00:28:40 +0200205if (defined $project) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200206 if (!validate_input($project)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200207 die_error(undef, "Invalid project parameter");
Kay Sievers9cd3d982005-08-07 20:17:42 +0200208 }
209 if (!(-d "$projectroot/$project")) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200210 die_error(undef, "No such directory");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200211 }
212 if (!(-e "$projectroot/$project/HEAD")) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200213 die_error(undef, "No such project");
Kay Sievers9cd3d982005-08-07 20:17:42 +0200214 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200215 $git_dir = "$projectroot/$project";
Kay Sievers2ad93312005-08-07 20:14:48 +0200216}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200217
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200218our $file_name = $cgi->param('f');
Kay Sieversb87d78d2005-08-07 20:21:04 +0200219if (defined $file_name) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200220 if (!validate_input($file_name)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200221 die_error(undef, "Invalid file parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200222 }
Kay Sieversd51e9022005-08-07 20:16:07 +0200223}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200224
Martin Waitz5c95fab2006-08-17 00:28:38 +0200225our $file_parent = $cgi->param('fp');
226if (defined $file_parent) {
227 if (!validate_input($file_parent)) {
228 die_error(undef, "Invalid file parent parameter");
229 }
230}
231
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200232our $hash = $cgi->param('h');
Kay Sievers4fac5292005-08-07 20:27:38 +0200233if (defined $hash) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200234 if (!validate_input($hash)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200235 die_error(undef, "Invalid hash parameter");
Kay Sievers4fac5292005-08-07 20:27:38 +0200236 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200237}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200238
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200239our $hash_parent = $cgi->param('hp');
Kay Sieversc91da262005-09-03 14:50:33 +0200240if (defined $hash_parent) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200241 if (!validate_input($hash_parent)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200242 die_error(undef, "Invalid hash parent parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200243 }
Kay Sievers09bd7892005-08-07 20:21:23 +0200244}
245
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200246our $hash_base = $cgi->param('hb');
Kay Sieversc91da262005-09-03 14:50:33 +0200247if (defined $hash_base) {
Jakub Narebskidbd954a2006-08-05 12:58:06 +0200248 if (!validate_input($hash_base)) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200249 die_error(undef, "Invalid hash base parameter");
Kay Sieversc91da262005-09-03 14:50:33 +0200250 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200251}
Kay Sievers6191f8e2005-08-07 20:19:56 +0200252
Jakub Narebski420e92f2006-08-24 23:53:54 +0200253our $hash_parent_base = $cgi->param('hpb');
254if (defined $hash_parent_base) {
255 if (!validate_input($hash_parent_base)) {
256 die_error(undef, "Invalid hash parent base parameter");
257 }
258}
259
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200260our $page = $cgi->param('pg');
Kay Sieversea4a6df2005-08-07 20:26:49 +0200261if (defined $page) {
Kay Sieversc91da262005-09-03 14:50:33 +0200262 if ($page =~ m/[^0-9]$/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200263 die_error(undef, "Invalid page parameter");
Kay Sieversb87d78d2005-08-07 20:21:04 +0200264 }
Kay Sieversa59d4af2005-08-07 20:15:44 +0200265}
Kay Sievers823d5dc2005-08-07 19:57:58 +0200266
Dennis Stosberg4a87b432006-06-21 15:07:08 +0200267our $searchtext = $cgi->param('s');
Kay Sievers19806692005-08-07 20:26:27 +0200268if (defined $searchtext) {
269 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200270 die_error(undef, "Invalid search parameter");
Kay Sievers19806692005-08-07 20:26:27 +0200271 }
272 $searchtext = quotemeta $searchtext;
273}
274
Jakub Narebski717b8312006-07-31 21:22:15 +0200275# dispatch
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200276my %actions = (
277 "blame" => \&git_blame2,
278 "blobdiff" => \&git_blobdiff,
279 "blobdiff_plain" => \&git_blobdiff_plain,
280 "blob" => \&git_blob,
281 "blob_plain" => \&git_blob_plain,
282 "commitdiff" => \&git_commitdiff,
283 "commitdiff_plain" => \&git_commitdiff_plain,
284 "commit" => \&git_commit,
285 "heads" => \&git_heads,
286 "history" => \&git_history,
287 "log" => \&git_log,
288 "rss" => \&git_rss,
289 "search" => \&git_search,
290 "shortlog" => \&git_shortlog,
291 "summary" => \&git_summary,
292 "tag" => \&git_tag,
293 "tags" => \&git_tags,
294 "tree" => \&git_tree,
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +0530295 "snapshot" => \&git_snapshot,
Jakub Narebski77a153f2006-08-22 16:59:20 +0200296 # those below don't need $project
297 "opml" => \&git_opml,
298 "project_list" => \&git_project_list,
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200299);
300
Jakub Narebski77a153f2006-08-22 16:59:20 +0200301if (defined $project) {
302 $action ||= 'summary';
303} else {
304 $action ||= 'project_list';
305}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200306if (!defined($actions{$action})) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200307 die_error(undef, "Unknown action");
Kay Sievers09bd7892005-08-07 20:21:23 +0200308}
Matthias Lederhofer8e85cdc2006-07-31 23:46:25 +0200309$actions{$action}->();
310exit;
Kay Sievers09bd7892005-08-07 20:21:23 +0200311
Jakub Narebski717b8312006-07-31 21:22:15 +0200312## ======================================================================
Martin Waitz06a9d862006-08-16 00:23:50 +0200313## action links
314
315sub href(%) {
Jakub Narebski498fe002006-08-22 19:05:25 +0200316 my %params = @_;
317
318 my @mapping = (
Martin Waitz06a9d862006-08-16 00:23:50 +0200319 project => "p",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200320 action => "a",
Martin Waitz06a9d862006-08-16 00:23:50 +0200321 file_name => "f",
Martin Waitz5c95fab2006-08-17 00:28:38 +0200322 file_parent => "fp",
Martin Waitz06a9d862006-08-16 00:23:50 +0200323 hash => "h",
324 hash_parent => "hp",
325 hash_base => "hb",
Jakub Narebski420e92f2006-08-24 23:53:54 +0200326 hash_parent_base => "hpb",
Martin Waitz06a9d862006-08-16 00:23:50 +0200327 page => "pg",
328 searchtext => "s",
329 );
Jakub Narebski498fe002006-08-22 19:05:25 +0200330 my %mapping = @mapping;
Martin Waitz06a9d862006-08-16 00:23:50 +0200331
Martin Waitz06a9d862006-08-16 00:23:50 +0200332 $params{"project"} ||= $project;
333
Jakub Narebski498fe002006-08-22 19:05:25 +0200334 my @result = ();
335 for (my $i = 0; $i < @mapping; $i += 2) {
336 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
337 if (defined $params{$name}) {
338 push @result, $symbol . "=" . esc_param($params{$name});
339 }
340 }
341 return "$my_uri?" . join(';', @result);
Martin Waitz06a9d862006-08-16 00:23:50 +0200342}
343
344
345## ======================================================================
Jakub Narebski717b8312006-07-31 21:22:15 +0200346## validation, quoting/unquoting and escaping
347
348sub validate_input {
349 my $input = shift;
350
351 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
352 return $input;
353 }
354 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
355 return undef;
356 }
357 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
358 return undef;
359 }
360 return $input;
361}
362
Kay Sievers232ff552005-11-24 16:56:55 +0100363# quote unsafe chars, but keep the slash, even when it's not
364# correct, but quoted slashes look too horrible in bookmarks
365sub esc_param {
Kay Sievers353347b2005-11-14 05:47:18 +0100366 my $str = shift;
Kay Sievers232ff552005-11-24 16:56:55 +0100367 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
Kay Sievers18216712005-11-14 06:10:07 +0100368 $str =~ s/\+/%2B/g;
Kay Sieversa9e60b72005-11-14 15:15:12 +0100369 $str =~ s/ /\+/g;
Kay Sievers353347b2005-11-14 05:47:18 +0100370 return $str;
371}
372
Kay Sievers232ff552005-11-24 16:56:55 +0100373# replace invalid utf8 character with SUBSTITUTION sequence
Kay Sievers40c13812005-11-19 17:41:29 +0100374sub esc_html {
375 my $str = shift;
Kay Sievers40c13812005-11-19 17:41:29 +0100376 $str = decode("utf8", $str, Encode::FB_DEFAULT);
Kay Sievers10bb9032005-11-23 04:26:40 +0100377 $str = escapeHTML($str);
Jakub Narebski3dc13832006-07-30 15:02:27 +0200378 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
Kay Sievers40c13812005-11-19 17:41:29 +0100379 return $str;
380}
381
Kay Sievers232ff552005-11-24 16:56:55 +0100382# git may return quoted and escaped filenames
383sub unquote {
384 my $str = shift;
385 if ($str =~ m/^"(.*)"$/) {
386 $str = $1;
387 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
388 }
389 return $str;
390}
391
Jakub Narebskif16db172006-08-06 02:08:31 +0200392# escape tabs (convert tabs to spaces)
393sub untabify {
394 my $line = shift;
395
396 while ((my $pos = index($line, "\t")) != -1) {
397 if (my $count = (8 - ($pos % 8))) {
398 my $spaces = ' ' x $count;
399 $line =~ s/\t/$spaces/;
400 }
401 }
402
403 return $line;
404}
405
Jakub Narebski717b8312006-07-31 21:22:15 +0200406## ----------------------------------------------------------------------
407## HTML aware string manipulation
408
409sub chop_str {
410 my $str = shift;
411 my $len = shift;
412 my $add_len = shift || 10;
413
414 # allow only $len chars, but don't cut a word if it would fit in $add_len
415 # if it doesn't fit, cut it if it's still longer than the dots we would add
416 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
417 my $body = $1;
418 my $tail = $2;
419 if (length($tail) > 4) {
420 $tail = " ...";
421 $body =~ s/&[^;]*$//; # remove chopped character entities
422 }
423 return "$body$tail";
424}
425
426## ----------------------------------------------------------------------
427## functions returning short strings
428
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +0000429# CSS class for given age value (in seconds)
430sub age_class {
431 my $age = shift;
432
433 if ($age < 60*60*2) {
434 return "age0";
435 } elsif ($age < 60*60*24*2) {
436 return "age1";
437 } else {
438 return "age2";
439 }
440}
441
Jakub Narebski717b8312006-07-31 21:22:15 +0200442# convert age in seconds to "nn units ago" string
443sub age_string {
444 my $age = shift;
445 my $age_str;
446
447 if ($age > 60*60*24*365*2) {
448 $age_str = (int $age/60/60/24/365);
449 $age_str .= " years ago";
450 } elsif ($age > 60*60*24*(365/12)*2) {
451 $age_str = int $age/60/60/24/(365/12);
452 $age_str .= " months ago";
453 } elsif ($age > 60*60*24*7*2) {
454 $age_str = int $age/60/60/24/7;
455 $age_str .= " weeks ago";
456 } elsif ($age > 60*60*24*2) {
457 $age_str = int $age/60/60/24;
458 $age_str .= " days ago";
459 } elsif ($age > 60*60*2) {
460 $age_str = int $age/60/60;
461 $age_str .= " hours ago";
462 } elsif ($age > 60*2) {
463 $age_str = int $age/60;
464 $age_str .= " min ago";
465 } elsif ($age > 2) {
466 $age_str = int $age;
467 $age_str .= " sec ago";
468 } else {
469 $age_str .= " right now";
470 }
471 return $age_str;
472}
473
474# convert file mode in octal to symbolic file mode string
475sub mode_str {
476 my $mode = oct shift;
477
478 if (S_ISDIR($mode & S_IFMT)) {
479 return 'drwxr-xr-x';
480 } elsif (S_ISLNK($mode)) {
481 return 'lrwxrwxrwx';
482 } elsif (S_ISREG($mode)) {
483 # git cares only about the executable bit
484 if ($mode & S_IXUSR) {
485 return '-rwxr-xr-x';
486 } else {
487 return '-rw-r--r--';
488 };
489 } else {
490 return '----------';
491 }
492}
493
494# convert file mode in octal to file type string
495sub file_type {
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +0200496 my $mode = shift;
497
498 if ($mode !~ m/^[0-7]+$/) {
499 return $mode;
500 } else {
501 $mode = oct $mode;
502 }
Jakub Narebski717b8312006-07-31 21:22:15 +0200503
504 if (S_ISDIR($mode & S_IFMT)) {
505 return "directory";
506 } elsif (S_ISLNK($mode)) {
507 return "symlink";
508 } elsif (S_ISREG($mode)) {
509 return "file";
510 } else {
511 return "unknown";
512 }
513}
514
515## ----------------------------------------------------------------------
516## functions returning short HTML fragments, or transforming HTML fragments
517## which don't beling to other sections
518
519# format line of commit message or tag comment
520sub format_log_line_html {
521 my $line = shift;
522
523 $line = esc_html($line);
524 $line =~ s/ /&nbsp;/g;
525 if ($line =~ m/([0-9a-fA-F]{40})/) {
526 my $hash_text = $1;
527 if (git_get_type($hash_text) eq "commit") {
Jakub Narebski952c65f2006-08-22 16:52:50 +0200528 my $link =
529 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
530 -class => "text"}, $hash_text);
Jakub Narebski717b8312006-07-31 21:22:15 +0200531 $line =~ s/$hash_text/$link/;
532 }
533 }
534 return $line;
535}
536
537# format marker of refs pointing to given object
Jakub Narebski847e01f2006-08-14 02:05:47 +0200538sub format_ref_marker {
Jakub Narebski717b8312006-07-31 21:22:15 +0200539 my ($refs, $id) = @_;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200540 my $markers = '';
Jakub Narebski717b8312006-07-31 21:22:15 +0200541
542 if (defined $refs->{$id}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200543 foreach my $ref (@{$refs->{$id}}) {
544 my ($type, $name) = qw();
545 # e.g. tags/v2.6.11 or heads/next
546 if ($ref =~ m!^(.*?)s?/(.*)$!) {
547 $type = $1;
548 $name = $2;
549 } else {
550 $type = "ref";
551 $name = $ref;
552 }
553
554 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
555 }
556 }
557
558 if ($markers) {
559 return ' <span class="refs">'. $markers . '</span>';
Jakub Narebski717b8312006-07-31 21:22:15 +0200560 } else {
561 return "";
562 }
563}
564
Jakub Narebski17d07442006-08-14 02:08:27 +0200565# format, perhaps shortened and with markers, title line
566sub format_subject_html {
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200567 my ($long, $short, $href, $extra) = @_;
Jakub Narebski17d07442006-08-14 02:08:27 +0200568 $extra = '' unless defined($extra);
569
570 if (length($short) < length($long)) {
Jakub Narebski7c278012006-08-22 12:02:48 +0200571 return $cgi->a({-href => $href, -class => "list subject",
Martin Waitz1c2a4f52006-08-16 00:24:30 +0200572 -title => $long},
Jakub Narebski17d07442006-08-14 02:08:27 +0200573 esc_html($short) . $extra);
574 } else {
Jakub Narebski7c278012006-08-22 12:02:48 +0200575 return $cgi->a({-href => $href, -class => "list subject"},
Jakub Narebski17d07442006-08-14 02:08:27 +0200576 esc_html($long) . $extra);
577 }
578}
579
Jakub Narebskieee08902006-08-24 00:15:14 +0200580sub format_diff_line {
581 my $line = shift;
582 my $char = substr($line, 0, 1);
583 my $diff_class = "";
584
585 chomp $line;
586
587 if ($char eq '+') {
588 $diff_class = " add";
589 } elsif ($char eq "-") {
590 $diff_class = " rem";
591 } elsif ($char eq "@") {
592 $diff_class = " chunk_header";
593 } elsif ($char eq "\\") {
Jakub Narebskiaf33ef22006-08-24 01:58:49 +0200594 $diff_class = " incomplete";
Jakub Narebskieee08902006-08-24 00:15:14 +0200595 }
596 $line = untabify($line);
597 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
598}
599
Jakub Narebski717b8312006-07-31 21:22:15 +0200600## ----------------------------------------------------------------------
601## git utility subroutines, invoking git commands
602
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200603# returns path to the core git executable and the --git-dir parameter as list
604sub git_cmd {
605 return $GIT, '--git-dir='.$git_dir;
606}
607
608# returns path to the core git executable and the --git-dir parameter as string
609sub git_cmd_str {
610 return join(' ', git_cmd());
611}
612
Jakub Narebski717b8312006-07-31 21:22:15 +0200613# get HEAD ref of given project as hash
Jakub Narebski847e01f2006-08-14 02:05:47 +0200614sub git_get_head_hash {
Jakub Narebski717b8312006-07-31 21:22:15 +0200615 my $project = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200616 my $o_git_dir = $git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200617 my $retval = undef;
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200618 $git_dir = "$projectroot/$project";
619 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
Jakub Narebski717b8312006-07-31 21:22:15 +0200620 my $head = <$fd>;
621 close $fd;
622 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
623 $retval = $1;
624 }
625 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200626 if (defined $o_git_dir) {
627 $git_dir = $o_git_dir;
Jakub Narebski717b8312006-07-31 21:22:15 +0200628 }
629 return $retval;
630}
631
632# get type of given object
633sub git_get_type {
634 my $hash = shift;
635
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200636 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200637 my $type = <$fd>;
638 close $fd or return;
639 chomp $type;
640 return $type;
641}
642
643sub git_get_project_config {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530644 my ($key, $type) = @_;
Jakub Narebski717b8312006-07-31 21:22:15 +0200645
646 return unless ($key);
647 $key =~ s/^gitweb\.//;
648 return if ($key =~ m/\W/);
649
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200650 my @x = (git_cmd(), 'repo-config');
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +0530651 if (defined $type) { push @x, $type; }
652 push @x, "--get";
653 push @x, "gitweb.$key";
654 my $val = qx(@x);
655 chomp $val;
Jakub Narebski717b8312006-07-31 21:22:15 +0200656 return ($val);
657}
658
Jakub Narebski717b8312006-07-31 21:22:15 +0200659# get hash of given path at given ref
660sub git_get_hash_by_path {
661 my $base = shift;
662 my $path = shift || return undef;
663
664 my $tree = $base;
665
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200666 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
Jakub Narebskicac4bd92006-08-05 13:13:53 +0200667 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski717b8312006-07-31 21:22:15 +0200668 my $line = <$fd>;
669 close $fd or return undef;
670
671 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
672 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
673 return $3;
674}
675
676## ......................................................................
677## git utility functions, directly accessing git repository
678
679# assumes that PATH is not symref
Jakub Narebski847e01f2006-08-14 02:05:47 +0200680sub git_get_hash_by_ref {
Jakub Narebski717b8312006-07-31 21:22:15 +0200681 my $path = shift;
682
683 open my $fd, "$projectroot/$path" or return undef;
684 my $head = <$fd>;
685 close $fd;
686 chomp $head;
687 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
688 return $head;
689 }
690}
691
Jakub Narebski847e01f2006-08-14 02:05:47 +0200692sub git_get_project_description {
Jakub Narebski717b8312006-07-31 21:22:15 +0200693 my $path = shift;
694
695 open my $fd, "$projectroot/$path/description" or return undef;
696 my $descr = <$fd>;
697 close $fd;
698 chomp $descr;
699 return $descr;
700}
701
Jakub Narebskie79ca7c2006-08-16 14:50:34 +0200702sub git_get_project_url_list {
703 my $path = shift;
704
705 open my $fd, "$projectroot/$path/cloneurl" or return undef;
706 my @git_project_url_list = map { chomp; $_ } <$fd>;
707 close $fd;
708
709 return wantarray ? @git_project_url_list : \@git_project_url_list;
710}
711
Jakub Narebski847e01f2006-08-14 02:05:47 +0200712sub git_get_projects_list {
Jakub Narebski717b8312006-07-31 21:22:15 +0200713 my @list;
714
715 if (-d $projects_list) {
716 # search in directory
717 my $dir = $projects_list;
718 opendir my ($dh), $dir or return undef;
719 while (my $dir = readdir($dh)) {
720 if (-e "$projectroot/$dir/HEAD") {
721 my $pr = {
722 path => $dir,
723 };
724 push @list, $pr
725 }
726 }
727 closedir($dh);
728 } elsif (-f $projects_list) {
729 # read from file(url-encoded):
730 # 'git%2Fgit.git Linus+Torvalds'
731 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
732 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
733 open my ($fd), $projects_list or return undef;
734 while (my $line = <$fd>) {
735 chomp $line;
736 my ($path, $owner) = split ' ', $line;
737 $path = unescape($path);
738 $owner = unescape($owner);
739 if (!defined $path) {
740 next;
741 }
742 if (-e "$projectroot/$path/HEAD") {
743 my $pr = {
744 path => $path,
745 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
746 };
747 push @list, $pr
748 }
749 }
750 close $fd;
751 }
752 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
753 return @list;
754}
755
Jakub Narebski1e0cf032006-08-14 02:10:06 +0200756sub git_get_project_owner {
757 my $project = shift;
758 my $owner;
759
760 return undef unless $project;
761
762 # read from file (url-encoded):
763 # 'git%2Fgit.git Linus+Torvalds'
764 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
765 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
766 if (-f $projects_list) {
767 open (my $fd , $projects_list);
768 while (my $line = <$fd>) {
769 chomp $line;
770 my ($pr, $ow) = split ' ', $line;
771 $pr = unescape($pr);
772 $ow = unescape($ow);
773 if ($pr eq $project) {
774 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
775 last;
776 }
777 }
778 close $fd;
779 }
780 if (!defined $owner) {
781 $owner = get_file_owner("$projectroot/$project");
782 }
783
784 return $owner;
785}
786
Jakub Narebski847e01f2006-08-14 02:05:47 +0200787sub git_get_references {
Jakub Narebski717b8312006-07-31 21:22:15 +0200788 my $type = shift || "";
789 my %refs;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200790 my $fd;
Jakub Narebski717b8312006-07-31 21:22:15 +0200791 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
792 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200793 if (-f "$projectroot/$project/info/refs") {
794 open $fd, "$projectroot/$project/info/refs"
795 or return;
796 } else {
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200797 open $fd, "-|", git_cmd(), "ls-remote", "."
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200798 or return;
799 }
800
Jakub Narebski717b8312006-07-31 21:22:15 +0200801 while (my $line = <$fd>) {
802 chomp $line;
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200803 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
Jakub Narebski717b8312006-07-31 21:22:15 +0200804 if (defined $refs{$1}) {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200805 push @{$refs{$1}}, $2;
Jakub Narebski717b8312006-07-31 21:22:15 +0200806 } else {
Jakub Narebskid294e1c2006-08-14 02:14:20 +0200807 $refs{$1} = [ $2 ];
Jakub Narebski717b8312006-07-31 21:22:15 +0200808 }
809 }
810 }
811 close $fd or return;
812 return \%refs;
813}
814
Jakub Narebski56a322f2006-08-24 19:41:23 +0200815sub git_get_rev_name_tags {
816 my $hash = shift || return undef;
817
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200818 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
Jakub Narebski56a322f2006-08-24 19:41:23 +0200819 or return;
820 my $name_rev = <$fd>;
821 close $fd;
822
823 if ($name_rev =~ m|^$hash tags/(.*)$|) {
824 return $1;
825 } else {
826 # catches also '$hash undefined' output
827 return undef;
828 }
829}
830
Jakub Narebski717b8312006-07-31 21:22:15 +0200831## ----------------------------------------------------------------------
832## parse to hash functions
833
Jakub Narebski847e01f2006-08-14 02:05:47 +0200834sub parse_date {
Jakub Narebski717b8312006-07-31 21:22:15 +0200835 my $epoch = shift;
836 my $tz = shift || "-0000";
837
838 my %date;
839 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
840 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
841 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
842 $date{'hour'} = $hour;
843 $date{'minute'} = $min;
844 $date{'mday'} = $mday;
845 $date{'day'} = $days[$wday];
846 $date{'month'} = $months[$mon];
Jakub Narebski952c65f2006-08-22 16:52:50 +0200847 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
848 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
849 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
850 $mday, $months[$mon], $hour ,$min;
Jakub Narebski717b8312006-07-31 21:22:15 +0200851
852 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
853 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
854 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
855 $date{'hour_local'} = $hour;
856 $date{'minute_local'} = $min;
857 $date{'tz_local'} = $tz;
858 return %date;
859}
860
Jakub Narebski847e01f2006-08-14 02:05:47 +0200861sub parse_tag {
Jakub Narebski717b8312006-07-31 21:22:15 +0200862 my $tag_id = shift;
863 my %tag;
864 my @comment;
865
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200866 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200867 $tag{'id'} = $tag_id;
868 while (my $line = <$fd>) {
869 chomp $line;
870 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
871 $tag{'object'} = $1;
872 } elsif ($line =~ m/^type (.+)$/) {
873 $tag{'type'} = $1;
874 } elsif ($line =~ m/^tag (.+)$/) {
875 $tag{'name'} = $1;
876 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
877 $tag{'author'} = $1;
878 $tag{'epoch'} = $2;
879 $tag{'tz'} = $3;
880 } elsif ($line =~ m/--BEGIN/) {
881 push @comment, $line;
882 last;
883 } elsif ($line eq "") {
884 last;
885 }
886 }
887 push @comment, <$fd>;
888 $tag{'comment'} = \@comment;
889 close $fd or return;
890 if (!defined $tag{'name'}) {
891 return
892 };
893 return %tag
894}
895
Jakub Narebski847e01f2006-08-14 02:05:47 +0200896sub parse_commit {
Jakub Narebski717b8312006-07-31 21:22:15 +0200897 my $commit_id = shift;
898 my $commit_text = shift;
899
900 my @commit_lines;
901 my %co;
902
903 if (defined $commit_text) {
904 @commit_lines = @$commit_text;
905 } else {
906 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +0200907 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
Jakub Narebski952c65f2006-08-22 16:52:50 +0200908 or return;
Jakub Narebski717b8312006-07-31 21:22:15 +0200909 @commit_lines = split '\n', <$fd>;
910 close $fd or return;
911 $/ = "\n";
912 pop @commit_lines;
913 }
914 my $header = shift @commit_lines;
915 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
916 return;
917 }
918 ($co{'id'}, my @parents) = split ' ', $header;
919 $co{'parents'} = \@parents;
920 $co{'parent'} = $parents[0];
921 while (my $line = shift @commit_lines) {
922 last if $line eq "\n";
923 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
924 $co{'tree'} = $1;
925 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
926 $co{'author'} = $1;
927 $co{'author_epoch'} = $2;
928 $co{'author_tz'} = $3;
929 if ($co{'author'} =~ m/^([^<]+) </) {
930 $co{'author_name'} = $1;
931 } else {
932 $co{'author_name'} = $co{'author'};
933 }
934 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
935 $co{'committer'} = $1;
936 $co{'committer_epoch'} = $2;
937 $co{'committer_tz'} = $3;
938 $co{'committer_name'} = $co{'committer'};
939 $co{'committer_name'} =~ s/ <.*//;
940 }
941 }
942 if (!defined $co{'tree'}) {
943 return;
944 };
945
946 foreach my $title (@commit_lines) {
947 $title =~ s/^ //;
948 if ($title ne "") {
949 $co{'title'} = chop_str($title, 80, 5);
950 # remove leading stuff of merges to make the interesting part visible
951 if (length($title) > 50) {
952 $title =~ s/^Automatic //;
953 $title =~ s/^merge (of|with) /Merge ... /i;
954 if (length($title) > 50) {
955 $title =~ s/(http|rsync):\/\///;
956 }
957 if (length($title) > 50) {
958 $title =~ s/(master|www|rsync)\.//;
959 }
960 if (length($title) > 50) {
961 $title =~ s/kernel.org:?//;
962 }
963 if (length($title) > 50) {
964 $title =~ s/\/pub\/scm//;
965 }
966 }
967 $co{'title_short'} = chop_str($title, 50, 5);
968 last;
969 }
970 }
971 # remove added spaces
972 foreach my $line (@commit_lines) {
973 $line =~ s/^ //;
974 }
975 $co{'comment'} = \@commit_lines;
976
977 my $age = time - $co{'committer_epoch'};
978 $co{'age'} = $age;
979 $co{'age_string'} = age_string($age);
980 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
981 if ($age > 60*60*24*7*2) {
982 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
983 $co{'age_string_age'} = $co{'age_string'};
984 } else {
985 $co{'age_string_date'} = $co{'age_string'};
986 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
987 }
988 return %co;
989}
990
Jakub Narebskia446d6b2006-08-14 02:07:00 +0200991# parse ref from ref_file, given by ref_id, with given type
992sub parse_ref {
993 my $ref_file = shift;
994 my $ref_id = shift;
995 my $type = shift || git_get_type($ref_id);
996 my %ref_item;
997
998 $ref_item{'type'} = $type;
999 $ref_item{'id'} = $ref_id;
1000 $ref_item{'epoch'} = 0;
1001 $ref_item{'age'} = "unknown";
1002 if ($type eq "tag") {
1003 my %tag = parse_tag($ref_id);
1004 $ref_item{'comment'} = $tag{'comment'};
1005 if ($tag{'type'} eq "commit") {
1006 my %co = parse_commit($tag{'object'});
1007 $ref_item{'epoch'} = $co{'committer_epoch'};
1008 $ref_item{'age'} = $co{'age_string'};
1009 } elsif (defined($tag{'epoch'})) {
1010 my $age = time - $tag{'epoch'};
1011 $ref_item{'epoch'} = $tag{'epoch'};
1012 $ref_item{'age'} = age_string($age);
1013 }
1014 $ref_item{'reftype'} = $tag{'type'};
1015 $ref_item{'name'} = $tag{'name'};
1016 $ref_item{'refid'} = $tag{'object'};
1017 } elsif ($type eq "commit"){
1018 my %co = parse_commit($ref_id);
1019 $ref_item{'reftype'} = "commit";
1020 $ref_item{'name'} = $ref_file;
1021 $ref_item{'title'} = $co{'title'};
1022 $ref_item{'refid'} = $ref_id;
1023 $ref_item{'epoch'} = $co{'committer_epoch'};
1024 $ref_item{'age'} = $co{'age_string'};
1025 } else {
1026 $ref_item{'reftype'} = $type;
1027 $ref_item{'name'} = $ref_file;
1028 $ref_item{'refid'} = $ref_id;
1029 }
1030
1031 return %ref_item;
1032}
1033
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001034# parse line of git-diff-tree "raw" output
Jakub Narebski740e67f2006-08-21 23:07:00 +02001035sub parse_difftree_raw_line {
1036 my $line = shift;
1037 my %res;
1038
1039 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1040 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1041 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1042 $res{'from_mode'} = $1;
1043 $res{'to_mode'} = $2;
1044 $res{'from_id'} = $3;
1045 $res{'to_id'} = $4;
1046 $res{'status'} = $5;
1047 $res{'similarity'} = $6;
1048 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001049 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
Jakub Narebski740e67f2006-08-21 23:07:00 +02001050 } else {
1051 $res{'file'} = unquote($7);
1052 }
1053 }
1054 # 'c512b523472485aef4fff9e57b229d9d243c967f'
Jakub Narebski0edcb372006-08-31 00:36:04 +02001055 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1056 $res{'commit'} = $1;
1057 }
Jakub Narebski740e67f2006-08-21 23:07:00 +02001058
1059 return wantarray ? %res : \%res;
1060}
1061
Jakub Narebskicb849b42006-08-31 00:32:15 +02001062# parse line of git-ls-tree output
1063sub parse_ls_tree_line ($;%) {
1064 my $line = shift;
1065 my %opts = @_;
1066 my %res;
1067
1068 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1069 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1070
1071 $res{'mode'} = $1;
1072 $res{'type'} = $2;
1073 $res{'hash'} = $3;
1074 if ($opts{'-z'}) {
1075 $res{'name'} = $4;
1076 } else {
1077 $res{'name'} = unquote($4);
1078 }
1079
1080 return wantarray ? %res : \%res;
1081}
1082
Jakub Narebski717b8312006-07-31 21:22:15 +02001083## ......................................................................
1084## parse to array of hashes functions
1085
Jakub Narebski847e01f2006-08-14 02:05:47 +02001086sub git_get_refs_list {
Jakub Narebski717b8312006-07-31 21:22:15 +02001087 my $ref_dir = shift;
1088 my @reflist;
1089
1090 my @refs;
Junio C Hamano7a13b992006-07-31 19:18:34 -07001091 my $pfxlen = length("$projectroot/$project/$ref_dir");
1092 File::Find::find(sub {
1093 return if (/^\./);
1094 if (-f $_) {
1095 push @refs, substr($File::Find::name, $pfxlen + 1);
Jakub Narebski717b8312006-07-31 21:22:15 +02001096 }
Junio C Hamano7a13b992006-07-31 19:18:34 -07001097 }, "$projectroot/$project/$ref_dir");
1098
Jakub Narebski717b8312006-07-31 21:22:15 +02001099 foreach my $ref_file (@refs) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02001100 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
Jakub Narebski717b8312006-07-31 21:22:15 +02001101 my $type = git_get_type($ref_id) || next;
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001102 my %ref_item = parse_ref($ref_file, $ref_id, $type);
Jakub Narebski717b8312006-07-31 21:22:15 +02001103
1104 push @reflist, \%ref_item;
1105 }
Jakub Narebskia446d6b2006-08-14 02:07:00 +02001106 # sort refs by age
Jakub Narebski717b8312006-07-31 21:22:15 +02001107 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1108 return \@reflist;
1109}
1110
1111## ----------------------------------------------------------------------
1112## filesystem-related functions
1113
1114sub get_file_owner {
1115 my $path = shift;
1116
1117 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1118 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1119 if (!defined $gcos) {
1120 return undef;
1121 }
1122 my $owner = $gcos;
1123 $owner =~ s/[,;].*$//;
1124 return decode("utf8", $owner, Encode::FB_DEFAULT);
1125}
1126
1127## ......................................................................
1128## mimetype related functions
1129
1130sub mimetype_guess_file {
1131 my $filename = shift;
1132 my $mimemap = shift;
1133 -r $mimemap or return undef;
1134
1135 my %mimemap;
1136 open(MIME, $mimemap) or return undef;
1137 while (<MIME>) {
Jakub Narebski618918e2006-08-14 02:15:22 +02001138 next if m/^#/; # skip comments
Jakub Narebski717b8312006-07-31 21:22:15 +02001139 my ($mime, $exts) = split(/\t+/);
Junio C Hamano46b059d2006-07-31 19:24:37 -07001140 if (defined $exts) {
1141 my @exts = split(/\s+/, $exts);
1142 foreach my $ext (@exts) {
1143 $mimemap{$ext} = $mime;
1144 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001145 }
1146 }
1147 close(MIME);
1148
1149 $filename =~ /\.(.*?)$/;
1150 return $mimemap{$1};
1151}
1152
1153sub mimetype_guess {
1154 my $filename = shift;
1155 my $mime;
1156 $filename =~ /\./ or return undef;
1157
1158 if ($mimetypes_file) {
1159 my $file = $mimetypes_file;
Jakub Narebskid5aa50d2006-08-14 02:16:33 +02001160 if ($file !~ m!^/!) { # if it is relative path
1161 # it is relative to project
1162 $file = "$projectroot/$project/$file";
1163 }
Jakub Narebski717b8312006-07-31 21:22:15 +02001164 $mime = mimetype_guess_file($filename, $file);
1165 }
1166 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1167 return $mime;
1168}
1169
Jakub Narebski847e01f2006-08-14 02:05:47 +02001170sub blob_mimetype {
Jakub Narebski717b8312006-07-31 21:22:15 +02001171 my $fd = shift;
1172 my $filename = shift;
1173
1174 if ($filename) {
1175 my $mime = mimetype_guess($filename);
1176 $mime and return $mime;
1177 }
1178
1179 # just in case
1180 return $default_blob_plain_mimetype unless $fd;
1181
1182 if (-T $fd) {
1183 return 'text/plain' .
1184 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1185 } elsif (! $filename) {
1186 return 'application/octet-stream';
1187 } elsif ($filename =~ m/\.png$/i) {
1188 return 'image/png';
1189 } elsif ($filename =~ m/\.gif$/i) {
1190 return 'image/gif';
1191 } elsif ($filename =~ m/\.jpe?g$/i) {
1192 return 'image/jpeg';
1193 } else {
1194 return 'application/octet-stream';
1195 }
1196}
1197
1198## ======================================================================
1199## functions printing HTML: header, footer, error page
1200
Kay Sievers12a88f22005-08-07 20:02:47 +02001201sub git_header_html {
Kay Sieversa59d4af2005-08-07 20:15:44 +02001202 my $status = shift || "200 OK";
Kay Sievers11044292005-10-19 03:18:45 +02001203 my $expires = shift;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001204
Alp Toker49da1da2006-07-11 21:10:26 +01001205 my $title = "$site_name git";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001206 if (defined $project) {
1207 $title .= " - $project";
1208 if (defined $action) {
1209 $title .= "/$action";
Jakub Narebski7bedd9f2006-06-20 06:17:03 +00001210 if (defined $file_name) {
1211 $title .= " - $file_name";
1212 if ($action eq "tree" && $file_name !~ m|/$|) {
1213 $title .= "/";
1214 }
1215 }
Kay Sieversb87d78d2005-08-07 20:21:04 +02001216 }
1217 }
Alp Tokerf6801d62006-07-11 11:19:34 +01001218 my $content_type;
1219 # require explicit support from the UA if we are to send the page as
1220 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1221 # we have to do this because MSIE sometimes globs '*/*', pretending to
1222 # support xhtml+xml but choking when it gets what it asked for.
Jakub Narebski952c65f2006-08-22 16:52:50 +02001223 if (defined $cgi->http('HTTP_ACCEPT') &&
1224 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1225 $cgi->Accept('application/xhtml+xml') != 0) {
Alp Tokerf6801d62006-07-11 11:19:34 +01001226 $content_type = 'application/xhtml+xml';
1227 } else {
1228 $content_type = 'text/html';
1229 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02001230 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1231 -status=> $status, -expires => $expires);
Kay Sieversa59d4af2005-08-07 20:15:44 +02001232 print <<EOF;
Kay Sievers6191f8e2005-08-07 20:19:56 +02001233<?xml version="1.0" encoding="utf-8"?>
Kay Sievers161332a2005-08-07 19:49:46 +02001234<!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 +02001235<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001236<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
Jakub Narebskiae20de52006-06-21 09:48:03 +02001237<!-- git core binaries version $git_version -->
Kay Sievers161332a2005-08-07 19:49:46 +02001238<head>
Alp Tokerf6801d62006-07-11 11:19:34 +01001239<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
Jakub Narebskid4baf9e2006-08-17 11:21:28 +02001240<meta name="generator" content="gitweb/$version git/$git_version"/>
Kay Sieversc994d622005-08-07 20:27:18 +02001241<meta name="robots" content="index, nofollow"/>
Kay Sieversb87d78d2005-08-07 20:21:04 +02001242<title>$title</title>
Jakub Narebskiae20de52006-06-21 09:48:03 +02001243<link rel="stylesheet" type="text/css" href="$stylesheet"/>
Kay Sievers161332a2005-08-07 19:49:46 +02001244EOF
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001245 if (defined $project) {
1246 printf('<link rel="alternate" title="%s log" '.
1247 'href="%s" type="application/rss+xml"/>'."\n",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001248 esc_param($project), href(action=>"rss"));
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001249 }
Jakub Narebski0b5deba2006-09-04 20:32:13 +02001250 if (defined $favicon) {
1251 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1252 }
Jakub Narebski10161352006-08-05 13:18:58 +02001253
Matthias Lederhoferdd04c422006-08-06 13:25:41 +02001254 print "</head>\n" .
1255 "<body>\n" .
Jakub Narebski10161352006-08-05 13:18:58 +02001256 "<div class=\"page_header\">\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001257 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
Martin Waitz281f2f62006-07-31 00:38:39 +02001258 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
Kay Sievers19806692005-08-07 20:26:27 +02001259 "</a>\n";
Yasushi SHOJI2de21fa2006-08-15 07:50:49 +09001260 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001261 if (defined $project) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001262 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
Kay Sieversb87d78d2005-08-07 20:21:04 +02001263 if (defined $action) {
1264 print " / $action";
1265 }
Kay Sievers19806692005-08-07 20:26:27 +02001266 print "\n";
1267 if (!defined $searchtext) {
1268 $searchtext = "";
1269 }
Kay Sieversc39e47d2005-09-17 03:00:21 +02001270 my $search_hash;
Timo Hirvonen4c5c2022006-06-20 16:41:05 +03001271 if (defined $hash_base) {
1272 $search_hash = $hash_base;
1273 } elsif (defined $hash) {
Kay Sieversc39e47d2005-09-17 03:00:21 +02001274 $search_hash = $hash;
1275 } else {
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02001276 $search_hash = "HEAD";
Kay Sieversc39e47d2005-09-17 03:00:21 +02001277 }
Kay Sievers19806692005-08-07 20:26:27 +02001278 $cgi->param("a", "search");
Kay Sieversc39e47d2005-09-17 03:00:21 +02001279 $cgi->param("h", $search_hash);
Kay Sievers353347b2005-11-14 05:47:18 +01001280 print $cgi->startform(-method => "get", -action => $my_uri) .
Kay Sieversc994d622005-08-07 20:27:18 +02001281 "<div class=\"search\">\n" .
1282 $cgi->hidden(-name => "p") . "\n" .
1283 $cgi->hidden(-name => "a") . "\n" .
Kay Sieversc39e47d2005-09-17 03:00:21 +02001284 $cgi->hidden(-name => "h") . "\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02001285 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1286 "</div>" .
1287 $cgi->end_form() . "\n";
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001288 }
1289 print "</div>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02001290}
1291
Kay Sievers12a88f22005-08-07 20:02:47 +02001292sub git_footer_html {
Kay Sievers6191f8e2005-08-07 20:19:56 +02001293 print "<div class=\"page_footer\">\n";
Kay Sieversb87d78d2005-08-07 20:21:04 +02001294 if (defined $project) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02001295 my $descr = git_get_project_description($project);
Kay Sieversb87d78d2005-08-07 20:21:04 +02001296 if (defined $descr) {
Kay Sievers40c13812005-11-19 17:41:29 +01001297 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
Kay Sievers6191f8e2005-08-07 20:19:56 +02001298 }
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001299 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
Kay Sieversc994d622005-08-07 20:27:18 +02001300 } else {
Martin Waitz756d2f02006-08-17 00:28:36 +02001301 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
Kay Sieversff7669a2005-08-07 20:13:02 +02001302 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02001303 print "</div>\n" .
1304 "</body>\n" .
Kay Sievers9cd3d982005-08-07 20:17:42 +02001305 "</html>";
Kay Sievers161332a2005-08-07 19:49:46 +02001306}
1307
Kay Sievers061cc7c2005-08-07 20:15:57 +02001308sub die_error {
1309 my $status = shift || "403 Forbidden";
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02001310 my $error = shift || "Malformed query, file missing or permission denied";
Kay Sievers664f4cc2005-08-07 20:17:19 +02001311
Kay Sieversa59d4af2005-08-07 20:15:44 +02001312 git_header_html($status);
Jakub Narebski59b9f612006-08-22 23:42:53 +02001313 print <<EOF;
1314<div class="page_body">
1315<br /><br />
1316$status - $error
1317<br />
1318</div>
1319EOF
Kay Sieversa59d4af2005-08-07 20:15:44 +02001320 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02001321 exit;
Kay Sieversa59d4af2005-08-07 20:15:44 +02001322}
1323
Jakub Narebski717b8312006-07-31 21:22:15 +02001324## ----------------------------------------------------------------------
1325## functions printing or outputting HTML: navigation
1326
Jakub Narebski847e01f2006-08-14 02:05:47 +02001327sub git_print_page_nav {
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001328 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1329 $extra = '' if !defined $extra; # pager or formats
1330
1331 my @navs = qw(summary shortlog log commit commitdiff tree);
1332 if ($suppress) {
1333 @navs = grep { $_ ne $suppress } @navs;
1334 }
1335
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001336 my %arg = map { $_ => {action=>$_} } @navs;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001337 if (defined $head) {
1338 for (qw(commit commitdiff)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001339 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001340 }
1341 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1342 for (qw(shortlog log)) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001343 $arg{$_}{hash} = $head;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001344 }
1345 }
1346 }
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001347 $arg{tree}{hash} = $treehead if defined $treehead;
1348 $arg{tree}{hash_base} = $treebase if defined $treebase;
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001349
1350 print "<div class=\"page_nav\">\n" .
1351 (join " | ",
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001352 map { $_ eq $current ?
1353 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1354 } @navs);
Jakub Narebski898a8932006-07-30 16:14:43 +02001355 print "<br/>\n$extra<br/>\n" .
Jakub Narebskib18f9bf2006-07-30 14:59:57 +02001356 "</div>\n";
1357}
1358
Jakub Narebski847e01f2006-08-14 02:05:47 +02001359sub format_paging_nav {
Jakub Narebski6855f422006-07-30 20:31:00 +02001360 my ($action, $hash, $head, $page, $nrevs) = @_;
Jakub Narebski43ffc062006-07-30 17:49:00 +02001361 my $paging_nav;
1362
1363
1364 if ($hash ne $head || $page) {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001365 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001366 } else {
1367 $paging_nav .= "HEAD";
1368 }
1369
1370 if ($page > 0) {
1371 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001372 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001373 -accesskey => "p", -title => "Alt-p"}, "prev");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001374 } else {
1375 $paging_nav .= " &sdot; prev";
1376 }
1377
1378 if ($nrevs >= (100 * ($page+1)-1)) {
1379 $paging_nav .= " &sdot; " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001380 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
Jakub Narebski26298b52006-08-10 12:38:47 +02001381 -accesskey => "n", -title => "Alt-n"}, "next");
Jakub Narebski43ffc062006-07-30 17:49:00 +02001382 } else {
1383 $paging_nav .= " &sdot; next";
1384 }
1385
1386 return $paging_nav;
1387}
1388
Jakub Narebski717b8312006-07-31 21:22:15 +02001389## ......................................................................
1390## functions printing or outputting HTML: div
Kay Sievers42f7eb92005-08-07 20:21:46 +02001391
Jakub Narebski847e01f2006-08-14 02:05:47 +02001392sub git_print_header_div {
Jakub Narebski717b8312006-07-31 21:22:15 +02001393 my ($action, $title, $hash, $hash_base) = @_;
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001394 my %args = ();
Jakub Narebski717b8312006-07-31 21:22:15 +02001395
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001396 $args{action} = $action;
1397 $args{hash} = $hash if $hash;
1398 $args{hash_base} = $hash_base if $hash_base;
Jakub Narebski717b8312006-07-31 21:22:15 +02001399
1400 print "<div class=\"header\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001401 $cgi->a({-href => href(%args), -class => "title"},
1402 $title ? $title : $action) .
1403 "\n</div>\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02001404}
1405
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001406#sub git_print_authorship (\%) {
1407sub git_print_authorship {
1408 my $co = shift;
1409
Jakub Narebskia44465c2006-08-28 23:17:31 +02001410 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001411 print "<div class=\"author_date\">" .
1412 esc_html($co->{'author_name'}) .
Jakub Narebskia44465c2006-08-28 23:17:31 +02001413 " [$ad{'rfc2822'}";
1414 if ($ad{'hour_local'} < 6) {
1415 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1416 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1417 } else {
1418 printf(" (%02d:%02d %s)",
1419 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1420 }
1421 print "]</div>\n";
Jakub Narebski6fd92a22006-08-28 14:48:12 +02001422}
1423
Jakub Narebski717b8312006-07-31 21:22:15 +02001424sub git_print_page_path {
1425 my $name = shift;
1426 my $type = shift;
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001427 my $hb = shift;
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01001428
Jakub Narebski717b8312006-07-31 21:22:15 +02001429 if (!defined $name) {
Jakub Narebski63e42202006-08-22 12:38:59 +02001430 print "<div class=\"page_path\">/</div>\n";
Jakub Narebski762c7202006-09-04 18:17:58 +02001431 } else {
1432 my @dirname = split '/', $name;
1433 my $basename = pop @dirname;
1434 my $fullname = '';
1435
Jakub Narebski63e42202006-08-22 12:38:59 +02001436 print "<div class=\"page_path\">";
Jakub Narebski762c7202006-09-04 18:17:58 +02001437 foreach my $dir (@dirname) {
1438 $fullname .= $dir . '/';
1439 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1440 hash_base=>$hb),
1441 -title => $fullname}, esc_html($dir));
1442 print "/";
1443 }
1444 if (defined $type && $type eq 'blob') {
Jakub Narebski952c65f2006-08-22 16:52:50 +02001445 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
Jakub Narebski762c7202006-09-04 18:17:58 +02001446 hash_base=>$hb),
1447 -title => $name}, esc_html($basename));
1448 } elsif (defined $type && $type eq 'tree') {
1449 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1450 hash_base=>$hb),
1451 -title => $name}, esc_html($basename));
1452 print "/";
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001453 } else {
Jakub Narebski762c7202006-09-04 18:17:58 +02001454 print esc_html($basename);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07001455 }
Jakub Narebski63e42202006-08-22 12:38:59 +02001456 print "<br/></div>\n";
Kay Sievers8ed23e12005-08-07 20:05:44 +02001457 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02001458}
1459
Jakub Narebskib7f92532006-08-28 14:48:10 +02001460# sub git_print_log (\@;%) {
1461sub git_print_log ($;%) {
Jakub Narebskid16d0932006-08-17 11:21:23 +02001462 my $log = shift;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001463 my %opts = @_;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001464
Jakub Narebskib7f92532006-08-28 14:48:10 +02001465 if ($opts{'-remove_title'}) {
1466 # remove title, i.e. first line of log
1467 shift @$log;
1468 }
Jakub Narebskid16d0932006-08-17 11:21:23 +02001469 # remove leading empty lines
1470 while (defined $log->[0] && $log->[0] eq "") {
1471 shift @$log;
1472 }
1473
1474 # print log
1475 my $signoff = 0;
1476 my $empty = 0;
1477 foreach my $line (@$log) {
Jakub Narebskib7f92532006-08-28 14:48:10 +02001478 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1479 $signoff = 1;
Jakub Narebskifba20b42006-08-28 14:48:13 +02001480 $empty = 0;
Jakub Narebskib7f92532006-08-28 14:48:10 +02001481 if (! $opts{'-remove_signoff'}) {
1482 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1483 next;
1484 } else {
1485 # remove signoff lines
1486 next;
1487 }
1488 } else {
1489 $signoff = 0;
1490 }
1491
Jakub Narebskid16d0932006-08-17 11:21:23 +02001492 # print only one empty line
1493 # do not print empty line after signoff
1494 if ($line eq "") {
1495 next if ($empty || $signoff);
1496 $empty = 1;
1497 } else {
1498 $empty = 0;
1499 }
Jakub Narebskib7f92532006-08-28 14:48:10 +02001500
1501 print format_log_line_html($line) . "<br/>\n";
1502 }
1503
1504 if ($opts{'-final_empty_line'}) {
1505 # end with single empty line
1506 print "<br/>\n" unless $empty;
Jakub Narebskid16d0932006-08-17 11:21:23 +02001507 }
1508}
1509
1510sub git_print_simplified_log {
1511 my $log = shift;
1512 my $remove_title = shift;
1513
Jakub Narebskib7f92532006-08-28 14:48:10 +02001514 git_print_log($log,
1515 -final_empty_line=> 1,
Jakub Narebskib7f92532006-08-28 14:48:10 +02001516 -remove_title => $remove_title);
Jakub Narebskid16d0932006-08-17 11:21:23 +02001517}
1518
Jakub Narebskifa702002006-08-31 00:35:07 +02001519# print tree entry (row of git_tree), but without encompassing <tr> element
1520sub git_print_tree_entry {
1521 my ($t, $basedir, $hash_base, $have_blame) = @_;
1522
1523 my %base_key = ();
1524 $base_key{hash_base} = $hash_base if defined $hash_base;
1525
1526 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1527 if ($t->{'type'} eq "blob") {
1528 print "<td class=\"list\">" .
1529 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1530 file_name=>"$basedir$t->{'name'}", %base_key),
1531 -class => "list"}, esc_html($t->{'name'})) .
1532 "</td>\n" .
1533 "<td class=\"link\">" .
1534 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1535 file_name=>"$basedir$t->{'name'}", %base_key)},
1536 "blob");
1537 if ($have_blame) {
1538 print " | " .
1539 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1540 file_name=>"$basedir$t->{'name'}", %base_key)},
1541 "blame");
1542 }
1543 if (defined $hash_base) {
1544 print " | " .
1545 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1546 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1547 "history");
1548 }
1549 print " | " .
1550 $cgi->a({-href => href(action=>"blob_plain",
1551 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1552 "raw") .
1553 "</td>\n";
1554
1555 } elsif ($t->{'type'} eq "tree") {
1556 print "<td class=\"list\">" .
1557 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1558 file_name=>"$basedir$t->{'name'}", %base_key)},
1559 esc_html($t->{'name'})) .
1560 "</td>\n" .
1561 "<td class=\"link\">" .
1562 $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1563 file_name=>"$basedir$t->{'name'}", %base_key)},
1564 "tree");
1565 if (defined $hash_base) {
1566 print " | " .
1567 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1568 file_name=>"$basedir$t->{'name'}")},
1569 "history");
1570 }
1571 print "</td>\n";
1572 }
1573}
1574
Jakub Narebski717b8312006-07-31 21:22:15 +02001575## ......................................................................
1576## functions printing large fragments of HTML
Kay Sieversede5e102005-08-07 20:23:12 +02001577
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001578sub git_difftree_body {
Jakub Narebskieee08902006-08-24 00:15:14 +02001579 my ($difftree, $hash, $parent) = @_;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001580
1581 print "<div class=\"list_head\">\n";
1582 if ($#{$difftree} > 10) {
1583 print(($#{$difftree} + 1) . " files changed:\n");
1584 }
1585 print "</div>\n";
1586
1587 print "<table class=\"diff_tree\">\n";
1588 my $alternate = 0;
Jakub Narebskib4657e72006-08-28 14:48:14 +02001589 my $patchno = 0;
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001590 foreach my $line (@{$difftree}) {
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001591 my %diff = parse_difftree_raw_line($line);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001592
1593 if ($alternate) {
1594 print "<tr class=\"dark\">\n";
1595 } else {
1596 print "<tr class=\"light\">\n";
1597 }
1598 $alternate ^= 1;
1599
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001600 my ($to_mode_oct, $to_mode_str, $to_file_type);
1601 my ($from_mode_oct, $from_mode_str, $from_file_type);
1602 if ($diff{'to_mode'} ne ('0' x 6)) {
1603 $to_mode_oct = oct $diff{'to_mode'};
1604 if (S_ISREG($to_mode_oct)) { # only for regular file
1605 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001606 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001607 $to_file_type = file_type($diff{'to_mode'});
1608 }
1609 if ($diff{'from_mode'} ne ('0' x 6)) {
1610 $from_mode_oct = oct $diff{'from_mode'};
1611 if (S_ISREG($to_mode_oct)) { # only for regular file
1612 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1613 }
1614 $from_file_type = file_type($diff{'from_mode'});
1615 }
1616
1617 if ($diff{'status'} eq "A") { # created
1618 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1619 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1620 $mode_chng .= "]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001621 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001622 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1623 hash_base=>$hash, file_name=>$diff{'file'}),
1624 -class => "list"}, esc_html($diff{'file'})) .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001625 "</td>\n" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001626 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001627 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001628 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1629 hash_base=>$hash, file_name=>$diff{'file'})},
Jakub Narebskib4657e72006-08-28 14:48:14 +02001630 "blob");
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001631 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001632 # link to patch
1633 $patchno++;
1634 print " | " .
1635 $cgi->a({-href => "#patch$patchno"}, "patch");
1636 }
1637 print "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001638
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001639 } elsif ($diff{'status'} eq "D") { # deleted
1640 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001641 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001642 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1643 hash_base=>$parent, file_name=>$diff{'file'}),
1644 -class => "list"}, esc_html($diff{'file'})) .
1645 "</td>\n" .
1646 "<td>$mode_chng</td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001647 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001648 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1649 hash_base=>$parent, file_name=>$diff{'file'})},
1650 "blob") .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001651 " | ";
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001652 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001653 # link to patch
1654 $patchno++;
1655 print " | " .
1656 $cgi->a({-href => "#patch$patchno"}, "patch");
1657 }
1658 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
Jakub Narebski09052552006-08-26 23:33:58 +02001659 file_name=>$diff{'file'})},
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001660 "history") .
1661 "</td>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001662
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001663 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001664 my $mode_chnge = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001665 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1666 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1667 if ($from_file_type != $to_file_type) {
1668 $mode_chnge .= " from $from_file_type to $to_file_type";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001669 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001670 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1671 if ($from_mode_str && $to_mode_str) {
1672 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1673 } elsif ($to_mode_str) {
1674 $mode_chnge .= " mode: $to_mode_str";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001675 }
1676 }
1677 $mode_chnge .= "]</span>\n";
1678 }
1679 print "<td>";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001680 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski420e92f2006-08-24 23:53:54 +02001681 print $cgi->a({-href => href(action=>"blobdiff",
1682 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1683 hash_base=>$hash, hash_parent_base=>$parent,
1684 file_name=>$diff{'file'}),
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001685 -class => "list"}, esc_html($diff{'file'}));
1686 } else { # only mode changed
1687 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1688 hash_base=>$hash, file_name=>$diff{'file'}),
1689 -class => "list"}, esc_html($diff{'file'}));
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001690 }
1691 print "</td>\n" .
1692 "<td>$mode_chnge</td>\n" .
1693 "<td class=\"link\">" .
Jakub Narebskib4657e72006-08-28 14:48:14 +02001694 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1695 hash_base=>$hash, file_name=>$diff{'file'})},
1696 "blob");
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001697 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001698 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001699 # link to patch
1700 $patchno++;
1701 print " | " .
1702 $cgi->a({-href => "#patch$patchno"}, "patch");
1703 } else {
1704 print " | " .
1705 $cgi->a({-href => href(action=>"blobdiff",
1706 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1707 hash_base=>$hash, hash_parent_base=>$parent,
1708 file_name=>$diff{'file'})},
1709 "diff");
1710 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001711 }
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001712 print " | " .
1713 $cgi->a({-href => href(action=>"history",
1714 hash_base=>$hash, file_name=>$diff{'file'})},
1715 "history");
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001716 print "</td>\n";
1717
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001718 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1719 my %status_name = ('R' => 'moved', 'C' => 'copied');
1720 my $nstatus = $status_name{$diff{'status'}};
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001721 my $mode_chng = "";
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001722 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1723 # mode also for directories, so we cannot use $to_mode_str
1724 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001725 }
1726 print "<td>" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001727 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1728 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1729 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1730 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1731 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1732 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1733 -class => "list"}, esc_html($diff{'from_file'})) .
1734 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001735 "<td class=\"link\">" .
Jakub Narebskie8e41a92006-08-21 23:08:52 +02001736 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1737 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1738 "blob");
1739 if ($diff{'to_id'} ne $diff{'from_id'}) {
Jakub Narebski72dbafa2006-09-04 18:19:58 +02001740 if ($action eq 'commitdiff') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02001741 # link to patch
1742 $patchno++;
1743 print " | " .
1744 $cgi->a({-href => "#patch$patchno"}, "patch");
1745 } else {
1746 print " | " .
1747 $cgi->a({-href => href(action=>"blobdiff",
1748 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1749 hash_base=>$hash, hash_parent_base=>$parent,
1750 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1751 "diff");
1752 }
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001753 }
1754 print "</td>\n";
1755
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02001756 } # we should not encounter Unmerged (U) or Unknown (X) status
1757 print "</tr>\n";
1758 }
1759 print "</table>\n";
1760}
1761
Jakub Narebskieee08902006-08-24 00:15:14 +02001762sub git_patchset_body {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001763 my ($fd, $difftree, $hash, $hash_parent) = @_;
Jakub Narebskieee08902006-08-24 00:15:14 +02001764
1765 my $patch_idx = 0;
1766 my $in_header = 0;
1767 my $patch_found = 0;
Jakub Narebskife875852006-08-25 20:59:39 +02001768 my $diffinfo;
Jakub Narebskieee08902006-08-24 00:15:14 +02001769
1770 print "<div class=\"patchset\">\n";
1771
Jakub Narebski157e43b2006-08-24 19:34:36 +02001772 LINE:
Jakub Narebskic8a99d72006-08-26 19:14:23 +02001773 while (my $patch_line = <$fd>) {
Jakub Narebski157e43b2006-08-24 19:34:36 +02001774 chomp $patch_line;
Jakub Narebskieee08902006-08-24 00:15:14 +02001775
1776 if ($patch_line =~ m/^diff /) { # "git diff" header
1777 # beginning of patch (in patchset)
1778 if ($patch_found) {
1779 # close previous patch
1780 print "</div>\n"; # class="patch"
1781 } else {
1782 # first patch in patchset
1783 $patch_found = 1;
1784 }
Jakub Narebskib4657e72006-08-28 14:48:14 +02001785 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02001786
Jakub Narebskife875852006-08-25 20:59:39 +02001787 if (ref($difftree->[$patch_idx]) eq "HASH") {
1788 $diffinfo = $difftree->[$patch_idx];
1789 } else {
1790 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1791 }
1792 $patch_idx++;
Jakub Narebskieee08902006-08-24 00:15:14 +02001793
1794 # for now, no extended header, hence we skip empty patches
1795 # companion to next LINE if $in_header;
Jakub Narebskife875852006-08-25 20:59:39 +02001796 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
Jakub Narebskieee08902006-08-24 00:15:14 +02001797 $in_header = 1;
1798 next LINE;
1799 }
1800
Jakub Narebskife875852006-08-25 20:59:39 +02001801 if ($diffinfo->{'status'} eq "A") { # added
1802 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001803 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001804 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1805 $diffinfo->{'to_id'}) . "(new)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001806 "</div>\n"; # class="diff_info"
1807
Jakub Narebskife875852006-08-25 20:59:39 +02001808 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1809 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001810 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001811 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1812 $diffinfo->{'from_id'}) . "(deleted)" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001813 "</div>\n"; # class="diff_info"
1814
Jakub Narebskife875852006-08-25 20:59:39 +02001815 } elsif ($diffinfo->{'status'} eq "R" || # renamed
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02001816 $diffinfo->{'status'} eq "C" || # copied
1817 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
Jakub Narebskieee08902006-08-24 00:15:14 +02001818 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001819 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001820 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001821 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1822 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001823 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001824 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001825 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001826 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1827 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001828 print "</div>\n"; # class="diff_info"
1829
1830 } else { # modified, mode changed, ...
1831 print "<div class=\"diff_info\">" .
Jakub Narebskife875852006-08-25 20:59:39 +02001832 file_type($diffinfo->{'from_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001833 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
Jakub Narebskife875852006-08-25 20:59:39 +02001834 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1835 $diffinfo->{'from_id'}) .
Jakub Narebskieee08902006-08-24 00:15:14 +02001836 " -> " .
Jakub Narebskife875852006-08-25 20:59:39 +02001837 file_type($diffinfo->{'to_mode'}) . ":" .
Jakub Narebskieee08902006-08-24 00:15:14 +02001838 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
Jakub Narebskife875852006-08-25 20:59:39 +02001839 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1840 $diffinfo->{'to_id'});
Jakub Narebskieee08902006-08-24 00:15:14 +02001841 print "</div>\n"; # class="diff_info"
1842 }
1843
1844 #print "<div class=\"diff extended_header\">\n";
1845 $in_header = 1;
1846 next LINE;
1847 } # start of patch in patchset
1848
1849
1850 if ($in_header && $patch_line =~ m/^---/) {
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001851 #print "</div>\n"; # class="diff extended_header"
Jakub Narebskieee08902006-08-24 00:15:14 +02001852 $in_header = 0;
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001853
1854 my $file = $diffinfo->{'from_file'};
1855 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001856 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1857 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1858 -class => "list"}, esc_html($file));
1859 $patch_line =~ s|a/.*$|a/$file|g;
1860 print "<div class=\"diff from_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001861
1862 $patch_line = <$fd>;
1863 chomp $patch_line;
1864
1865 #$patch_line =~ m/^+++/;
1866 $file = $diffinfo->{'to_file'};
1867 $file ||= $diffinfo->{'file'};
Jakub Narebskief10ee82006-08-25 21:05:07 +02001868 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1869 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1870 -class => "list"}, esc_html($file));
1871 $patch_line =~ s|b/.*|b/$file|g;
1872 print "<div class=\"diff to_file\">$patch_line</div>\n";
Jakub Narebskie4e4f822006-08-25 21:04:13 +02001873
1874 next LINE;
Jakub Narebskieee08902006-08-24 00:15:14 +02001875 }
1876 next LINE if $in_header;
1877
1878 print format_diff_line($patch_line);
1879 }
1880 print "</div>\n" if $patch_found; # class="patch"
1881
1882 print "</div>\n"; # class="patchset"
1883}
1884
1885# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1886
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001887sub git_shortlog_body {
1888 # uses global variable $project
1889 my ($revlist, $from, $to, $refs, $extra) = @_;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05301890
1891 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1892 my $have_snapshot = (defined $ctype && defined $suffix);
1893
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001894 $from = 0 unless defined $from;
1895 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1896
1897 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1898 my $alternate = 0;
1899 for (my $i = $from; $i <= $to; $i++) {
1900 my $commit = $revlist->[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02001901 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1902 my $ref = format_ref_marker($refs, $commit);
1903 my %co = parse_commit($commit);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001904 if ($alternate) {
1905 print "<tr class=\"dark\">\n";
1906 } else {
1907 print "<tr class=\"light\">\n";
1908 }
1909 $alternate ^= 1;
1910 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1911 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1912 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1913 "<td>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02001914 print format_subject_html($co{'title'}, $co{'title_short'},
1915 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001916 print "</td>\n" .
1917 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001918 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05301919 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1920 if ($have_snapshot) {
1921 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1922 }
1923 print "</td>\n" .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02001924 "</tr>\n";
1925 }
1926 if (defined $extra) {
1927 print "<tr>\n" .
1928 "<td colspan=\"4\">$extra</td>\n" .
1929 "</tr>\n";
1930 }
1931 print "</table>\n";
1932}
1933
Jakub Narebski581860e2006-08-14 02:09:08 +02001934sub git_history_body {
1935 # Warning: assumes constant type (blob or tree) during history
1936 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1937
1938 print "<table class=\"history\" cellspacing=\"0\">\n";
1939 my $alternate = 0;
1940 while (my $line = <$fd>) {
1941 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1942 next;
1943 }
1944
1945 my $commit = $1;
1946 my %co = parse_commit($commit);
1947 if (!%co) {
1948 next;
1949 }
1950
1951 my $ref = format_ref_marker($refs, $commit);
1952
1953 if ($alternate) {
1954 print "<tr class=\"dark\">\n";
1955 } else {
1956 print "<tr class=\"light\">\n";
1957 }
1958 $alternate ^= 1;
1959 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1960 # shortlog uses chop_str($co{'author_name'}, 10)
1961 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1962 "<td>";
1963 # originally git_history used chop_str($co{'title'}, 50)
Jakub Narebski952c65f2006-08-22 16:52:50 +02001964 print format_subject_html($co{'title'}, $co{'title_short'},
1965 href(action=>"commit", hash=>$commit), $ref);
Jakub Narebski581860e2006-08-14 02:09:08 +02001966 print "</td>\n" .
1967 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02001968 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1969 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1970 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
Jakub Narebski581860e2006-08-14 02:09:08 +02001971
1972 if ($ftype eq 'blob') {
1973 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1974 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1975 if (defined $blob_current && defined $blob_parent &&
1976 $blob_current ne $blob_parent) {
1977 print " | " .
Jakub Narebski420e92f2006-08-24 23:53:54 +02001978 $cgi->a({-href => href(action=>"blobdiff",
1979 hash=>$blob_current, hash_parent=>$blob_parent,
1980 hash_base=>$hash_base, hash_parent_base=>$commit,
1981 file_name=>$file_name)},
Jakub Narebski581860e2006-08-14 02:09:08 +02001982 "diff to current");
1983 }
1984 }
1985 print "</td>\n" .
1986 "</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 Narebski9f5dcb82006-07-31 11:22:13 +02001996sub git_tags_body {
1997 # uses global variable $project
1998 my ($taglist, $from, $to, $extra) = @_;
1999 $from = 0 unless defined $from;
2000 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2001
2002 print "<table class=\"tags\" cellspacing=\"0\">\n";
2003 my $alternate = 0;
2004 for (my $i = $from; $i <= $to; $i++) {
2005 my $entry = $taglist->[$i];
2006 my %tag = %$entry;
2007 my $comment_lines = $tag{'comment'};
2008 my $comment = shift @$comment_lines;
2009 my $comment_short;
2010 if (defined $comment) {
2011 $comment_short = chop_str($comment, 30, 5);
2012 }
2013 if ($alternate) {
2014 print "<tr class=\"dark\">\n";
2015 } else {
2016 print "<tr class=\"light\">\n";
2017 }
2018 $alternate ^= 1;
2019 print "<td><i>$tag{'age'}</i></td>\n" .
2020 "<td>" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002021 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002022 -class => "list name"}, esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002023 "</td>\n" .
2024 "<td>";
2025 if (defined $comment) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002026 print format_subject_html($comment, $comment_short,
2027 href(action=>"tag", hash=>$tag{'id'}));
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002028 }
2029 print "</td>\n" .
2030 "<td class=\"selflink\">";
2031 if ($tag{'type'} eq "tag") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002032 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002033 } else {
2034 print "&nbsp;";
2035 }
2036 print "</td>\n" .
2037 "<td class=\"link\">" . " | " .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002038 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002039 if ($tag{'reftype'} eq "commit") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002040 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2041 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002042 } elsif ($tag{'reftype'} eq "blob") {
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002043 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002044 }
2045 print "</td>\n" .
2046 "</tr>";
2047 }
2048 if (defined $extra) {
2049 print "<tr>\n" .
2050 "<td colspan=\"5\">$extra</td>\n" .
2051 "</tr>\n";
2052 }
2053 print "</table>\n";
2054}
2055
2056sub git_heads_body {
2057 # uses global variable $project
2058 my ($taglist, $head, $from, $to, $extra) = @_;
2059 $from = 0 unless defined $from;
2060 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2061
2062 print "<table class=\"heads\" cellspacing=\"0\">\n";
2063 my $alternate = 0;
2064 for (my $i = $from; $i <= $to; $i++) {
2065 my $entry = $taglist->[$i];
2066 my %tag = %$entry;
2067 my $curr = $tag{'id'} eq $head;
2068 if ($alternate) {
2069 print "<tr class=\"dark\">\n";
2070 } else {
2071 print "<tr class=\"light\">\n";
2072 }
2073 $alternate ^= 1;
2074 print "<td><i>$tag{'age'}</i></td>\n" .
2075 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002076 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
Jakub Narebski63e42202006-08-22 12:38:59 +02002077 -class => "list name"},esc_html($tag{'name'})) .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002078 "</td>\n" .
2079 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002080 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2081 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002082 "</td>\n" .
2083 "</tr>";
2084 }
2085 if (defined $extra) {
2086 print "<tr>\n" .
2087 "<td colspan=\"3\">$extra</td>\n" .
2088 "</tr>\n";
2089 }
2090 print "</table>\n";
2091}
2092
Jakub Narebski717b8312006-07-31 21:22:15 +02002093## ======================================================================
2094## ======================================================================
2095## actions
2096
Jakub Narebski717b8312006-07-31 21:22:15 +02002097sub git_project_list {
Jakub Narebski6326b602006-08-01 02:59:12 +02002098 my $order = $cgi->param('o');
2099 if (defined $order && $order !~ m/project|descr|owner|age/) {
Jakub Narebskie2860ea2006-08-05 13:15:24 +02002100 die_error(undef, "Unknown order parameter");
Jakub Narebski6326b602006-08-01 02:59:12 +02002101 }
2102
Jakub Narebski847e01f2006-08-14 02:05:47 +02002103 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02002104 my @projects;
2105 if (!@list) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002106 die_error(undef, "No projects found");
Jakub Narebski717b8312006-07-31 21:22:15 +02002107 }
2108 foreach my $pr (@list) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002109 my $head = git_get_head_hash($pr->{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02002110 if (!defined $head) {
2111 next;
2112 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002113 $git_dir = "$projectroot/$pr->{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02002114 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02002115 if (!%co) {
2116 next;
2117 }
2118 $pr->{'commit'} = \%co;
2119 if (!defined $pr->{'descr'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002120 my $descr = git_get_project_description($pr->{'path'}) || "";
Jakub Narebski717b8312006-07-31 21:22:15 +02002121 $pr->{'descr'} = chop_str($descr, 25, 5);
2122 }
2123 if (!defined $pr->{'owner'}) {
2124 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2125 }
2126 push @projects, $pr;
2127 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002128
Jakub Narebski717b8312006-07-31 21:22:15 +02002129 git_header_html();
2130 if (-f $home_text) {
2131 print "<div class=\"index_include\">\n";
2132 open (my $fd, $home_text);
2133 print <$fd>;
2134 close $fd;
2135 print "</div>\n";
2136 }
2137 print "<table class=\"project_list\">\n" .
2138 "<tr>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002139 $order ||= "project";
2140 if ($order eq "project") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002141 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2142 print "<th>Project</th>\n";
2143 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002144 print "<th>" .
2145 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2146 -class => "header"}, "Project") .
2147 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002148 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002149 if ($order eq "descr") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002150 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2151 print "<th>Description</th>\n";
2152 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002153 print "<th>" .
2154 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2155 -class => "header"}, "Description") .
2156 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002157 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002158 if ($order eq "owner") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002159 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2160 print "<th>Owner</th>\n";
2161 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002162 print "<th>" .
2163 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2164 -class => "header"}, "Owner") .
2165 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002166 }
Jakub Narebski6326b602006-08-01 02:59:12 +02002167 if ($order eq "age") {
Jakub Narebski717b8312006-07-31 21:22:15 +02002168 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2169 print "<th>Last Change</th>\n";
2170 } else {
Jakub Narebski6326b602006-08-01 02:59:12 +02002171 print "<th>" .
2172 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2173 -class => "header"}, "Last Change") .
2174 "</th>\n";
Jakub Narebski717b8312006-07-31 21:22:15 +02002175 }
2176 print "<th></th>\n" .
2177 "</tr>\n";
2178 my $alternate = 0;
2179 foreach my $pr (@projects) {
2180 if ($alternate) {
2181 print "<tr class=\"dark\">\n";
2182 } else {
2183 print "<tr class=\"light\">\n";
2184 }
2185 $alternate ^= 1;
Martin Waitz756d2f02006-08-17 00:28:36 +02002186 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
Jakub Narebski6326b602006-08-01 02:59:12 +02002187 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002188 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2189 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
Jakub Narebski6326b602006-08-01 02:59:12 +02002190 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2191 $pr->{'commit'}{'age_string'} . "</td>\n" .
Jakub Narebski717b8312006-07-31 21:22:15 +02002192 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02002193 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2194 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2195 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
Jakub Narebski717b8312006-07-31 21:22:15 +02002196 "</td>\n" .
2197 "</tr>\n";
2198 }
2199 print "</table>\n";
2200 git_footer_html();
2201}
2202
Kay Sieversede5e102005-08-07 20:23:12 +02002203sub git_summary {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002204 my $descr = git_get_project_description($project) || "none";
2205 my $head = git_get_head_hash($project);
2206 my %co = parse_commit($head);
2207 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sieversede5e102005-08-07 20:23:12 +02002208
Jakub Narebski1e0cf032006-08-14 02:10:06 +02002209 my $owner = git_get_project_owner($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002210
Jakub Narebski847e01f2006-08-14 02:05:47 +02002211 my $refs = git_get_references();
Kay Sieversede5e102005-08-07 20:23:12 +02002212 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002213 git_print_page_nav('summary','', $head);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002214
Kay Sievers19806692005-08-07 20:26:27 +02002215 print "<div class=\"title\">&nbsp;</div>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002216 print "<table cellspacing=\"0\">\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002217 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
Kay Sieversede5e102005-08-07 20:23:12 +02002218 "<tr><td>owner</td><td>$owner</td></tr>\n" .
Jakub Narebski19a87212006-08-15 23:03:17 +02002219 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002220 # use per project git URL list in $projectroot/$project/cloneurl
2221 # or make project git URL from git base URL and project name
Jakub Narebski19a87212006-08-15 23:03:17 +02002222 my $url_tag = "URL";
Jakub Narebskie79ca7c2006-08-16 14:50:34 +02002223 my @url_list = git_get_project_url_list($project);
2224 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2225 foreach my $git_url (@url_list) {
2226 next unless $git_url;
2227 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
Jakub Narebski19a87212006-08-15 23:03:17 +02002228 $url_tag = "";
2229 }
2230 print "</table>\n";
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002231
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002232 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2233 git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002234 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002235 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversede5e102005-08-07 20:23:12 +02002236 close $fd;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002237 git_print_header_div('shortlog');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002238 git_shortlog_body(\@revlist, 0, 15, $refs,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002239 $cgi->a({-href => href(action=>"shortlog")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002240
Jakub Narebski847e01f2006-08-14 02:05:47 +02002241 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002242 if (defined @$taglist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002243 git_print_header_div('tags');
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002244 git_tags_body($taglist, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002245 $cgi->a({-href => href(action=>"tags")}, "..."));
Kay Sieversede5e102005-08-07 20:23:12 +02002246 }
Kay Sievers0db37972005-08-07 20:24:35 +02002247
Jakub Narebski847e01f2006-08-14 02:05:47 +02002248 my $headlist = git_get_refs_list("refs/heads");
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002249 if (defined @$headlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002250 git_print_header_div('heads');
Junio C Hamanob77aeb22006-07-31 19:12:18 -07002251 git_heads_body($headlist, $head, 0, 15,
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002252 $cgi->a({-href => href(action=>"heads")}, "..."));
Kay Sievers0db37972005-08-07 20:24:35 +02002253 }
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002254
Kay Sieversede5e102005-08-07 20:23:12 +02002255 git_footer_html();
2256}
2257
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002258sub git_tag {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002259 my $head = git_get_head_hash($project);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002260 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002261 git_print_page_nav('','', $head,undef,$head);
2262 my %tag = parse_tag($hash);
2263 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002264 print "<div class=\"title_text\">\n" .
2265 "<table cellspacing=\"0\">\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002266 "<tr>\n" .
2267 "<td>object</td>\n" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002268 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2269 $tag{'object'}) . "</td>\n" .
2270 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2271 $tag{'type'}) . "</td>\n" .
Kay Sieverse4669df2005-08-08 00:02:39 +02002272 "</tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002273 if (defined($tag{'author'})) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002274 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
Kay Sievers40c13812005-11-19 17:41:29 +01002275 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002276 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2277 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2278 "</td></tr>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002279 }
2280 print "</table>\n\n" .
2281 "</div>\n";
2282 print "<div class=\"page_body\">";
2283 my $comment = $tag{'comment'};
2284 foreach my $line (@$comment) {
Kay Sievers40c13812005-11-19 17:41:29 +01002285 print esc_html($line) . "<br/>\n";
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002286 }
2287 print "</div>\n";
2288 git_footer_html();
2289}
2290
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002291sub git_blame2 {
2292 my $fd;
2293 my $ftype;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302294
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302295 my ($have_blame) = gitweb_check_feature('blame');
2296 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302297 die_error('403 Permission denied', "Permission denied");
2298 }
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002299 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002300 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002301 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002302 my %co = parse_commit($hash_base)
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002303 or die_error(undef, "Reading commit failed");
2304 if (!defined $hash) {
2305 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2306 or die_error(undef, "Error looking up file");
2307 }
2308 $ftype = git_get_type($hash);
2309 if ($ftype !~ "blob") {
Jakub Narebskie484a2d2006-08-05 13:12:51 +02002310 die_error("400 Bad Request", "Object is not a blob");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002311 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002312 open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002313 or die_error(undef, "Open git-blame failed");
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002314 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002315 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002316 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2317 "blob") .
2318 " | " .
2319 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2320 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002321 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2322 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002323 git_print_page_path($file_name, $ftype, $hash_base);
Luben Tuikov82f930d2006-08-04 15:09:59 -07002324 my @rev_color = (qw(light2 dark2));
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002325 my $num_colors = scalar(@rev_color);
2326 my $current_color = 0;
2327 my $last_rev;
Jakub Narebski59b9f612006-08-22 23:42:53 +02002328 print <<HTML;
2329<div class="page_body">
2330<table class="blame">
2331<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2332HTML
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002333 while (<$fd>) {
2334 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2335 my $full_rev = $1;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002336 my $rev = substr($full_rev, 0, 8);
Luben Tuikovacb0f6f2006-07-23 14:17:48 -07002337 my $lineno = $2;
2338 my $data = $3;
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002339
Luben Tuikovcc1bf972006-07-23 13:37:53 -07002340 if (!defined $last_rev) {
2341 $last_rev = $full_rev;
2342 } elsif ($last_rev ne $full_rev) {
2343 $last_rev = $full_rev;
2344 $current_color = ++$current_color % $num_colors;
2345 }
2346 print "<tr class=\"$rev_color[$current_color]\">\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002347 print "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002348 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2349 esc_html($rev)) . "</td>\n";
2350 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2351 esc_html($lineno) . "</a></td>\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002352 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2353 print "</tr>\n";
2354 }
2355 print "</table>\n";
2356 print "</div>";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002357 close $fd
2358 or print "Reading blob failed\n";
Luben Tuikov1f2857e2006-07-23 13:34:55 -07002359 git_footer_html();
2360}
2361
Florian Forstere34ef622006-06-11 17:45:19 +02002362sub git_blame {
2363 my $fd;
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302364
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302365 my ($have_blame) = gitweb_check_feature('blame');
2366 if (!$have_blame) {
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302367 die_error('403 Permission denied', "Permission denied");
2368 }
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002369 die_error('404 Not Found', "File name not defined") if (!$file_name);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002370 $hash_base ||= git_get_head_hash($project);
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002371 die_error(undef, "Couldn't find base commit") unless ($hash_base);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002372 my %co = parse_commit($hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002373 or die_error(undef, "Reading commit failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002374 if (!defined $hash) {
2375 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002376 or die_error(undef, "Error lookup file");
Florian Forstere34ef622006-06-11 17:45:19 +02002377 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002378 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002379 or die_error(undef, "Open git-annotate failed");
Florian Forstere34ef622006-06-11 17:45:19 +02002380 git_header_html();
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002381 my $formats_nav =
Jakub Narebski952c65f2006-08-22 16:52:50 +02002382 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2383 "blob") .
2384 " | " .
2385 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2386 "head");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002387 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2388 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002389 git_print_page_path($file_name, 'blob', $hash_base);
Florian Forstere34ef622006-06-11 17:45:19 +02002390 print "<div class=\"page_body\">\n";
2391 print <<HTML;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002392<table class="blame">
Florian Forstere34ef622006-06-11 17:45:19 +02002393 <tr>
2394 <th>Commit</th>
2395 <th>Age</th>
2396 <th>Author</th>
2397 <th>Line</th>
2398 <th>Data</th>
2399 </tr>
2400HTML
2401 my @line_class = (qw(light dark));
2402 my $line_class_len = scalar (@line_class);
2403 my $line_class_num = $#line_class;
2404 while (my $line = <$fd>) {
2405 my $long_rev;
2406 my $short_rev;
2407 my $author;
2408 my $time;
2409 my $lineno;
2410 my $data;
2411 my $age;
2412 my $age_str;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002413 my $age_class;
Florian Forstere34ef622006-06-11 17:45:19 +02002414
2415 chomp $line;
2416 $line_class_num = ($line_class_num + 1) % $line_class_len;
2417
Jakub Narebski030b5202006-08-26 02:13:05 +02002418 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 +02002419 $long_rev = $1;
2420 $author = $2;
2421 $time = $3;
2422 $lineno = $4;
2423 $data = $5;
2424 } else {
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002425 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
Florian Forstere34ef622006-06-11 17:45:19 +02002426 next;
2427 }
2428 $short_rev = substr ($long_rev, 0, 8);
2429 $age = time () - $time;
2430 $age_str = age_string ($age);
2431 $age_str =~ s/ /&nbsp;/g;
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002432 $age_class = age_class($age);
Florian Forstere34ef622006-06-11 17:45:19 +02002433 $author = esc_html ($author);
2434 $author =~ s/ /&nbsp;/g;
Jakub Narebskif16db172006-08-06 02:08:31 +02002435
2436 $data = untabify($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002437 $data = esc_html ($data);
Florian Forstere34ef622006-06-11 17:45:19 +02002438
2439 print <<HTML;
2440 <tr class="$line_class[$line_class_num]">
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002441 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002442 <td class="$age_class">$age_str</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002443 <td>$author</td>
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002444 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2445 <td class="pre">$data</td>
Florian Forstere34ef622006-06-11 17:45:19 +02002446 </tr>
2447HTML
2448 } # while (my $line = <$fd>)
2449 print "</table>\n\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002450 close $fd
2451 or print "Reading blob failed.\n";
Florian Forstere34ef622006-06-11 17:45:19 +02002452 print "</div>";
2453 git_footer_html();
2454}
2455
Kay Sieversede5e102005-08-07 20:23:12 +02002456sub git_tags {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002457 my $head = git_get_head_hash($project);
Kay Sieversede5e102005-08-07 20:23:12 +02002458 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002459 git_print_page_nav('','', $head,undef,$head);
2460 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002461
Jakub Narebski847e01f2006-08-14 02:05:47 +02002462 my $taglist = git_get_refs_list("refs/tags");
Kay Sieversede5e102005-08-07 20:23:12 +02002463 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002464 git_tags_body($taglist);
Kay Sieversede5e102005-08-07 20:23:12 +02002465 }
Kay Sieversede5e102005-08-07 20:23:12 +02002466 git_footer_html();
2467}
2468
Kay Sieversd8f1c5c2005-10-04 01:12:47 +02002469sub git_heads {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002470 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002471 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002472 git_print_page_nav('','', $head,undef,$head);
2473 git_print_header_div('summary', $project);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002474
Jakub Narebski847e01f2006-08-14 02:05:47 +02002475 my $taglist = git_get_refs_list("refs/heads");
Kay Sievers0db37972005-08-07 20:24:35 +02002476 if (defined @$taglist) {
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02002477 git_heads_body($taglist, $head);
Kay Sievers0db37972005-08-07 20:24:35 +02002478 }
Kay Sievers0db37972005-08-07 20:24:35 +02002479 git_footer_html();
2480}
2481
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002482sub git_blob_plain {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002483 # blobs defined by non-textual hash id's can be cached
2484 my $expires;
2485 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2486 $expires = "+1d";
2487 }
2488
Luben Tuikovcff07712006-07-23 13:28:55 -07002489 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002490 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002491 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002492 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002493 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002494 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002495 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002496 }
2497 }
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002498 my $type = shift;
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002499 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicfd82662006-08-05 12:56:04 +02002500 or die_error(undef, "Couldn't cat $file_name, $hash");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002501
Jakub Narebski847e01f2006-08-14 02:05:47 +02002502 $type ||= blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002503
2504 # save as filename, even when no $file_name is given
2505 my $save_as = "$hash";
2506 if (defined $file_name) {
2507 $save_as = $file_name;
2508 } elsif ($type =~ m/^text\//) {
2509 $save_as .= '.txt';
2510 }
2511
Jakub Narebskif2e73302006-08-26 19:14:25 +02002512 print $cgi->header(
2513 -type => "$type",
2514 -expires=>$expires,
2515 -content_disposition => "inline; filename=\"$save_as\"");
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002516 undef $/;
2517 binmode STDOUT, ':raw';
2518 print <$fd>;
2519 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2520 $/ = "\n";
2521 close $fd;
2522}
2523
Kay Sievers09bd7892005-08-07 20:21:23 +02002524sub git_blob {
Jakub Narebskif2e73302006-08-26 19:14:25 +02002525 # blobs defined by non-textual hash id's can be cached
2526 my $expires;
2527 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2528 $expires = "+1d";
2529 }
2530
Luben Tuikovcff07712006-07-23 13:28:55 -07002531 if (!defined $hash) {
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002532 if (defined $file_name) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002533 my $base = $hash_base || git_get_head_hash($project);
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002534 $hash = git_get_hash_by_path($base, $file_name, "blob")
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002535 or die_error(undef, "Error lookup file");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002536 } else {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002537 die_error(undef, "No file name defined");
Jakub Narebski5be01bc2006-07-29 22:43:40 +02002538 }
2539 }
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302540 my ($have_blame) = gitweb_check_feature('blame');
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002541 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002542 or die_error(undef, "Couldn't cat $file_name, $hash");
Jakub Narebski847e01f2006-08-14 02:05:47 +02002543 my $mimetype = blob_mimetype($fd, $file_name);
Luben Tuikov930cf7d2006-07-09 20:18:57 -07002544 if ($mimetype !~ m/^text\//) {
2545 close $fd;
2546 return git_blob_plain($mimetype);
2547 }
Jakub Narebskif2e73302006-08-26 19:14:25 +02002548 git_header_html(undef, $expires);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002549 my $formats_nav = '';
Jakub Narebski847e01f2006-08-14 02:05:47 +02002550 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Kay Sievers93129442005-10-17 03:27:54 +02002551 if (defined $file_name) {
Florian Forster5996ca02006-06-12 10:31:57 +02002552 if ($have_blame) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002553 $formats_nav .=
2554 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2555 hash=>$hash, file_name=>$file_name)},
2556 "blame") .
2557 " | ";
Florian Forster5996ca02006-06-12 10:31:57 +02002558 }
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002559 $formats_nav .=
Jakub Narebski952c65f2006-08-22 16:52:50 +02002560 $cgi->a({-href => href(action=>"blob_plain",
2561 hash=>$hash, file_name=>$file_name)},
2562 "plain") .
2563 " | " .
2564 $cgi->a({-href => href(action=>"blob",
2565 hash_base=>"HEAD", file_name=>$file_name)},
2566 "head");
Kay Sievers93129442005-10-17 03:27:54 +02002567 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002568 $formats_nav .=
2569 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
Kay Sievers93129442005-10-17 03:27:54 +02002570 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002571 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2572 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02002573 } else {
2574 print "<div class=\"page_nav\">\n" .
2575 "<br/><br/></div>\n" .
2576 "<div class=\"title\">$hash</div>\n";
2577 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002578 git_print_page_path($file_name, "blob", $hash_base);
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002579 print "<div class=\"page_body\">\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002580 my $nr;
2581 while (my $line = <$fd>) {
Kay Sieversc07ad4b2005-08-07 20:22:44 +02002582 chomp $line;
Kay Sievers161332a2005-08-07 19:49:46 +02002583 $nr++;
Jakub Narebskif16db172006-08-06 02:08:31 +02002584 $line = untabify($line);
Jakub Narebski952c65f2006-08-22 16:52:50 +02002585 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2586 $nr, $nr, $nr, esc_html($line);
Kay Sievers161332a2005-08-07 19:49:46 +02002587 }
Jakub Narebski952c65f2006-08-22 16:52:50 +02002588 close $fd
2589 or print "Reading blob failed.\n";
Kay Sieversfbb592a2005-08-07 20:12:11 +02002590 print "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002591 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002592}
2593
2594sub git_tree {
Kay Sieversb87d78d2005-08-07 20:21:04 +02002595 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002596 $hash = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02002597 if (defined $file_name) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002598 my $base = $hash_base || $hash;
Kay Sievers09bd7892005-08-07 20:21:23 +02002599 $hash = git_get_hash_by_path($base, $file_name, "tree");
2600 }
Kay Sievers10dba282005-08-07 20:25:27 +02002601 if (!defined $hash_base) {
Junio C Hamanodf2c37a2006-01-09 13:13:39 +01002602 $hash_base = $hash;
Kay Sievers10dba282005-08-07 20:25:27 +02002603 }
Kay Sieverse925f382005-08-07 20:23:35 +02002604 }
Kay Sievers232ff552005-11-24 16:56:55 +01002605 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002606 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002607 or die_error(undef, "Open git-ls-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002608 my @entries = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002609 close $fd or die_error(undef, "Reading tree failed");
Kay Sievers232ff552005-11-24 16:56:55 +01002610 $/ = "\n";
Kay Sieversd63577d2005-08-07 20:18:13 +02002611
Jakub Narebski847e01f2006-08-14 02:05:47 +02002612 my $refs = git_get_references();
2613 my $ref = format_ref_marker($refs, $hash_base);
Kay Sievers12a88f22005-08-07 20:02:47 +02002614 git_header_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002615 my $base = "";
Aneesh Kumar K.V1d3fc682006-09-01 09:13:32 +05302616 my ($have_blame) = gitweb_check_feature('blame');
Jakub Narebski847e01f2006-08-14 02:05:47 +02002617 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002618 git_print_page_nav('tree','', $hash_base);
2619 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
Kay Sieversd63577d2005-08-07 20:18:13 +02002620 } else {
Jakub Narebskifa702002006-08-31 00:35:07 +02002621 undef $hash_base;
Kay Sieversd63577d2005-08-07 20:18:13 +02002622 print "<div class=\"page_nav\">\n";
2623 print "<br/><br/></div>\n";
2624 print "<div class=\"title\">$hash</div>\n";
2625 }
Kay Sievers09bd7892005-08-07 20:21:23 +02002626 if (defined $file_name) {
Kay Sievers232ff552005-11-24 16:56:55 +01002627 $base = esc_html("$file_name/");
Kay Sievers09bd7892005-08-07 20:21:23 +02002628 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07002629 git_print_page_path($file_name, 'tree', $hash_base);
Kay Sieversfbb592a2005-08-07 20:12:11 +02002630 print "<div class=\"page_body\">\n";
Kay Sievers42f7eb92005-08-07 20:21:46 +02002631 print "<table cellspacing=\"0\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002632 my $alternate = 0;
Kay Sievers161332a2005-08-07 19:49:46 +02002633 foreach my $line (@entries) {
Jakub Narebskicb849b42006-08-31 00:32:15 +02002634 my %t = parse_ls_tree_line($line, -z => 1);
2635
Kay Sieversbddec012005-08-07 20:25:42 +02002636 if ($alternate) {
Kay Sieversc994d622005-08-07 20:27:18 +02002637 print "<tr class=\"dark\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002638 } else {
Kay Sieversc994d622005-08-07 20:27:18 +02002639 print "<tr class=\"light\">\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002640 }
2641 $alternate ^= 1;
Jakub Narebskicb849b42006-08-31 00:32:15 +02002642
Jakub Narebskifa702002006-08-31 00:35:07 +02002643 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2644
Kay Sievers42f7eb92005-08-07 20:21:46 +02002645 print "</tr>\n";
Kay Sievers161332a2005-08-07 19:49:46 +02002646 }
Kay Sievers42f7eb92005-08-07 20:21:46 +02002647 print "</table>\n" .
2648 "</div>";
Kay Sievers12a88f22005-08-07 20:02:47 +02002649 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002650}
2651
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302652sub git_snapshot {
2653
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302654 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2655 my $have_snapshot = (defined $ctype && defined $suffix);
2656 if (!$have_snapshot) {
2657 die_error('403 Permission denied', "Permission denied");
2658 }
2659
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302660 if (!defined $hash) {
2661 $hash = git_get_head_hash($project);
2662 }
2663
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302664 my $filename = basename($project) . "-$hash.tar.$suffix";
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302665
2666 print $cgi->header(-type => 'application/x-tar',
Jakub Narebski134a69412006-08-22 16:55:34 +02002667 -content_encoding => $ctype,
2668 -content_disposition => "inline; filename=\"$filename\"",
Jakub Narebski952c65f2006-08-22 16:52:50 +02002669 -status => '200 OK');
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302670
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002671 my $git_command = git_cmd_str();
2672 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302673 die_error(undef, "Execute git-tar-tree failed.");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302674 binmode STDOUT, ':raw';
2675 print <$fd>;
2676 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2677 close $fd;
2678
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302679}
2680
Kay Sievers09bd7892005-08-07 20:21:23 +02002681sub git_log {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002682 my $head = git_get_head_hash($project);
Kay Sievers0db37972005-08-07 20:24:35 +02002683 if (!defined $hash) {
Kay Sievers19806692005-08-07 20:26:27 +02002684 $hash = $head;
Kay Sievers0db37972005-08-07 20:24:35 +02002685 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02002686 if (!defined $page) {
2687 $page = 0;
Kay Sieversb87d78d2005-08-07 20:21:04 +02002688 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002689 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02002690
2691 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002692 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002693 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002694 my @revlist = map { chomp; $_ } <$fd>;
Kay Sieversea4a6df2005-08-07 20:26:49 +02002695 close $fd;
2696
Jakub Narebski847e01f2006-08-14 02:05:47 +02002697 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002698
2699 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02002700 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002701
Kay Sieversb87d78d2005-08-07 20:21:04 +02002702 if (!@revlist) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002703 my %co = parse_commit($hash);
Jakub Narebski27fb8c42006-07-30 20:32:01 +02002704
Jakub Narebski847e01f2006-08-14 02:05:47 +02002705 git_print_header_div('summary', $project);
Kay Sieverse925f382005-08-07 20:23:35 +02002706 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002707 }
Kay Sieversc994d622005-08-07 20:27:18 +02002708 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2709 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02002710 my $ref = format_ref_marker($refs, $commit);
2711 my %co = parse_commit($commit);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002712 next if !%co;
Jakub Narebski847e01f2006-08-14 02:05:47 +02002713 my %ad = parse_date($co{'author_epoch'});
2714 git_print_header_div('commit',
Jakub Narebski26298b52006-08-10 12:38:47 +02002715 "<span class=\"age\">$co{'age_string'}</span>" .
2716 esc_html($co{'title'}) . $ref,
2717 $commit);
Kay Sievers034df392005-08-07 20:20:07 +02002718 print "<div class=\"title_text\">\n" .
2719 "<div class=\"log_link\">\n" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002720 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002721 " | " .
2722 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
Kay Sieverseb282402005-08-07 20:21:34 +02002723 "<br/>\n" .
Kay Sievers034df392005-08-07 20:20:07 +02002724 "</div>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01002725 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
Jakub Narebskid16d0932006-08-17 11:21:23 +02002726 "</div>\n";
2727
2728 print "<div class=\"log_body\">\n";
2729 git_print_simplified_log($co{'comment'});
Kay Sievers09bd7892005-08-07 20:21:23 +02002730 print "</div>\n";
Kay Sievers034df392005-08-07 20:20:07 +02002731 }
2732 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002733}
2734
2735sub git_commit {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002736 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02002737 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002738 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02002739 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002740 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2741 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
Kay Sievers161332a2005-08-07 19:49:46 +02002742
Kay Sieversd8a20ba2005-08-07 20:28:53 +02002743 my $parent = $co{'parent'};
2744 if (!defined $parent) {
Jakub Narebskib9182982006-07-30 18:28:34 -07002745 $parent = "--root";
Kay Sievers6191f8e2005-08-07 20:19:56 +02002746 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002747 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002748 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02002749 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02002750 close $fd or die_error(undef, "Reading git-diff-tree failed");
Kay Sievers11044292005-10-19 03:18:45 +02002751
2752 # non-textual hash id's can be cached
2753 my $expires;
2754 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2755 $expires = "+1d";
2756 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02002757 my $refs = git_get_references();
2758 my $ref = format_ref_marker($refs, $co{'id'});
Aneesh Kumar K.Vddb8d902006-08-20 11:53:04 +05302759
2760 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2761 my $have_snapshot = (defined $ctype && defined $suffix);
2762
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02002763 my $formats_nav = '';
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002764 if (defined $file_name && defined $co{'parent'}) {
2765 my $parent = $co{'parent'};
Jakub Narebski952c65f2006-08-22 16:52:50 +02002766 $formats_nav .=
2767 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2768 "blame");
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002769 }
Jakub Narebski594e2122006-07-31 02:21:52 +02002770 git_header_html(undef, $expires);
Jakub Narebski847e01f2006-08-14 02:05:47 +02002771 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
Jakub Narebski952c65f2006-08-22 16:52:50 +02002772 $hash, $co{'tree'}, $hash,
2773 $formats_nav);
Luben Tuikov4f7b34c2006-07-23 13:36:32 -07002774
Kay Sieversb87d78d2005-08-07 20:21:04 +02002775 if (defined $co{'parent'}) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002776 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002777 } else {
Jakub Narebski847e01f2006-08-14 02:05:47 +02002778 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
Kay Sieversb87d78d2005-08-07 20:21:04 +02002779 }
Kay Sievers6191f8e2005-08-07 20:19:56 +02002780 print "<div class=\"title_text\">\n" .
Kay Sieversb87d78d2005-08-07 20:21:04 +02002781 "<table cellspacing=\"0\">\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002782 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
Kay Sieversbddec012005-08-07 20:25:42 +02002783 "<tr>" .
2784 "<td></td><td> $ad{'rfc2822'}";
Kay Sievers927dcec2005-08-07 20:18:44 +02002785 if ($ad{'hour_local'} < 6) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002786 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2787 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sieversb87d78d2005-08-07 20:21:04 +02002788 } else {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002789 printf(" (%02d:%02d %s)",
2790 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002791 }
Kay Sieversbddec012005-08-07 20:25:42 +02002792 print "</td>" .
2793 "</tr>\n";
Kay Sievers40c13812005-11-19 17:41:29 +01002794 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
Jakub Narebski952c65f2006-08-22 16:52:50 +02002795 print "<tr><td></td><td> $cd{'rfc2822'}" .
2796 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2797 "</td></tr>\n";
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002798 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
Kay Sieversbddec012005-08-07 20:25:42 +02002799 print "<tr>" .
2800 "<td>tree</td>" .
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00002801 "<td class=\"sha1\">" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002802 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2803 class => "list"}, $co{'tree'}) .
Kay Sievers19806692005-08-07 20:26:27 +02002804 "</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002805 "<td class=\"link\">" .
2806 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2807 "tree");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302808 if ($have_snapshot) {
Jakub Narebski952c65f2006-08-22 16:52:50 +02002809 print " | " .
2810 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
Aneesh Kumar K.Vcb9c6e52006-08-17 20:59:46 +05302811 }
2812 print "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002813 "</tr>\n";
Jakub Narebski8adc4bd2006-06-22 08:52:57 +02002814 my $parents = $co{'parents'};
Kay Sievers3e029292005-08-07 20:05:15 +02002815 foreach my $par (@$parents) {
Kay Sieversbddec012005-08-07 20:25:42 +02002816 print "<tr>" .
2817 "<td>parent</td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002818 "<td class=\"sha1\">" .
2819 $cgi->a({-href => href(action=>"commit", hash=>$par),
2820 class => "list"}, $par) .
2821 "</td>" .
Kay Sieversbddec012005-08-07 20:25:42 +02002822 "<td class=\"link\">" .
Martin Waitz1c2a4f52006-08-16 00:24:30 +02002823 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02002824 " | " .
Jakub Narebskif2e60942006-09-03 23:43:03 +02002825 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
Kay Sieversbddec012005-08-07 20:25:42 +02002826 "</td>" .
2827 "</tr>\n";
Kay Sievers3e029292005-08-07 20:05:15 +02002828 }
Jakub Narebski7a9b4c52006-06-21 09:48:02 +02002829 print "</table>".
Kay Sieversb87d78d2005-08-07 20:21:04 +02002830 "</div>\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002831
Kay Sieversfbb592a2005-08-07 20:12:11 +02002832 print "<div class=\"page_body\">\n";
Jakub Narebskid16d0932006-08-17 11:21:23 +02002833 git_print_log($co{'comment'});
Kay Sievers927dcec2005-08-07 20:18:44 +02002834 print "</div>\n";
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002835
Jakub Narebskieee08902006-08-24 00:15:14 +02002836 git_difftree_body(\@difftree, $hash, $parent);
Jakub Narebski4a4a1a52006-08-14 02:18:33 +02002837
Kay Sievers12a88f22005-08-07 20:02:47 +02002838 git_footer_html();
Kay Sievers09bd7892005-08-07 20:21:23 +02002839}
2840
2841sub git_blobdiff {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002842 my $format = shift || 'html';
2843
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002844 my $fd;
2845 my @difftree;
2846 my %diffinfo;
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002847 my $expires;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002848
2849 # preparing $fd and %diffinfo for git_patchset_body
2850 # new style URI
2851 if (defined $hash_base && defined $hash_parent_base) {
2852 if (defined $file_name) {
2853 # read raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002854 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002855 "--", $file_name
2856 or die_error(undef, "Open git-diff-tree failed");
2857 @difftree = map { chomp; $_ } <$fd>;
2858 close $fd
2859 or die_error(undef, "Reading git-diff-tree failed");
2860 @difftree
2861 or die_error('404 Not Found', "Blob diff not found");
2862
Jakub Narebski0aea3372006-08-27 23:45:26 +02002863 } elsif (defined $hash &&
2864 $hash =~ /[0-9a-fA-F]{40}/) {
2865 # try to find filename from $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002866
2867 # read filtered raw output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002868 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002869 or die_error(undef, "Open git-diff-tree failed");
2870 @difftree =
2871 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
2872 # $hash == to_id
2873 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2874 map { chomp; $_ } <$fd>;
2875 close $fd
2876 or die_error(undef, "Reading git-diff-tree failed");
2877 @difftree
2878 or die_error('404 Not Found', "Blob diff not found");
2879
2880 } else {
2881 die_error('404 Not Found', "Missing one of the blob diff parameters");
2882 }
2883
2884 if (@difftree > 1) {
2885 die_error('404 Not Found', "Ambiguous blob diff specification");
2886 }
2887
2888 %diffinfo = parse_difftree_raw_line($difftree[0]);
2889 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2890 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
2891
2892 $hash_parent ||= $diffinfo{'from_id'};
2893 $hash ||= $diffinfo{'to_id'};
2894
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002895 # non-textual hash id's can be cached
2896 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2897 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2898 $expires = '+1d';
2899 }
2900
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002901 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002902 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02002903 '-p', $hash_parent_base, $hash_base,
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002904 "--", $file_name
2905 or die_error(undef, "Open git-diff-tree failed");
2906 }
2907
2908 # old/legacy style URI
2909 if (!%diffinfo && # if new style URI failed
2910 defined $hash && defined $hash_parent) {
2911 # fake git-diff-tree raw output
2912 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2913 $diffinfo{'from_id'} = $hash_parent;
2914 $diffinfo{'to_id'} = $hash;
2915 if (defined $file_name) {
2916 if (defined $file_parent) {
2917 $diffinfo{'status'} = '2';
2918 $diffinfo{'from_file'} = $file_parent;
2919 $diffinfo{'to_file'} = $file_name;
2920 } else { # assume not renamed
2921 $diffinfo{'status'} = '1';
2922 $diffinfo{'from_file'} = $file_name;
2923 $diffinfo{'to_file'} = $file_name;
2924 }
2925 } else { # no filename given
2926 $diffinfo{'status'} = '2';
2927 $diffinfo{'from_file'} = $hash_parent;
2928 $diffinfo{'to_file'} = $hash;
2929 }
2930
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002931 # non-textual hash id's can be cached
2932 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2933 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2934 $expires = '+1d';
2935 }
2936
2937 # open patch output
Dennis Stosberg25691fb2006-08-28 17:49:58 +02002938 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002939 or die_error(undef, "Open git-diff failed");
2940 } else {
2941 die_error('404 Not Found', "Missing one of the blob diff parameters")
2942 unless %diffinfo;
2943 }
2944
2945 # header
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002946 if ($format eq 'html') {
2947 my $formats_nav =
2948 $cgi->a({-href => href(action=>"blobdiff_plain",
2949 hash=>$hash, hash_parent=>$hash_parent,
2950 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2951 file_name=>$file_name, file_parent=>$file_parent)},
2952 "plain");
2953 git_header_html(undef, $expires);
2954 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2955 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2956 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2957 } else {
2958 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2959 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2960 }
2961 if (defined $file_name) {
2962 git_print_page_path($file_name, "blob", $hash_base);
2963 } else {
2964 print "<div class=\"page_path\"></div>\n";
2965 }
2966
2967 } elsif ($format eq 'plain') {
2968 print $cgi->header(
2969 -type => 'text/plain',
2970 -charset => 'utf-8',
2971 -expires => $expires,
2972 -content_disposition => qq(inline; filename="${file_name}.patch"));
2973
2974 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2975
Kay Sievers09bd7892005-08-07 20:21:23 +02002976 } else {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002977 die_error(undef, "Unknown blobdiff format");
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002978 }
2979
2980 # patch
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002981 if ($format eq 'html') {
2982 print "<div class=\"page_body\">\n";
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002983
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002984 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2985 close $fd;
Jakub Narebski7c5e2eb2006-08-25 21:13:34 +02002986
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02002987 print "</div>\n"; # class="page_body"
2988 git_footer_html();
2989
2990 } else {
2991 while (my $line = <$fd>) {
2992 $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2993 $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2994
2995 print $line;
2996
2997 last if $line =~ m!^\+\+\+!;
2998 }
2999 local $/ = undef;
3000 print <$fd>;
3001 close $fd;
3002 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003003}
3004
Kay Sievers19806692005-08-07 20:26:27 +02003005sub git_blobdiff_plain {
Jakub Narebski9b71b1f2006-08-25 21:14:49 +02003006 git_blobdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003007}
3008
Kay Sievers09bd7892005-08-07 20:21:23 +02003009sub git_commitdiff {
Jakub Narebskieee08902006-08-24 00:15:14 +02003010 my $format = shift || 'html';
Jakub Narebski847e01f2006-08-14 02:05:47 +02003011 my %co = parse_commit($hash);
Kay Sievers034df392005-08-07 20:20:07 +02003012 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003013 die_error(undef, "Unknown commit object");
Kay Sieversd63577d2005-08-07 20:18:13 +02003014 }
Kay Sieversbddec012005-08-07 20:25:42 +02003015 if (!defined $hash_parent) {
Jakub Narebskib5ff2cf2006-08-06 16:14:25 +02003016 $hash_parent = $co{'parent'} || '--root';
Kay Sieversbddec012005-08-07 20:25:42 +02003017 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003018
3019 # read commitdiff
3020 my $fd;
3021 my @difftree;
Jakub Narebskieee08902006-08-24 00:15:14 +02003022 if ($format eq 'html') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003023 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebskieee08902006-08-24 00:15:14 +02003024 "--patch-with-raw", "--full-index", $hash_parent, $hash
3025 or die_error(undef, "Open git-diff-tree failed");
3026
3027 while (chomp(my $line = <$fd>)) {
3028 # empty line ends raw part of diff-tree output
3029 last unless $line;
3030 push @difftree, $line;
3031 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003032
Jakub Narebskieee08902006-08-24 00:15:14 +02003033 } elsif ($format eq 'plain') {
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003034 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003035 '-p', $hash_parent, $hash
Jakub Narebskieee08902006-08-24 00:15:14 +02003036 or die_error(undef, "Open git-diff-tree failed");
Jakub Narebski157e43b2006-08-24 19:34:36 +02003037
Jakub Narebskieee08902006-08-24 00:15:14 +02003038 } else {
3039 die_error(undef, "Unknown commitdiff format");
3040 }
Kay Sievers4c02e3c2005-08-07 19:52:52 +02003041
Kay Sievers11044292005-10-19 03:18:45 +02003042 # non-textual hash id's can be cached
3043 my $expires;
3044 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3045 $expires = "+1d";
3046 }
Kay Sievers09bd7892005-08-07 20:21:23 +02003047
Jakub Narebskieee08902006-08-24 00:15:14 +02003048 # write commit message
3049 if ($format eq 'html') {
3050 my $refs = git_get_references();
3051 my $ref = format_ref_marker($refs, $co{'id'});
3052 my $formats_nav =
3053 $cgi->a({-href => href(action=>"commitdiff_plain",
3054 hash=>$hash, hash_parent=>$hash_parent)},
3055 "plain");
Kay Sievers19806692005-08-07 20:26:27 +02003056
Jakub Narebskieee08902006-08-24 00:15:14 +02003057 git_header_html(undef, $expires);
3058 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3059 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
Jakub Narebski6fd92a22006-08-28 14:48:12 +02003060 git_print_authorship(\%co);
Jakub Narebskieee08902006-08-24 00:15:14 +02003061 print "<div class=\"page_body\">\n";
3062 print "<div class=\"log\">\n";
3063 git_print_simplified_log($co{'comment'}, 1); # skip title
3064 print "</div>\n"; # class="log"
Kay Sievers1b1cd422005-08-07 20:28:01 +02003065
Jakub Narebskieee08902006-08-24 00:15:14 +02003066 } elsif ($format eq 'plain') {
3067 my $refs = git_get_references("tags");
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003068 my $tagname = git_get_rev_name_tags($hash);
Jakub Narebskieee08902006-08-24 00:15:14 +02003069 my $filename = basename($project) . "-$hash.patch";
3070
3071 print $cgi->header(
3072 -type => 'text/plain',
3073 -charset => 'utf-8',
3074 -expires => $expires,
3075 -content_disposition => qq(inline; filename="$filename"));
3076 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3077 print <<TEXT;
Jakub Narebski59b9f612006-08-22 23:42:53 +02003078From: $co{'author'}
3079Date: $ad{'rfc2822'} ($ad{'tz_local'})
3080Subject: $co{'title'}
3081TEXT
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003082 print "X-Git-Tag: $tagname\n" if $tagname;
Jakub Narebskieee08902006-08-24 00:15:14 +02003083 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
Jakub Narebskiedf735a2006-08-24 19:45:30 +02003084
Jakub Narebskieee08902006-08-24 00:15:14 +02003085 foreach my $line (@{$co{'comment'}}) {
3086 print "$line\n";
Kay Sievers19806692005-08-07 20:26:27 +02003087 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003088 print "---\n\n";
Kay Sievers19806692005-08-07 20:26:27 +02003089 }
Jakub Narebskieee08902006-08-24 00:15:14 +02003090
3091 # write patch
3092 if ($format eq 'html') {
Jakub Narebskib4657e72006-08-28 14:48:14 +02003093 git_difftree_body(\@difftree, $hash, $hash_parent);
3094 print "<br/>\n";
Jakub Narebskieee08902006-08-24 00:15:14 +02003095
Jakub Narebski157e43b2006-08-24 19:34:36 +02003096 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3097 close $fd;
Jakub Narebskieee08902006-08-24 00:15:14 +02003098 print "</div>\n"; # class="page_body"
3099 git_footer_html();
3100
3101 } elsif ($format eq 'plain') {
3102 local $/ = undef;
3103 print <$fd>;
3104 close $fd
3105 or print "Reading git-diff-tree failed\n";
3106 }
3107}
3108
3109sub git_commitdiff_plain {
3110 git_commitdiff('plain');
Kay Sievers19806692005-08-07 20:26:27 +02003111}
3112
Kay Sievers09bd7892005-08-07 20:21:23 +02003113sub git_history {
Luben Tuikovc6e1d9e2006-07-23 13:26:30 -07003114 if (!defined $hash_base) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003115 $hash_base = git_get_head_hash($project);
Kay Sievers09bd7892005-08-07 20:21:23 +02003116 }
Luben Tuikov63433102006-07-23 13:31:15 -07003117 my $ftype;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003118 my %co = parse_commit($hash_base);
Kay Sievers09bd7892005-08-07 20:21:23 +02003119 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003120 die_error(undef, "Unknown commit object");
Kay Sievers2ae100d2005-08-07 20:17:00 +02003121 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003122 my $refs = git_get_references();
Kay Sieversd51e9022005-08-07 20:16:07 +02003123 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003124 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
3125 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
Luben Tuikov93d5f062006-07-23 13:30:08 -07003126 if (!defined $hash && defined $file_name) {
3127 $hash = git_get_hash_by_path($hash_base, $file_name);
3128 }
Luben Tuikovcff07712006-07-23 13:28:55 -07003129 if (defined $hash) {
Luben Tuikov63433102006-07-23 13:31:15 -07003130 $ftype = git_get_type($hash);
Luben Tuikovcff07712006-07-23 13:28:55 -07003131 }
Luben Tuikov59fb1c92006-08-17 10:39:29 -07003132 git_print_page_path($file_name, $ftype, $hash_base);
Kay Sievers10dba282005-08-07 20:25:27 +02003133
Junio C Hamanocdd40372006-06-30 18:54:32 -07003134 open my $fd, "-|",
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003135 git_cmd(), "rev-list", "--full-history", $hash_base, "--", $file_name;
3136
Jakub Narebski581860e2006-08-14 02:09:08 +02003137 git_history_body($fd, $refs, $hash_base, $ftype);
3138
Kay Sieversb87d78d2005-08-07 20:21:04 +02003139 close $fd;
Kay Sieversd51e9022005-08-07 20:16:07 +02003140 git_footer_html();
Kay Sievers161332a2005-08-07 19:49:46 +02003141}
Kay Sievers19806692005-08-07 20:26:27 +02003142
3143sub git_search {
3144 if (!defined $searchtext) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003145 die_error(undef, "Text field empty");
Kay Sievers19806692005-08-07 20:26:27 +02003146 }
3147 if (!defined $hash) {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003148 $hash = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003149 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003150 my %co = parse_commit($hash);
Kay Sievers19806692005-08-07 20:26:27 +02003151 if (!%co) {
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003152 die_error(undef, "Unknown commit object");
Kay Sievers19806692005-08-07 20:26:27 +02003153 }
Jakub Narebski04f7a942006-09-11 00:29:27 +02003154
Kay Sieversc994d622005-08-07 20:27:18 +02003155 my $commit_search = 1;
3156 my $author_search = 0;
3157 my $committer_search = 0;
3158 my $pickaxe_search = 0;
3159 if ($searchtext =~ s/^author\\://i) {
3160 $author_search = 1;
3161 } elsif ($searchtext =~ s/^committer\\://i) {
3162 $committer_search = 1;
3163 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3164 $commit_search = 0;
3165 $pickaxe_search = 1;
Jakub Narebski04f7a942006-09-11 00:29:27 +02003166
3167 # pickaxe may take all resources of your box and run for several minutes
3168 # with every query - so decide by yourself how public you make this feature
3169 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3170 if (!$have_pickaxe) {
3171 die_error('403 Permission denied', "Permission denied");
3172 }
Kay Sieversc994d622005-08-07 20:27:18 +02003173 }
Kay Sievers19806692005-08-07 20:26:27 +02003174 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003175 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3176 git_print_header_div('commit', esc_html($co{'title'}), $hash);
Kay Sievers19806692005-08-07 20:26:27 +02003177
Kay Sievers19806692005-08-07 20:26:27 +02003178 print "<table cellspacing=\"0\">\n";
Kay Sievers19806692005-08-07 20:26:27 +02003179 my $alternate = 0;
Kay Sieversc994d622005-08-07 20:27:18 +02003180 if ($commit_search) {
3181 $/ = "\0";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003182 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
Kay Sieversc994d622005-08-07 20:27:18 +02003183 while (my $commit_text = <$fd>) {
3184 if (!grep m/$searchtext/i, $commit_text) {
Kay Sievers19806692005-08-07 20:26:27 +02003185 next;
3186 }
Kay Sieversc994d622005-08-07 20:27:18 +02003187 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3188 next;
Kay Sievers19806692005-08-07 20:26:27 +02003189 }
Kay Sieversc994d622005-08-07 20:27:18 +02003190 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3191 next;
3192 }
3193 my @commit_lines = split "\n", $commit_text;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003194 my %co = parse_commit(undef, \@commit_lines);
Kay Sieversc994d622005-08-07 20:27:18 +02003195 if (!%co) {
3196 next;
3197 }
3198 if ($alternate) {
3199 print "<tr class=\"dark\">\n";
3200 } else {
3201 print "<tr class=\"light\">\n";
3202 }
3203 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003204 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003205 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003206 "<td>" .
Jakub Narebski63e42202006-08-22 12:38:59 +02003207 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3208 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003209 my $comment = $co{'comment'};
3210 foreach my $line (@$comment) {
3211 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
Kay Sievers40c13812005-11-19 17:41:29 +01003212 my $lead = esc_html($1) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003213 $lead = chop_str($lead, 30, 10);
Kay Sievers40c13812005-11-19 17:41:29 +01003214 my $match = esc_html($2) || "";
3215 my $trail = esc_html($3) || "";
Kay Sieversc994d622005-08-07 20:27:18 +02003216 $trail = chop_str($trail, 30, 10);
Jakub Narebski1f1ab5f2006-06-20 14:58:12 +00003217 my $text = "$lead<span class=\"match\">$match</span>$trail";
Kay Sieversc994d622005-08-07 20:27:18 +02003218 print chop_str($text, 80, 5) . "<br/>\n";
3219 }
3220 }
3221 print "</td>\n" .
3222 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003223 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003224 " | " .
3225 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003226 print "</td>\n" .
3227 "</tr>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003228 }
Kay Sieversc994d622005-08-07 20:27:18 +02003229 close $fd;
3230 }
3231
3232 if ($pickaxe_search) {
3233 $/ = "\n";
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003234 my $git_command = git_cmd_str();
3235 open my $fd, "-|", "$git_command rev-list $hash | " .
3236 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
Kay Sieversc994d622005-08-07 20:27:18 +02003237 undef %co;
3238 my @files;
3239 while (my $line = <$fd>) {
3240 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3241 my %set;
3242 $set{'file'} = $6;
3243 $set{'from_id'} = $3;
3244 $set{'to_id'} = $4;
3245 $set{'id'} = $set{'to_id'};
3246 if ($set{'id'} =~ m/0{40}/) {
3247 $set{'id'} = $set{'from_id'};
3248 }
3249 if ($set{'id'} =~ m/0{40}/) {
3250 next;
3251 }
3252 push @files, \%set;
Kay Sievers53b89d82005-08-12 22:12:58 +02003253 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
Kay Sieversc994d622005-08-07 20:27:18 +02003254 if (%co) {
3255 if ($alternate) {
3256 print "<tr class=\"dark\">\n";
3257 } else {
3258 print "<tr class=\"light\">\n";
3259 }
3260 $alternate ^= 1;
Kay Sievers71be1e72005-08-07 20:27:27 +02003261 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
Kay Sievers40c13812005-11-19 17:41:29 +01003262 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
Kay Sieversc994d622005-08-07 20:27:18 +02003263 "<td>" .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003264 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3265 -class => "list subject"},
Jakub Narebski63e42202006-08-22 12:38:59 +02003266 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
Kay Sieversc994d622005-08-07 20:27:18 +02003267 while (my $setref = shift @files) {
3268 my %set = %$setref;
Jakub Narebski952c65f2006-08-22 16:52:50 +02003269 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3270 hash=>$set{'id'}, file_name=>$set{'file'}),
3271 -class => "list"},
3272 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
Kay Sieversc994d622005-08-07 20:27:18 +02003273 "<br/>\n";
3274 }
3275 print "</td>\n" .
3276 "<td class=\"link\">" .
Martin Waitz756d2f02006-08-17 00:28:36 +02003277 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
Jakub Narebski952c65f2006-08-22 16:52:50 +02003278 " | " .
3279 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
Kay Sieversc994d622005-08-07 20:27:18 +02003280 print "</td>\n" .
3281 "</tr>\n";
3282 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003283 %co = parse_commit($1);
Kay Sieversc994d622005-08-07 20:27:18 +02003284 }
3285 }
3286 close $fd;
Kay Sievers19806692005-08-07 20:26:27 +02003287 }
3288 print "</table>\n";
Kay Sievers19806692005-08-07 20:26:27 +02003289 git_footer_html();
3290}
3291
3292sub git_shortlog {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003293 my $head = git_get_head_hash($project);
Kay Sievers19806692005-08-07 20:26:27 +02003294 if (!defined $hash) {
3295 $hash = $head;
3296 }
Kay Sieversea4a6df2005-08-07 20:26:49 +02003297 if (!defined $page) {
3298 $page = 0;
3299 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003300 my $refs = git_get_references();
Kay Sieversea4a6df2005-08-07 20:26:49 +02003301
3302 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003303 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003304 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski0881d2d2006-07-30 14:58:11 +02003305 my @revlist = map { chomp; $_ } <$fd>;
Kay Sievers19806692005-08-07 20:26:27 +02003306 close $fd;
Kay Sieversea4a6df2005-08-07 20:26:49 +02003307
Jakub Narebski847e01f2006-08-14 02:05:47 +02003308 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003309 my $next_link = '';
3310 if ($#revlist >= (100 * ($page+1)-1)) {
3311 $next_link =
Martin Waitz756d2f02006-08-17 00:28:36 +02003312 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003313 -title => "Alt-n"}, "next");
3314 }
3315
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003316
3317 git_header_html();
Jakub Narebski847e01f2006-08-14 02:05:47 +02003318 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3319 git_print_header_div('summary', $project);
Jakub Narebski0d83ddc2006-07-30 15:01:07 +02003320
Jakub Narebski9f5dcb82006-07-31 11:22:13 +02003321 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3322
Kay Sievers19806692005-08-07 20:26:27 +02003323 git_footer_html();
3324}
Jakub Narebski717b8312006-07-31 21:22:15 +02003325
3326## ......................................................................
3327## feeds (RSS, OPML)
3328
3329sub git_rss {
3330 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003331 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003332 or die_error(undef, "Open git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003333 my @revlist = map { chomp; $_ } <$fd>;
Jakub Narebskicac4bd92006-08-05 13:13:53 +02003334 close $fd or die_error(undef, "Reading git-rev-list failed");
Jakub Narebski717b8312006-07-31 21:22:15 +02003335 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003336 print <<XML;
3337<?xml version="1.0" encoding="utf-8"?>
3338<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3339<channel>
3340<title>$project $my_uri $my_url</title>
3341<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3342<description>$project log</description>
3343<language>en</language>
3344XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003345
3346 for (my $i = 0; $i <= $#revlist; $i++) {
3347 my $commit = $revlist[$i];
Jakub Narebski847e01f2006-08-14 02:05:47 +02003348 my %co = parse_commit($commit);
Jakub Narebski717b8312006-07-31 21:22:15 +02003349 # we read 150, we always show 30 and the ones more recent than 48 hours
3350 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3351 last;
3352 }
Jakub Narebski847e01f2006-08-14 02:05:47 +02003353 my %cd = parse_date($co{'committer_epoch'});
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003354 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003355 $co{'parent'}, $co{'id'}
3356 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003357 my @difftree = map { chomp; $_ } <$fd>;
Jakub Narebski6bcf4b42006-08-27 23:49:36 +02003358 close $fd
3359 or next;
Jakub Narebski717b8312006-07-31 21:22:15 +02003360 print "<item>\n" .
3361 "<title>" .
3362 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3363 "</title>\n" .
3364 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3365 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3366 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3367 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3368 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3369 "<content:encoded>" .
3370 "<![CDATA[\n";
3371 my $comment = $co{'comment'};
3372 foreach my $line (@$comment) {
3373 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3374 print "$line<br/>\n";
3375 }
3376 print "<br/>\n";
3377 foreach my $line (@difftree) {
3378 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3379 next;
3380 }
3381 my $file = validate_input(unquote($7));
3382 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3383 print "$file<br/>\n";
3384 }
3385 print "]]>\n" .
3386 "</content:encoded>\n" .
3387 "</item>\n";
3388 }
3389 print "</channel></rss>";
3390}
3391
3392sub git_opml {
Jakub Narebski847e01f2006-08-14 02:05:47 +02003393 my @list = git_get_projects_list();
Jakub Narebski717b8312006-07-31 21:22:15 +02003394
3395 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
Jakub Narebski59b9f612006-08-22 23:42:53 +02003396 print <<XML;
3397<?xml version="1.0" encoding="utf-8"?>
3398<opml version="1.0">
3399<head>
3400 <title>$site_name Git OPML Export</title>
3401</head>
3402<body>
3403<outline text="git RSS feeds">
3404XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003405
3406 foreach my $pr (@list) {
3407 my %proj = %$pr;
Jakub Narebski847e01f2006-08-14 02:05:47 +02003408 my $head = git_get_head_hash($proj{'path'});
Jakub Narebski717b8312006-07-31 21:22:15 +02003409 if (!defined $head) {
3410 next;
3411 }
Dennis Stosberg25691fb2006-08-28 17:49:58 +02003412 $git_dir = "$projectroot/$proj{'path'}";
Jakub Narebski847e01f2006-08-14 02:05:47 +02003413 my %co = parse_commit($head);
Jakub Narebski717b8312006-07-31 21:22:15 +02003414 if (!%co) {
3415 next;
3416 }
3417
3418 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3419 my $rss = "$my_url?p=$proj{'path'};a=rss";
3420 my $html = "$my_url?p=$proj{'path'};a=summary";
3421 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3422 }
Jakub Narebski59b9f612006-08-22 23:42:53 +02003423 print <<XML;
3424</outline>
3425</body>
3426</opml>
3427XML
Jakub Narebski717b8312006-07-31 21:22:15 +02003428}