]>
Commit | Line | Data |
---|---|---|
161332a5 KS |
1 | #!/usr/bin/perl |
2 | ||
c994d620 | 3 | # gitweb - simple web interface to track changes in git repositories |
22fafb99 | 4 | # |
00cd0794 KS |
5 | # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> |
6 | # (C) 2005, Christian Gierke | |
823d5dc8 | 7 | # |
d8f1c5c2 | 8 | # This program is licensed under the GPLv2 |
161332a5 KS |
9 | |
10 | use strict; | |
11 | use warnings; | |
19806691 | 12 | use CGI qw(:standard :escapeHTML -nosticky); |
7403d50b | 13 | use CGI::Util qw(unescape); |
161332a5 | 14 | use CGI::Carp qw(fatalsToBrowser); |
40c13813 | 15 | use Encode; |
b87d78d6 | 16 | use Fcntl ':mode'; |
7a13b999 | 17 | use File::Find qw(); |
cb9c6e5b | 18 | use File::Basename qw(basename); |
10bb9036 | 19 | binmode STDOUT, ':utf8'; |
161332a5 | 20 | |
b1f5f64f | 21 | BEGIN { |
3be8e720 | 22 | CGI->compile() if $ENV{'MOD_PERL'}; |
b1f5f64f JN |
23 | } |
24 | ||
4a87b43e | 25 | our $cgi = new CGI; |
06c084d2 | 26 | our $version = "++GIT_VERSION++"; |
4a87b43e DS |
27 | our $my_url = $cgi->url(); |
28 | our $my_uri = $cgi->url(-absolute => 1); | |
3e029299 | 29 | |
e130ddaa AT |
30 | # core git executable to use |
31 | # this can just be "git" if your webserver has a sensible PATH | |
06c084d2 | 32 | our $GIT = "++GIT_BINDIR++/git"; |
3f7f2710 | 33 | |
b87d78d6 | 34 | # absolute fs-path which will be prepended to the project path |
4a87b43e | 35 | #our $projectroot = "/pub/scm"; |
06c084d2 | 36 | our $projectroot = "++GITWEB_PROJECTROOT++"; |
b87d78d6 | 37 | |
ca5e9495 LL |
38 | # fs traversing limit for getting project list |
39 | # the number is relative to the projectroot | |
40 | our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++"; | |
41 | ||
b87d78d6 | 42 | # target of the home link on top of all pages |
6132b7e4 | 43 | our $home_link = $my_uri || "/"; |
b87d78d6 | 44 | |
2de21fac YS |
45 | # string of the home link on top of all pages |
46 | our $home_link_str = "++GITWEB_HOME_LINK_STR++"; | |
47 | ||
49da1daf AT |
48 | # name of your site or organization to appear in page titles |
49 | # replace this with something more descriptive for clearer bookmarks | |
8be2890c PB |
50 | our $site_name = "++GITWEB_SITENAME++" |
51 | || ($ENV{'SERVER_NAME'} || "Untitled") . " Git"; | |
49da1daf | 52 | |
b2d3476e AC |
53 | # filename of html text to include at top of each page |
54 | our $site_header = "++GITWEB_SITE_HEADER++"; | |
8ab1da2c | 55 | # html text to include at home page |
06c084d2 | 56 | our $home_text = "++GITWEB_HOMETEXT++"; |
b2d3476e AC |
57 | # filename of html text to include at bottom of each page |
58 | our $site_footer = "++GITWEB_SITE_FOOTER++"; | |
59 | ||
60 | # URI of stylesheets | |
61 | our @stylesheets = ("++GITWEB_CSS++"); | |
887a612f PB |
62 | # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. |
63 | our $stylesheet = undef; | |
9a7a62ff | 64 | # URI of GIT logo (72x27 size) |
06c084d2 | 65 | our $logo = "++GITWEB_LOGO++"; |
0b5deba1 JN |
66 | # URI of GIT favicon, assumed to be image/png type |
67 | our $favicon = "++GITWEB_FAVICON++"; | |
aedd9425 | 68 | |
9a7a62ff JN |
69 | # URI and label (title) of GIT logo link |
70 | #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; | |
71 | #our $logo_label = "git documentation"; | |
72 | our $logo_url = "http://git.or.cz/"; | |
73 | our $logo_label = "git homepage"; | |
51a7c66a | 74 | |
09bd7898 | 75 | # source of projects list |
06c084d2 | 76 | our $projects_list = "++GITWEB_LIST++"; |
b87d78d6 | 77 | |
55feb120 MH |
78 | # the width (in characters) of the projects list "Description" column |
79 | our $projects_list_description_width = 25; | |
80 | ||
b06dcf8c FL |
81 | # default order of projects list |
82 | # valid values are none, project, descr, owner, and age | |
83 | our $default_projects_order = "project"; | |
84 | ||
32f4aacc ML |
85 | # show repository only if this file exists |
86 | # (only effective if this variable evaluates to true) | |
87 | our $export_ok = "++GITWEB_EXPORT_OK++"; | |
88 | ||
89 | # only allow viewing of repositories also shown on the overview page | |
90 | our $strict_export = "++GITWEB_STRICT_EXPORT++"; | |
91 | ||
19a8721e JN |
92 | # list of git base URLs used for URL to where fetch project from, |
93 | # i.e. full URL is "$git_base_url/$project" | |
d6b7e0b9 | 94 | our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++"); |
19a8721e | 95 | |
f5aa79d9 | 96 | # default blob_plain mimetype and default charset for text/plain blob |
4a87b43e DS |
97 | our $default_blob_plain_mimetype = 'text/plain'; |
98 | our $default_text_plain_charset = undef; | |
f5aa79d9 | 99 | |
2d007374 PB |
100 | # file to use for guessing MIME types before trying /etc/mime.types |
101 | # (relative to the current git repository) | |
4a87b43e | 102 | our $mimetypes_file = undef; |
2d007374 | 103 | |
00f429af MK |
104 | # assume this charset if line contains non-UTF-8 characters; |
105 | # it should be valid encoding (see Encoding::Supported(3pm) for list), | |
106 | # for which encoding all byte sequences are valid, for example | |
107 | # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it | |
108 | # could be even 'utf-8' for the old behavior) | |
109 | our $fallback_encoding = 'latin1'; | |
110 | ||
69a9b41c JN |
111 | # rename detection options for git-diff and git-diff-tree |
112 | # - default is '-M', with the cost proportional to | |
113 | # (number of removed files) * (number of new files). | |
114 | # - more costly is '-C' (which implies '-M'), with the cost proportional to | |
115 | # (number of changed files + number of removed files) * (number of new files) | |
116 | # - even more costly is '-C', '--find-copies-harder' with cost | |
117 | # (number of files in the original tree) * (number of new files) | |
118 | # - one might want to include '-B' option, e.g. '-B', '-M' | |
119 | our @diff_opts = ('-M'); # taken from git_commit | |
120 | ||
a3c8ab30 MM |
121 | # information about snapshot formats that gitweb is capable of serving |
122 | our %known_snapshot_formats = ( | |
123 | # name => { | |
124 | # 'display' => display name, | |
125 | # 'type' => mime type, | |
126 | # 'suffix' => filename suffix, | |
127 | # 'format' => --format for git-archive, | |
128 | # 'compressor' => [compressor command and arguments] | |
129 | # (array reference, optional)} | |
130 | # | |
131 | 'tgz' => { | |
132 | 'display' => 'tar.gz', | |
133 | 'type' => 'application/x-gzip', | |
134 | 'suffix' => '.tar.gz', | |
135 | 'format' => 'tar', | |
136 | 'compressor' => ['gzip']}, | |
137 | ||
138 | 'tbz2' => { | |
139 | 'display' => 'tar.bz2', | |
140 | 'type' => 'application/x-bzip2', | |
141 | 'suffix' => '.tar.bz2', | |
142 | 'format' => 'tar', | |
143 | 'compressor' => ['bzip2']}, | |
144 | ||
145 | 'zip' => { | |
146 | 'display' => 'zip', | |
147 | 'type' => 'application/x-zip', | |
148 | 'suffix' => '.zip', | |
149 | 'format' => 'zip'}, | |
150 | ); | |
151 | ||
152 | # Aliases so we understand old gitweb.snapshot values in repository | |
153 | # configuration. | |
154 | our %known_snapshot_format_aliases = ( | |
155 | 'gzip' => 'tgz', | |
156 | 'bzip2' => 'tbz2', | |
157 | ||
158 | # backward compatibility: legacy gitweb config support | |
159 | 'x-gzip' => undef, 'gz' => undef, | |
160 | 'x-bzip2' => undef, 'bz2' => undef, | |
161 | 'x-zip' => undef, '' => undef, | |
162 | ); | |
163 | ||
ddb8d900 AK |
164 | # You define site-wide feature defaults here; override them with |
165 | # $GITWEB_CONFIG as necessary. | |
952c65fc | 166 | our %feature = ( |
17848fc6 JN |
167 | # feature => { |
168 | # 'sub' => feature-sub (subroutine), | |
169 | # 'override' => allow-override (boolean), | |
170 | # 'default' => [ default options...] (array reference)} | |
171 | # | |
b4b20b21 | 172 | # if feature is overridable (it means that allow-override has true value), |
17848fc6 JN |
173 | # then feature-sub will be called with default options as parameters; |
174 | # return value of feature-sub indicates if to enable specified feature | |
175 | # | |
b4b20b21 JN |
176 | # if there is no 'sub' key (no feature-sub), then feature cannot be |
177 | # overriden | |
178 | # | |
17848fc6 | 179 | # use gitweb_check_feature(<feature>) to check if <feature> is enabled |
952c65fc | 180 | |
45a3b12c PB |
181 | # Enable the 'blame' blob view, showing the last commit that modified |
182 | # each line in the file. This can be very CPU-intensive. | |
183 | ||
184 | # To enable system wide have in $GITWEB_CONFIG | |
185 | # $feature{'blame'}{'default'} = [1]; | |
186 | # To have project specific config enable override in $GITWEB_CONFIG | |
187 | # $feature{'blame'}{'override'} = 1; | |
188 | # and in project config gitweb.blame = 0|1; | |
952c65fc JN |
189 | 'blame' => { |
190 | 'sub' => \&feature_blame, | |
191 | 'override' => 0, | |
192 | 'default' => [0]}, | |
193 | ||
a3c8ab30 | 194 | # Enable the 'snapshot' link, providing a compressed archive of any |
45a3b12c PB |
195 | # tree. This can potentially generate high traffic if you have large |
196 | # project. | |
197 | ||
a3c8ab30 MM |
198 | # Value is a list of formats defined in %known_snapshot_formats that |
199 | # you wish to offer. | |
45a3b12c | 200 | # To disable system wide have in $GITWEB_CONFIG |
a3c8ab30 | 201 | # $feature{'snapshot'}{'default'} = []; |
45a3b12c | 202 | # To have project specific config enable override in $GITWEB_CONFIG |
bbee1d97 | 203 | # $feature{'snapshot'}{'override'} = 1; |
a3c8ab30 MM |
204 | # and in project config, a comma-separated list of formats or "none" |
205 | # to disable. Example: gitweb.snapshot = tbz2,zip; | |
952c65fc JN |
206 | 'snapshot' => { |
207 | 'sub' => \&feature_snapshot, | |
208 | 'override' => 0, | |
a3c8ab30 | 209 | 'default' => ['tgz']}, |
04f7a94f | 210 | |
6be93511 RF |
211 | # Enable text search, which will list the commits which match author, |
212 | # committer or commit text to a given string. Enabled by default. | |
b4b20b21 | 213 | # Project specific override is not supported. |
6be93511 RF |
214 | 'search' => { |
215 | 'override' => 0, | |
216 | 'default' => [1]}, | |
217 | ||
e7738553 PB |
218 | # Enable grep search, which will list the files in currently selected |
219 | # tree containing the given string. Enabled by default. This can be | |
220 | # potentially CPU-intensive, of course. | |
221 | ||
222 | # To enable system wide have in $GITWEB_CONFIG | |
223 | # $feature{'grep'}{'default'} = [1]; | |
224 | # To have project specific config enable override in $GITWEB_CONFIG | |
225 | # $feature{'grep'}{'override'} = 1; | |
226 | # and in project config gitweb.grep = 0|1; | |
227 | 'grep' => { | |
228 | 'override' => 0, | |
229 | 'default' => [1]}, | |
230 | ||
45a3b12c PB |
231 | # Enable the pickaxe search, which will list the commits that modified |
232 | # a given string in a file. This can be practical and quite faster | |
233 | # alternative to 'blame', but still potentially CPU-intensive. | |
234 | ||
235 | # To enable system wide have in $GITWEB_CONFIG | |
236 | # $feature{'pickaxe'}{'default'} = [1]; | |
237 | # To have project specific config enable override in $GITWEB_CONFIG | |
238 | # $feature{'pickaxe'}{'override'} = 1; | |
239 | # and in project config gitweb.pickaxe = 0|1; | |
04f7a94f JN |
240 | 'pickaxe' => { |
241 | 'sub' => \&feature_pickaxe, | |
242 | 'override' => 0, | |
243 | 'default' => [1]}, | |
9e756904 | 244 | |
45a3b12c PB |
245 | # Make gitweb use an alternative format of the URLs which can be |
246 | # more readable and natural-looking: project name is embedded | |
247 | # directly in the path and the query string contains other | |
248 | # auxiliary information. All gitweb installations recognize | |
249 | # URL in either format; this configures in which formats gitweb | |
250 | # generates links. | |
251 | ||
252 | # To enable system wide have in $GITWEB_CONFIG | |
253 | # $feature{'pathinfo'}{'default'} = [1]; | |
254 | # Project specific override is not supported. | |
255 | ||
256 | # Note that you will need to change the default location of CSS, | |
257 | # favicon, logo and possibly other files to an absolute URL. Also, | |
258 | # if gitweb.cgi serves as your indexfile, you will need to force | |
259 | # $my_uri to contain the script name in your $GITWEB_CONFIG. | |
9e756904 MW |
260 | 'pathinfo' => { |
261 | 'override' => 0, | |
262 | 'default' => [0]}, | |
e30496df PB |
263 | |
264 | # Make gitweb consider projects in project root subdirectories | |
265 | # to be forks of existing projects. Given project $projname.git, | |
266 | # projects matching $projname/*.git will not be shown in the main | |
267 | # projects list, instead a '+' mark will be added to $projname | |
268 | # there and a 'forks' view will be enabled for the project, listing | |
c2b8b134 FL |
269 | # all the forks. If project list is taken from a file, forks have |
270 | # to be listed after the main project. | |
e30496df PB |
271 | |
272 | # To enable system wide have in $GITWEB_CONFIG | |
273 | # $feature{'forks'}{'default'} = [1]; | |
274 | # Project specific override is not supported. | |
275 | 'forks' => { | |
276 | 'override' => 0, | |
277 | 'default' => [0]}, | |
ddb8d900 AK |
278 | ); |
279 | ||
280 | sub gitweb_check_feature { | |
281 | my ($name) = @_; | |
dd1ad5f1 | 282 | return unless exists $feature{$name}; |
952c65fc JN |
283 | my ($sub, $override, @defaults) = ( |
284 | $feature{$name}{'sub'}, | |
285 | $feature{$name}{'override'}, | |
286 | @{$feature{$name}{'default'}}); | |
ddb8d900 | 287 | if (!$override) { return @defaults; } |
a9455919 MW |
288 | if (!defined $sub) { |
289 | warn "feature $name is not overrideable"; | |
290 | return @defaults; | |
291 | } | |
ddb8d900 AK |
292 | return $sub->(@defaults); |
293 | } | |
294 | ||
ddb8d900 AK |
295 | sub feature_blame { |
296 | my ($val) = git_get_project_config('blame', '--bool'); | |
297 | ||
298 | if ($val eq 'true') { | |
299 | return 1; | |
300 | } elsif ($val eq 'false') { | |
301 | return 0; | |
302 | } | |
303 | ||
304 | return $_[0]; | |
305 | } | |
306 | ||
ddb8d900 | 307 | sub feature_snapshot { |
a3c8ab30 | 308 | my (@fmts) = @_; |
ddb8d900 AK |
309 | |
310 | my ($val) = git_get_project_config('snapshot'); | |
311 | ||
a3c8ab30 MM |
312 | if ($val) { |
313 | @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val); | |
ddb8d900 AK |
314 | } |
315 | ||
a3c8ab30 | 316 | return @fmts; |
de9272f4 LT |
317 | } |
318 | ||
e7738553 PB |
319 | sub feature_grep { |
320 | my ($val) = git_get_project_config('grep', '--bool'); | |
321 | ||
322 | if ($val eq 'true') { | |
323 | return (1); | |
324 | } elsif ($val eq 'false') { | |
325 | return (0); | |
326 | } | |
327 | ||
328 | return ($_[0]); | |
329 | } | |
330 | ||
04f7a94f JN |
331 | sub feature_pickaxe { |
332 | my ($val) = git_get_project_config('pickaxe', '--bool'); | |
333 | ||
334 | if ($val eq 'true') { | |
335 | return (1); | |
336 | } elsif ($val eq 'false') { | |
337 | return (0); | |
338 | } | |
339 | ||
340 | return ($_[0]); | |
341 | } | |
342 | ||
2172ce4b JH |
343 | # checking HEAD file with -e is fragile if the repository was |
344 | # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed | |
345 | # and then pruned. | |
346 | sub check_head_link { | |
347 | my ($dir) = @_; | |
348 | my $headfile = "$dir/HEAD"; | |
349 | return ((-e $headfile) || | |
350 | (-l $headfile && readlink($headfile) =~ /^refs\/heads\//)); | |
351 | } | |
352 | ||
353 | sub check_export_ok { | |
354 | my ($dir) = @_; | |
355 | return (check_head_link($dir) && | |
356 | (!$export_ok || -e "$dir/$export_ok")); | |
357 | } | |
358 | ||
a781785d JN |
359 | # process alternate names for backward compatibility |
360 | # filter out unsupported (unknown) snapshot formats | |
361 | sub filter_snapshot_fmts { | |
362 | my @fmts = @_; | |
363 | ||
364 | @fmts = map { | |
365 | exists $known_snapshot_format_aliases{$_} ? | |
366 | $known_snapshot_format_aliases{$_} : $_} @fmts; | |
367 | @fmts = grep(exists $known_snapshot_formats{$_}, @fmts); | |
368 | ||
369 | } | |
370 | ||
06c084d2 | 371 | our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++"; |
4b5dc988 | 372 | do $GITWEB_CONFIG if -e $GITWEB_CONFIG; |
c8d138a8 JK |
373 | |
374 | # version of the core git binary | |
375 | our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown"; | |
376 | ||
377 | $projects_list ||= $projectroot; | |
c8d138a8 | 378 | |
154b4d78 | 379 | # ====================================================================== |
09bd7898 | 380 | # input validation and dispatch |
4a87b43e | 381 | our $action = $cgi->param('a'); |
09bd7898 | 382 | if (defined $action) { |
c91da262 | 383 | if ($action =~ m/[^0-9a-zA-Z\.\-_]/) { |
cac4bd94 | 384 | die_error(undef, "Invalid action parameter"); |
b87d78d6 | 385 | } |
b87d78d6 | 386 | } |
44ad2978 | 387 | |
24d0693a | 388 | # parameters which are pathnames |
dd70235f | 389 | our $project = $cgi->param('p'); |
13d02165 | 390 | if (defined $project) { |
24d0693a | 391 | if (!validate_pathname($project) || |
7939fe44 | 392 | !(-d "$projectroot/$project") || |
2172ce4b | 393 | !check_head_link("$projectroot/$project") || |
32f4aacc ML |
394 | ($export_ok && !(-e "$projectroot/$project/$export_ok")) || |
395 | ($strict_export && !project_in_list($project))) { | |
7939fe44 | 396 | undef $project; |
cac4bd94 | 397 | die_error(undef, "No such project"); |
9cd3d988 | 398 | } |
a59d4afd | 399 | } |
6191f8e1 | 400 | |
4a87b43e | 401 | our $file_name = $cgi->param('f'); |
24d0693a JN |
402 | if (defined $file_name) { |
403 | if (!validate_pathname($file_name)) { | |
404 | die_error(undef, "Invalid file parameter"); | |
405 | } | |
406 | } | |
407 | ||
5c95fab0 | 408 | our $file_parent = $cgi->param('fp'); |
24d0693a JN |
409 | if (defined $file_parent) { |
410 | if (!validate_pathname($file_parent)) { | |
411 | die_error(undef, "Invalid file parent parameter"); | |
412 | } | |
413 | } | |
5c95fab0 | 414 | |
24d0693a | 415 | # parameters which are refnames |
4a87b43e | 416 | our $hash = $cgi->param('h'); |
4fac5294 | 417 | if (defined $hash) { |
24d0693a | 418 | if (!validate_refname($hash)) { |
cac4bd94 | 419 | die_error(undef, "Invalid hash parameter"); |
4fac5294 | 420 | } |
a59d4afd | 421 | } |
6191f8e1 | 422 | |
4a87b43e | 423 | our $hash_parent = $cgi->param('hp'); |
c91da262 | 424 | if (defined $hash_parent) { |
24d0693a | 425 | if (!validate_refname($hash_parent)) { |
cac4bd94 | 426 | die_error(undef, "Invalid hash parent parameter"); |
c91da262 | 427 | } |
09bd7898 KS |
428 | } |
429 | ||
4a87b43e | 430 | our $hash_base = $cgi->param('hb'); |
c91da262 | 431 | if (defined $hash_base) { |
24d0693a | 432 | if (!validate_refname($hash_base)) { |
cac4bd94 | 433 | die_error(undef, "Invalid hash base parameter"); |
c91da262 | 434 | } |
a59d4afd | 435 | } |
6191f8e1 | 436 | |
868bc068 MV |
437 | my %allowed_options = ( |
438 | "--no-merges" => [ qw(rss atom log shortlog history) ], | |
439 | ); | |
440 | ||
441 | our @extra_options = $cgi->param('opt'); | |
442 | if (defined @extra_options) { | |
12075103 JN |
443 | foreach my $opt (@extra_options) { |
444 | if (not exists $allowed_options{$opt}) { | |
868bc068 MV |
445 | die_error(undef, "Invalid option parameter"); |
446 | } | |
12075103 | 447 | if (not grep(/^$action$/, @{$allowed_options{$opt}})) { |
868bc068 MV |
448 | die_error(undef, "Invalid option parameter for this action"); |
449 | } | |
450 | } | |
451 | } | |
452 | ||
420e92f2 JN |
453 | our $hash_parent_base = $cgi->param('hpb'); |
454 | if (defined $hash_parent_base) { | |
24d0693a | 455 | if (!validate_refname($hash_parent_base)) { |
420e92f2 JN |
456 | die_error(undef, "Invalid hash parent base parameter"); |
457 | } | |
458 | } | |
459 | ||
24d0693a | 460 | # other parameters |
4a87b43e | 461 | our $page = $cgi->param('pg'); |
ea4a6df4 | 462 | if (defined $page) { |
ac8e3f2b | 463 | if ($page =~ m/[^0-9]/) { |
cac4bd94 | 464 | die_error(undef, "Invalid page parameter"); |
b87d78d6 | 465 | } |
2ad9331e | 466 | } |
823d5dc8 | 467 | |
e7738553 PB |
468 | our $searchtype = $cgi->param('st'); |
469 | if (defined $searchtype) { | |
470 | if ($searchtype =~ m/[^a-z]/) { | |
471 | die_error(undef, "Invalid searchtype parameter"); | |
472 | } | |
473 | } | |
474 | ||
4a87b43e | 475 | our $searchtext = $cgi->param('s'); |
7e431ef9 | 476 | our $search_regexp; |
19806691 | 477 | if (defined $searchtext) { |
9d032c72 RF |
478 | if (length($searchtext) < 2) { |
479 | die_error(undef, "At least two characters are required for search parameter"); | |
480 | } | |
7e431ef9 | 481 | $search_regexp = quotemeta $searchtext; |
19806691 KS |
482 | } |
483 | ||
dd70235f | 484 | # now read PATH_INFO and use it as alternative to parameters |
645927ce ML |
485 | sub evaluate_path_info { |
486 | return if defined $project; | |
487 | my $path_info = $ENV{"PATH_INFO"}; | |
488 | return if !$path_info; | |
cd90e75f | 489 | $path_info =~ s,^/+,,; |
645927ce | 490 | return if !$path_info; |
cd90e75f | 491 | # find which part of PATH_INFO is project |
dd70235f | 492 | $project = $path_info; |
cd90e75f | 493 | $project =~ s,/+$,,; |
2172ce4b | 494 | while ($project && !check_head_link("$projectroot/$project")) { |
dd70235f MW |
495 | $project =~ s,/*[^/]*$,,; |
496 | } | |
cd90e75f | 497 | # validate project |
24d0693a | 498 | $project = validate_pathname($project); |
645927ce ML |
499 | if (!$project || |
500 | ($export_ok && !-e "$projectroot/$project/$export_ok") || | |
501 | ($strict_export && !project_in_list($project))) { | |
502 | undef $project; | |
503 | return; | |
dd70235f | 504 | } |
645927ce ML |
505 | # do not change any parameters if an action is given using the query string |
506 | return if $action; | |
cd90e75f JN |
507 | $path_info =~ s,^$project/*,,; |
508 | my ($refname, $pathname) = split(/:/, $path_info, 2); | |
509 | if (defined $pathname) { | |
510 | # we got "project.git/branch:filename" or "project.git/branch:dir/" | |
511 | # we could use git_get_type(branch:pathname), but it needs $git_dir | |
512 | $pathname =~ s,^/+,,; | |
513 | if (!$pathname || substr($pathname, -1) eq "/") { | |
514 | $action ||= "tree"; | |
053d62bb | 515 | $pathname =~ s,/$,,; |
cd90e75f JN |
516 | } else { |
517 | $action ||= "blob_plain"; | |
518 | } | |
24d0693a JN |
519 | $hash_base ||= validate_refname($refname); |
520 | $file_name ||= validate_pathname($pathname); | |
cd90e75f | 521 | } elsif (defined $refname) { |
dd70235f MW |
522 | # we got "project.git/branch" |
523 | $action ||= "shortlog"; | |
24d0693a | 524 | $hash ||= validate_refname($refname); |
dd70235f MW |
525 | } |
526 | } | |
645927ce | 527 | evaluate_path_info(); |
dd70235f | 528 | |
645927ce ML |
529 | # path to the current git repository |
530 | our $git_dir; | |
531 | $git_dir = "$projectroot/$project" if $project; | |
dd70235f | 532 | |
717b8311 | 533 | # dispatch |
8e85cdc4 ML |
534 | my %actions = ( |
535 | "blame" => \&git_blame2, | |
536 | "blobdiff" => \&git_blobdiff, | |
537 | "blobdiff_plain" => \&git_blobdiff_plain, | |
538 | "blob" => \&git_blob, | |
539 | "blob_plain" => \&git_blob_plain, | |
540 | "commitdiff" => \&git_commitdiff, | |
541 | "commitdiff_plain" => \&git_commitdiff_plain, | |
542 | "commit" => \&git_commit, | |
e30496df | 543 | "forks" => \&git_forks, |
8e85cdc4 ML |
544 | "heads" => \&git_heads, |
545 | "history" => \&git_history, | |
546 | "log" => \&git_log, | |
547 | "rss" => \&git_rss, | |
af6feeb2 | 548 | "atom" => \&git_atom, |
8e85cdc4 | 549 | "search" => \&git_search, |
88ad729b | 550 | "search_help" => \&git_search_help, |
8e85cdc4 ML |
551 | "shortlog" => \&git_shortlog, |
552 | "summary" => \&git_summary, | |
553 | "tag" => \&git_tag, | |
554 | "tags" => \&git_tags, | |
555 | "tree" => \&git_tree, | |
cb9c6e5b | 556 | "snapshot" => \&git_snapshot, |
ca94601c | 557 | "object" => \&git_object, |
77a153fd JN |
558 | # those below don't need $project |
559 | "opml" => \&git_opml, | |
560 | "project_list" => \&git_project_list, | |
fc2b2be0 | 561 | "project_index" => \&git_project_index, |
8e85cdc4 ML |
562 | ); |
563 | ||
7f9778b1 GP |
564 | if (!defined $action) { |
565 | if (defined $hash) { | |
566 | $action = git_get_type($hash); | |
567 | } elsif (defined $hash_base && defined $file_name) { | |
568 | $action = git_get_type("$hash_base:$file_name"); | |
569 | } elsif (defined $project) { | |
570 | $action = 'summary'; | |
571 | } else { | |
572 | $action = 'project_list'; | |
573 | } | |
77a153fd | 574 | } |
8e85cdc4 | 575 | if (!defined($actions{$action})) { |
cac4bd94 | 576 | die_error(undef, "Unknown action"); |
09bd7898 | 577 | } |
d04d3d42 JN |
578 | if ($action !~ m/^(opml|project_list|project_index)$/ && |
579 | !$project) { | |
580 | die_error(undef, "Project needed"); | |
581 | } | |
8e85cdc4 ML |
582 | $actions{$action}->(); |
583 | exit; | |
09bd7898 | 584 | |
06a9d86b MW |
585 | ## ====================================================================== |
586 | ## action links | |
587 | ||
588 | sub href(%) { | |
498fe002 | 589 | my %params = @_; |
bd5d1e42 JN |
590 | # default is to use -absolute url() i.e. $my_uri |
591 | my $href = $params{-full} ? $my_url : $my_uri; | |
498fe002 | 592 | |
34c06118 PB |
593 | # XXX: Warning: If you touch this, check the search form for updating, |
594 | # too. | |
498fe002 JN |
595 | |
596 | my @mapping = ( | |
06a9d86b | 597 | project => "p", |
420e92f2 | 598 | action => "a", |
06a9d86b | 599 | file_name => "f", |
5c95fab0 | 600 | file_parent => "fp", |
06a9d86b MW |
601 | hash => "h", |
602 | hash_parent => "hp", | |
603 | hash_base => "hb", | |
420e92f2 | 604 | hash_parent_base => "hpb", |
06a9d86b | 605 | page => "pg", |
a1565c44 | 606 | order => "o", |
06a9d86b | 607 | searchtext => "s", |
88ad729b | 608 | searchtype => "st", |
a3c8ab30 | 609 | snapshot_format => "sf", |
12075103 | 610 | extra_options => "opt", |
06a9d86b | 611 | ); |
498fe002 | 612 | my %mapping = @mapping; |
06a9d86b | 613 | |
1cad283a JN |
614 | if ($params{-replay}) { |
615 | while (my ($name, $symbol) = each %mapping) { | |
616 | if (!exists $params{$name}) { | |
617 | # to allow for multivalued params we use arrayref form | |
618 | $params{$name} = [ $cgi->param($symbol) ]; | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
a1565c44 | 623 | $params{'project'} = $project unless exists $params{'project'}; |
06a9d86b | 624 | |
9e756904 MW |
625 | my ($use_pathinfo) = gitweb_check_feature('pathinfo'); |
626 | if ($use_pathinfo) { | |
627 | # use PATH_INFO for project name | |
628 | $href .= "/$params{'project'}" if defined $params{'project'}; | |
629 | delete $params{'project'}; | |
630 | ||
631 | # Summary just uses the project path URL | |
632 | if (defined $params{'action'} && $params{'action'} eq 'summary') { | |
633 | delete $params{'action'}; | |
634 | } | |
635 | } | |
636 | ||
637 | # now encode the parameters explicitly | |
498fe002 JN |
638 | my @result = (); |
639 | for (my $i = 0; $i < @mapping; $i += 2) { | |
640 | my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]); | |
641 | if (defined $params{$name}) { | |
f22cca44 JN |
642 | if (ref($params{$name}) eq "ARRAY") { |
643 | foreach my $par (@{$params{$name}}) { | |
644 | push @result, $symbol . "=" . esc_param($par); | |
645 | } | |
646 | } else { | |
647 | push @result, $symbol . "=" . esc_param($params{$name}); | |
648 | } | |
498fe002 JN |
649 | } |
650 | } | |
9e756904 MW |
651 | $href .= "?" . join(';', @result) if scalar @result; |
652 | ||
653 | return $href; | |
06a9d86b MW |
654 | } |
655 | ||
656 | ||
717b8311 JN |
657 | ## ====================================================================== |
658 | ## validation, quoting/unquoting and escaping | |
659 | ||
24d0693a JN |
660 | sub validate_pathname { |
661 | my $input = shift || return undef; | |
717b8311 | 662 | |
24d0693a JN |
663 | # no '.' or '..' as elements of path, i.e. no '.' nor '..' |
664 | # at the beginning, at the end, and between slashes. | |
665 | # also this catches doubled slashes | |
666 | if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) { | |
667 | return undef; | |
717b8311 | 668 | } |
24d0693a JN |
669 | # no null characters |
670 | if ($input =~ m!\0!) { | |
717b8311 JN |
671 | return undef; |
672 | } | |
24d0693a JN |
673 | return $input; |
674 | } | |
675 | ||
676 | sub validate_refname { | |
677 | my $input = shift || return undef; | |
678 | ||
679 | # textual hashes are O.K. | |
680 | if ($input =~ m/^[0-9a-fA-F]{40}$/) { | |
681 | return $input; | |
682 | } | |
683 | # it must be correct pathname | |
684 | $input = validate_pathname($input) | |
685 | or return undef; | |
686 | # restrictions on ref name according to git-check-ref-format | |
687 | if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { | |
717b8311 JN |
688 | return undef; |
689 | } | |
690 | return $input; | |
691 | } | |
692 | ||
00f429af MK |
693 | # decode sequences of octets in utf8 into Perl's internal form, |
694 | # which is utf-8 with utf8 flag set if needed. gitweb writes out | |
695 | # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning | |
696 | sub to_utf8 { | |
697 | my $str = shift; | |
e5d3de5c İD |
698 | if (utf8::valid($str)) { |
699 | utf8::decode($str); | |
700 | return $str; | |
00f429af MK |
701 | } else { |
702 | return decode($fallback_encoding, $str, Encode::FB_DEFAULT); | |
703 | } | |
704 | } | |
705 | ||
232ff553 KS |
706 | # quote unsafe chars, but keep the slash, even when it's not |
707 | # correct, but quoted slashes look too horrible in bookmarks | |
708 | sub esc_param { | |
353347b0 | 709 | my $str = shift; |
a2f3db2f | 710 | $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg; |
18216710 | 711 | $str =~ s/\+/%2B/g; |
a9e60b7d | 712 | $str =~ s/ /\+/g; |
353347b0 KS |
713 | return $str; |
714 | } | |
715 | ||
f93bff8d JN |
716 | # quote unsafe chars in whole URL, so some charactrs cannot be quoted |
717 | sub esc_url { | |
718 | my $str = shift; | |
719 | $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg; | |
720 | $str =~ s/\+/%2B/g; | |
721 | $str =~ s/ /\+/g; | |
722 | return $str; | |
723 | } | |
724 | ||
232ff553 | 725 | # replace invalid utf8 character with SUBSTITUTION sequence |
6255ef08 | 726 | sub esc_html ($;%) { |
40c13813 | 727 | my $str = shift; |
6255ef08 JN |
728 | my %opts = @_; |
729 | ||
00f429af | 730 | $str = to_utf8($str); |
c390ae97 | 731 | $str = $cgi->escapeHTML($str); |
6255ef08 JN |
732 | if ($opts{'-nbsp'}) { |
733 | $str =~ s/ / /g; | |
734 | } | |
25ffbb27 | 735 | $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg; |
40c13813 KS |
736 | return $str; |
737 | } | |
738 | ||
391862e3 JN |
739 | # quote control characters and escape filename to HTML |
740 | sub esc_path { | |
741 | my $str = shift; | |
742 | my %opts = @_; | |
743 | ||
00f429af | 744 | $str = to_utf8($str); |
c390ae97 | 745 | $str = $cgi->escapeHTML($str); |
391862e3 JN |
746 | if ($opts{'-nbsp'}) { |
747 | $str =~ s/ / /g; | |
748 | } | |
749 | $str =~ s|([[:cntrl:]])|quot_cec($1)|eg; | |
750 | return $str; | |
751 | } | |
752 | ||
753 | # Make control characters "printable", using character escape codes (CEC) | |
1d3bc0cc JN |
754 | sub quot_cec { |
755 | my $cntrl = shift; | |
756 | my %es = ( # character escape codes, aka escape sequences | |
757 | "\t" => '\t', # tab (HT) | |
758 | "\n" => '\n', # line feed (LF) | |
759 | "\r" => '\r', # carrige return (CR) | |
760 | "\f" => '\f', # form feed (FF) | |
761 | "\b" => '\b', # backspace (BS) | |
762 | "\a" => '\a', # alarm (bell) (BEL) | |
763 | "\e" => '\e', # escape (ESC) | |
764 | "\013" => '\v', # vertical tab (VT) | |
765 | "\000" => '\0', # nul character (NUL) | |
766 | ); | |
767 | my $chr = ( (exists $es{$cntrl}) | |
768 | ? $es{$cntrl} | |
769 | : sprintf('\%03o', ord($cntrl)) ); | |
770 | return "<span class=\"cntrl\">$chr</span>"; | |
771 | } | |
772 | ||
391862e3 JN |
773 | # Alternatively use unicode control pictures codepoints, |
774 | # Unicode "printable representation" (PR) | |
1d3bc0cc JN |
775 | sub quot_upr { |
776 | my $cntrl = shift; | |
777 | my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl)); | |
778 | return "<span class=\"cntrl\">$chr</span>"; | |
779 | } | |
780 | ||
232ff553 KS |
781 | # git may return quoted and escaped filenames |
782 | sub unquote { | |
783 | my $str = shift; | |
403d0906 JN |
784 | |
785 | sub unq { | |
786 | my $seq = shift; | |
787 | my %es = ( # character escape codes, aka escape sequences | |
788 | 't' => "\t", # tab (HT, TAB) | |
789 | 'n' => "\n", # newline (NL) | |
790 | 'r' => "\r", # return (CR) | |
791 | 'f' => "\f", # form feed (FF) | |
792 | 'b' => "\b", # backspace (BS) | |
793 | 'a' => "\a", # alarm (bell) (BEL) | |
794 | 'e' => "\e", # escape (ESC) | |
795 | 'v' => "\013", # vertical tab (VT) | |
796 | ); | |
797 | ||
798 | if ($seq =~ m/^[0-7]{1,3}$/) { | |
799 | # octal char sequence | |
800 | return chr(oct($seq)); | |
801 | } elsif (exists $es{$seq}) { | |
802 | # C escape sequence, aka character escape code | |
803 | return $es{$seq} | |
804 | } | |
805 | # quoted ordinary character | |
806 | return $seq; | |
807 | } | |
808 | ||
232ff553 | 809 | if ($str =~ m/^"(.*)"$/) { |
403d0906 | 810 | # needs unquoting |
232ff553 | 811 | $str = $1; |
403d0906 | 812 | $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg; |
232ff553 KS |
813 | } |
814 | return $str; | |
815 | } | |
816 | ||
f16db173 JN |
817 | # escape tabs (convert tabs to spaces) |
818 | sub untabify { | |
819 | my $line = shift; | |
820 | ||
821 | while ((my $pos = index($line, "\t")) != -1) { | |
822 | if (my $count = (8 - ($pos % 8))) { | |
823 | my $spaces = ' ' x $count; | |
824 | $line =~ s/\t/$spaces/; | |
825 | } | |
826 | } | |
827 | ||
828 | return $line; | |
829 | } | |
830 | ||
32f4aacc ML |
831 | sub project_in_list { |
832 | my $project = shift; | |
833 | my @list = git_get_projects_list(); | |
834 | return @list && scalar(grep { $_->{'path'} eq $project } @list); | |
835 | } | |
836 | ||
717b8311 JN |
837 | ## ---------------------------------------------------------------------- |
838 | ## HTML aware string manipulation | |
839 | ||
840 | sub chop_str { | |
841 | my $str = shift; | |
842 | my $len = shift; | |
843 | my $add_len = shift || 10; | |
844 | ||
845 | # allow only $len chars, but don't cut a word if it would fit in $add_len | |
846 | # if it doesn't fit, cut it if it's still longer than the dots we would add | |
847 | $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/; | |
848 | my $body = $1; | |
849 | my $tail = $2; | |
850 | if (length($tail) > 4) { | |
851 | $tail = " ..."; | |
852 | $body =~ s/&[^;]*$//; # remove chopped character entities | |
853 | } | |
854 | return "$body$tail"; | |
855 | } | |
856 | ||
ce58ec91 DS |
857 | # takes the same arguments as chop_str, but also wraps a <span> around the |
858 | # result with a title attribute if it does get chopped. Additionally, the | |
859 | # string is HTML-escaped. | |
860 | sub chop_and_escape_str { | |
861 | my $str = shift; | |
862 | my $len = shift; | |
863 | my $add_len = shift || 10; | |
864 | ||
865 | my $chopped = chop_str($str, $len, $add_len); | |
866 | if ($chopped eq $str) { | |
867 | return esc_html($chopped); | |
868 | } else { | |
869 | return qq{<span title="} . esc_html($str) . qq{">} . | |
870 | esc_html($chopped) . qq{</span>}; | |
871 | } | |
872 | } | |
873 | ||
717b8311 JN |
874 | ## ---------------------------------------------------------------------- |
875 | ## functions returning short strings | |
876 | ||
1f1ab5f0 JN |
877 | # CSS class for given age value (in seconds) |
878 | sub age_class { | |
879 | my $age = shift; | |
880 | ||
785cdea9 JN |
881 | if (!defined $age) { |
882 | return "noage"; | |
883 | } elsif ($age < 60*60*2) { | |
1f1ab5f0 JN |
884 | return "age0"; |
885 | } elsif ($age < 60*60*24*2) { | |
886 | return "age1"; | |
887 | } else { | |
888 | return "age2"; | |
889 | } | |
890 | } | |
891 | ||
717b8311 JN |
892 | # convert age in seconds to "nn units ago" string |
893 | sub age_string { | |
894 | my $age = shift; | |
895 | my $age_str; | |
a59d4afd | 896 | |
717b8311 JN |
897 | if ($age > 60*60*24*365*2) { |
898 | $age_str = (int $age/60/60/24/365); | |
899 | $age_str .= " years ago"; | |
900 | } elsif ($age > 60*60*24*(365/12)*2) { | |
901 | $age_str = int $age/60/60/24/(365/12); | |
902 | $age_str .= " months ago"; | |
903 | } elsif ($age > 60*60*24*7*2) { | |
904 | $age_str = int $age/60/60/24/7; | |
905 | $age_str .= " weeks ago"; | |
906 | } elsif ($age > 60*60*24*2) { | |
907 | $age_str = int $age/60/60/24; | |
908 | $age_str .= " days ago"; | |
909 | } elsif ($age > 60*60*2) { | |
910 | $age_str = int $age/60/60; | |
911 | $age_str .= " hours ago"; | |
912 | } elsif ($age > 60*2) { | |
913 | $age_str = int $age/60; | |
914 | $age_str .= " min ago"; | |
915 | } elsif ($age > 2) { | |
916 | $age_str = int $age; | |
917 | $age_str .= " sec ago"; | |
f6801d66 | 918 | } else { |
717b8311 | 919 | $age_str .= " right now"; |
4c02e3c5 | 920 | } |
717b8311 | 921 | return $age_str; |
161332a5 KS |
922 | } |
923 | ||
01ac1e38 JN |
924 | use constant { |
925 | S_IFINVALID => 0030000, | |
926 | S_IFGITLINK => 0160000, | |
927 | }; | |
928 | ||
929 | # submodule/subproject, a commit object reference | |
930 | sub S_ISGITLINK($) { | |
931 | my $mode = shift; | |
932 | ||
933 | return (($mode & S_IFMT) == S_IFGITLINK) | |
934 | } | |
935 | ||
717b8311 JN |
936 | # convert file mode in octal to symbolic file mode string |
937 | sub mode_str { | |
938 | my $mode = oct shift; | |
939 | ||
01ac1e38 JN |
940 | if (S_ISGITLINK($mode)) { |
941 | return 'm---------'; | |
942 | } elsif (S_ISDIR($mode & S_IFMT)) { | |
717b8311 JN |
943 | return 'drwxr-xr-x'; |
944 | } elsif (S_ISLNK($mode)) { | |
945 | return 'lrwxrwxrwx'; | |
946 | } elsif (S_ISREG($mode)) { | |
947 | # git cares only about the executable bit | |
948 | if ($mode & S_IXUSR) { | |
949 | return '-rwxr-xr-x'; | |
950 | } else { | |
951 | return '-rw-r--r--'; | |
952 | }; | |
c994d620 | 953 | } else { |
717b8311 | 954 | return '----------'; |
ff7669a5 | 955 | } |
161332a5 KS |
956 | } |
957 | ||
717b8311 JN |
958 | # convert file mode in octal to file type string |
959 | sub file_type { | |
7c5e2ebb JN |
960 | my $mode = shift; |
961 | ||
962 | if ($mode !~ m/^[0-7]+$/) { | |
963 | return $mode; | |
964 | } else { | |
965 | $mode = oct $mode; | |
966 | } | |
664f4cc5 | 967 | |
01ac1e38 JN |
968 | if (S_ISGITLINK($mode)) { |
969 | return "submodule"; | |
970 | } elsif (S_ISDIR($mode & S_IFMT)) { | |
717b8311 JN |
971 | return "directory"; |
972 | } elsif (S_ISLNK($mode)) { | |
973 | return "symlink"; | |
974 | } elsif (S_ISREG($mode)) { | |
975 | return "file"; | |
976 | } else { | |
977 | return "unknown"; | |
978 | } | |
a59d4afd KS |
979 | } |
980 | ||
744d0ac3 JN |
981 | # convert file mode in octal to file type description string |
982 | sub file_type_long { | |
983 | my $mode = shift; | |
984 | ||
985 | if ($mode !~ m/^[0-7]+$/) { | |
986 | return $mode; | |
987 | } else { | |
988 | $mode = oct $mode; | |
989 | } | |
990 | ||
01ac1e38 JN |
991 | if (S_ISGITLINK($mode)) { |
992 | return "submodule"; | |
993 | } elsif (S_ISDIR($mode & S_IFMT)) { | |
744d0ac3 JN |
994 | return "directory"; |
995 | } elsif (S_ISLNK($mode)) { | |
996 | return "symlink"; | |
997 | } elsif (S_ISREG($mode)) { | |
998 | if ($mode & S_IXUSR) { | |
999 | return "executable"; | |
1000 | } else { | |
1001 | return "file"; | |
1002 | }; | |
1003 | } else { | |
1004 | return "unknown"; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | ||
717b8311 JN |
1009 | ## ---------------------------------------------------------------------- |
1010 | ## functions returning short HTML fragments, or transforming HTML fragments | |
3dff5379 | 1011 | ## which don't belong to other sections |
b18f9bf4 | 1012 | |
225932ed | 1013 | # format line of commit message. |
717b8311 JN |
1014 | sub format_log_line_html { |
1015 | my $line = shift; | |
b18f9bf4 | 1016 | |
225932ed | 1017 | $line = esc_html($line, -nbsp=>1); |
bfe2191f | 1018 | if ($line =~ m/([0-9a-fA-F]{8,40})/) { |
717b8311 | 1019 | my $hash_text = $1; |
bfe2191f JN |
1020 | my $link = |
1021 | $cgi->a({-href => href(action=>"object", hash=>$hash_text), | |
1022 | -class => "text"}, $hash_text); | |
1023 | $line =~ s/$hash_text/$link/; | |
b18f9bf4 | 1024 | } |
717b8311 | 1025 | return $line; |
b18f9bf4 JN |
1026 | } |
1027 | ||
717b8311 | 1028 | # format marker of refs pointing to given object |
847e01fb | 1029 | sub format_ref_marker { |
717b8311 | 1030 | my ($refs, $id) = @_; |
d294e1ca | 1031 | my $markers = ''; |
27fb8c40 | 1032 | |
717b8311 | 1033 | if (defined $refs->{$id}) { |
d294e1ca JN |
1034 | foreach my $ref (@{$refs->{$id}}) { |
1035 | my ($type, $name) = qw(); | |
1036 | # e.g. tags/v2.6.11 or heads/next | |
1037 | if ($ref =~ m!^(.*?)s?/(.*)$!) { | |
1038 | $type = $1; | |
1039 | $name = $2; | |
1040 | } else { | |
1041 | $type = "ref"; | |
1042 | $name = $ref; | |
1043 | } | |
1044 | ||
5fce278e JN |
1045 | $markers .= " <span class=\"$type\" title=\"$ref\">" . |
1046 | esc_html($name) . "</span>"; | |
d294e1ca JN |
1047 | } |
1048 | } | |
1049 | ||
1050 | if ($markers) { | |
1051 | return ' <span class="refs">'. $markers . '</span>'; | |
717b8311 JN |
1052 | } else { |
1053 | return ""; | |
1054 | } | |
27fb8c40 JN |
1055 | } |
1056 | ||
17d07443 JN |
1057 | # format, perhaps shortened and with markers, title line |
1058 | sub format_subject_html { | |
1c2a4f5a | 1059 | my ($long, $short, $href, $extra) = @_; |
17d07443 JN |
1060 | $extra = '' unless defined($extra); |
1061 | ||
1062 | if (length($short) < length($long)) { | |
7c278014 | 1063 | return $cgi->a({-href => $href, -class => "list subject", |
00f429af | 1064 | -title => to_utf8($long)}, |
17d07443 JN |
1065 | esc_html($short) . $extra); |
1066 | } else { | |
7c278014 | 1067 | return $cgi->a({-href => $href, -class => "list subject"}, |
17d07443 JN |
1068 | esc_html($long) . $extra); |
1069 | } | |
1070 | } | |
1071 | ||
90921740 JN |
1072 | # format git diff header line, i.e. "diff --(git|combined|cc) ..." |
1073 | sub format_git_diff_header_line { | |
1074 | my $line = shift; | |
1075 | my $diffinfo = shift; | |
1076 | my ($from, $to) = @_; | |
1077 | ||
1078 | if ($diffinfo->{'nparents'}) { | |
1079 | # combined diff | |
1080 | $line =~ s!^(diff (.*?) )"?.*$!$1!; | |
1081 | if ($to->{'href'}) { | |
1082 | $line .= $cgi->a({-href => $to->{'href'}, -class => "path"}, | |
1083 | esc_path($to->{'file'})); | |
1084 | } else { # file was deleted (no href) | |
1085 | $line .= esc_path($to->{'file'}); | |
1086 | } | |
1087 | } else { | |
1088 | # "ordinary" diff | |
1089 | $line =~ s!^(diff (.*?) )"?a/.*$!$1!; | |
1090 | if ($from->{'href'}) { | |
1091 | $line .= $cgi->a({-href => $from->{'href'}, -class => "path"}, | |
1092 | 'a/' . esc_path($from->{'file'})); | |
1093 | } else { # file was added (no href) | |
1094 | $line .= 'a/' . esc_path($from->{'file'}); | |
1095 | } | |
1096 | $line .= ' '; | |
1097 | if ($to->{'href'}) { | |
1098 | $line .= $cgi->a({-href => $to->{'href'}, -class => "path"}, | |
1099 | 'b/' . esc_path($to->{'file'})); | |
1100 | } else { # file was deleted | |
1101 | $line .= 'b/' . esc_path($to->{'file'}); | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | return "<div class=\"diff header\">$line</div>\n"; | |
1106 | } | |
1107 | ||
1108 | # format extended diff header line, before patch itself | |
1109 | sub format_extended_diff_header_line { | |
1110 | my $line = shift; | |
1111 | my $diffinfo = shift; | |
1112 | my ($from, $to) = @_; | |
1113 | ||
1114 | # match <path> | |
1115 | if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) { | |
1116 | $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"}, | |
1117 | esc_path($from->{'file'})); | |
1118 | } | |
1119 | if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) { | |
1120 | $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"}, | |
1121 | esc_path($to->{'file'})); | |
1122 | } | |
1123 | # match single <mode> | |
1124 | if ($line =~ m/\s(\d{6})$/) { | |
1125 | $line .= '<span class="info"> (' . | |
1126 | file_type_long($1) . | |
1127 | ')</span>'; | |
1128 | } | |
1129 | # match <hash> | |
1130 | if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) { | |
1131 | # can match only for combined diff | |
1132 | $line = 'index '; | |
1133 | for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) { | |
1134 | if ($from->{'href'}[$i]) { | |
1135 | $line .= $cgi->a({-href=>$from->{'href'}[$i], | |
1136 | -class=>"hash"}, | |
1137 | substr($diffinfo->{'from_id'}[$i],0,7)); | |
1138 | } else { | |
1139 | $line .= '0' x 7; | |
1140 | } | |
1141 | # separator | |
1142 | $line .= ',' if ($i < $diffinfo->{'nparents'} - 1); | |
1143 | } | |
1144 | $line .= '..'; | |
1145 | if ($to->{'href'}) { | |
1146 | $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"}, | |
1147 | substr($diffinfo->{'to_id'},0,7)); | |
1148 | } else { | |
1149 | $line .= '0' x 7; | |
1150 | } | |
1151 | ||
1152 | } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) { | |
1153 | # can match only for ordinary diff | |
1154 | my ($from_link, $to_link); | |
1155 | if ($from->{'href'}) { | |
1156 | $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"}, | |
1157 | substr($diffinfo->{'from_id'},0,7)); | |
1158 | } else { | |
1159 | $from_link = '0' x 7; | |
1160 | } | |
1161 | if ($to->{'href'}) { | |
1162 | $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"}, | |
1163 | substr($diffinfo->{'to_id'},0,7)); | |
1164 | } else { | |
1165 | $to_link = '0' x 7; | |
1166 | } | |
1167 | my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'}); | |
1168 | $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!; | |
1169 | } | |
1170 | ||
1171 | return $line . "<br/>\n"; | |
1172 | } | |
1173 | ||
1174 | # format from-file/to-file diff header | |
1175 | sub format_diff_from_to_header { | |
91af4ce4 | 1176 | my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_; |
90921740 JN |
1177 | my $line; |
1178 | my $result = ''; | |
1179 | ||
1180 | $line = $from_line; | |
1181 | #assert($line =~ m/^---/) if DEBUG; | |
deaa01a9 JN |
1182 | # no extra formatting for "^--- /dev/null" |
1183 | if (! $diffinfo->{'nparents'}) { | |
1184 | # ordinary (single parent) diff | |
1185 | if ($line =~ m!^--- "?a/!) { | |
1186 | if ($from->{'href'}) { | |
1187 | $line = '--- a/' . | |
1188 | $cgi->a({-href=>$from->{'href'}, -class=>"path"}, | |
1189 | esc_path($from->{'file'})); | |
1190 | } else { | |
1191 | $line = '--- a/' . | |
1192 | esc_path($from->{'file'}); | |
1193 | } | |
1194 | } | |
1195 | $result .= qq!<div class="diff from_file">$line</div>\n!; | |
1196 | ||
1197 | } else { | |
1198 | # combined diff (merge commit) | |
1199 | for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) { | |
1200 | if ($from->{'href'}[$i]) { | |
1201 | $line = '--- ' . | |
91af4ce4 JN |
1202 | $cgi->a({-href=>href(action=>"blobdiff", |
1203 | hash_parent=>$diffinfo->{'from_id'}[$i], | |
1204 | hash_parent_base=>$parents[$i], | |
1205 | file_parent=>$from->{'file'}[$i], | |
1206 | hash=>$diffinfo->{'to_id'}, | |
1207 | hash_base=>$hash, | |
1208 | file_name=>$to->{'file'}), | |
1209 | -class=>"path", | |
1210 | -title=>"diff" . ($i+1)}, | |
1211 | $i+1) . | |
1212 | '/' . | |
deaa01a9 JN |
1213 | $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"}, |
1214 | esc_path($from->{'file'}[$i])); | |
1215 | } else { | |
1216 | $line = '--- /dev/null'; | |
1217 | } | |
1218 | $result .= qq!<div class="diff from_file">$line</div>\n!; | |
90921740 JN |
1219 | } |
1220 | } | |
90921740 JN |
1221 | |
1222 | $line = $to_line; | |
1223 | #assert($line =~ m/^\+\+\+/) if DEBUG; | |
1224 | # no extra formatting for "^+++ /dev/null" | |
1225 | if ($line =~ m!^\+\+\+ "?b/!) { | |
1226 | if ($to->{'href'}) { | |
1227 | $line = '+++ b/' . | |
1228 | $cgi->a({-href=>$to->{'href'}, -class=>"path"}, | |
1229 | esc_path($to->{'file'})); | |
1230 | } else { | |
1231 | $line = '+++ b/' . | |
1232 | esc_path($to->{'file'}); | |
1233 | } | |
1234 | } | |
1235 | $result .= qq!<div class="diff to_file">$line</div>\n!; | |
1236 | ||
1237 | return $result; | |
1238 | } | |
1239 | ||
cd030c3a JN |
1240 | # create note for patch simplified by combined diff |
1241 | sub format_diff_cc_simplified { | |
1242 | my ($diffinfo, @parents) = @_; | |
1243 | my $result = ''; | |
1244 | ||
1245 | $result .= "<div class=\"diff header\">" . | |
1246 | "diff --cc "; | |
1247 | if (!is_deleted($diffinfo)) { | |
1248 | $result .= $cgi->a({-href => href(action=>"blob", | |
1249 | hash_base=>$hash, | |
1250 | hash=>$diffinfo->{'to_id'}, | |
1251 | file_name=>$diffinfo->{'to_file'}), | |
1252 | -class => "path"}, | |
1253 | esc_path($diffinfo->{'to_file'})); | |
1254 | } else { | |
1255 | $result .= esc_path($diffinfo->{'to_file'}); | |
1256 | } | |
1257 | $result .= "</div>\n" . # class="diff header" | |
1258 | "<div class=\"diff nodifferences\">" . | |
1259 | "Simple merge" . | |
1260 | "</div>\n"; # class="diff nodifferences" | |
1261 | ||
1262 | return $result; | |
1263 | } | |
1264 | ||
90921740 | 1265 | # format patch (diff) line (not to be used for diff headers) |
eee08903 JN |
1266 | sub format_diff_line { |
1267 | my $line = shift; | |
59e3b14e | 1268 | my ($from, $to) = @_; |
eee08903 JN |
1269 | my $diff_class = ""; |
1270 | ||
1271 | chomp $line; | |
1272 | ||
e72c0eaf JN |
1273 | if ($from && $to && ref($from->{'href'}) eq "ARRAY") { |
1274 | # combined diff | |
1275 | my $prefix = substr($line, 0, scalar @{$from->{'href'}}); | |
1276 | if ($line =~ m/^\@{3}/) { | |
1277 | $diff_class = " chunk_header"; | |
1278 | } elsif ($line =~ m/^\\/) { | |
1279 | $diff_class = " incomplete"; | |
1280 | } elsif ($prefix =~ tr/+/+/) { | |
1281 | $diff_class = " add"; | |
1282 | } elsif ($prefix =~ tr/-/-/) { | |
1283 | $diff_class = " rem"; | |
1284 | } | |
1285 | } else { | |
1286 | # assume ordinary diff | |
1287 | my $char = substr($line, 0, 1); | |
1288 | if ($char eq '+') { | |
1289 | $diff_class = " add"; | |
1290 | } elsif ($char eq '-') { | |
1291 | $diff_class = " rem"; | |
1292 | } elsif ($char eq '@') { | |
1293 | $diff_class = " chunk_header"; | |
1294 | } elsif ($char eq "\\") { | |
1295 | $diff_class = " incomplete"; | |
1296 | } | |
eee08903 JN |
1297 | } |
1298 | $line = untabify($line); | |
59e3b14e JN |
1299 | if ($from && $to && $line =~ m/^\@{2} /) { |
1300 | my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) = | |
1301 | $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/; | |
1302 | ||
1303 | $from_lines = 0 unless defined $from_lines; | |
1304 | $to_lines = 0 unless defined $to_lines; | |
1305 | ||
1306 | if ($from->{'href'}) { | |
1307 | $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start", | |
1308 | -class=>"list"}, $from_text); | |
1309 | } | |
1310 | if ($to->{'href'}) { | |
1311 | $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start", | |
1312 | -class=>"list"}, $to_text); | |
1313 | } | |
1314 | $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" . | |
1315 | "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>"; | |
1316 | return "<div class=\"diff$diff_class\">$line</div>\n"; | |
e72c0eaf JN |
1317 | } elsif ($from && $to && $line =~ m/^\@{3}/) { |
1318 | my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/; | |
1319 | my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines); | |
1320 | ||
1321 | @from_text = split(' ', $ranges); | |
1322 | for (my $i = 0; $i < @from_text; ++$i) { | |
1323 | ($from_start[$i], $from_nlines[$i]) = | |
1324 | (split(',', substr($from_text[$i], 1)), 0); | |
1325 | } | |
1326 | ||
1327 | $to_text = pop @from_text; | |
1328 | $to_start = pop @from_start; | |
1329 | $to_nlines = pop @from_nlines; | |
1330 | ||
1331 | $line = "<span class=\"chunk_info\">$prefix "; | |
1332 | for (my $i = 0; $i < @from_text; ++$i) { | |
1333 | if ($from->{'href'}[$i]) { | |
1334 | $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]", | |
1335 | -class=>"list"}, $from_text[$i]); | |
1336 | } else { | |
1337 | $line .= $from_text[$i]; | |
1338 | } | |
1339 | $line .= " "; | |
1340 | } | |
1341 | if ($to->{'href'}) { | |
1342 | $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start", | |
1343 | -class=>"list"}, $to_text); | |
1344 | } else { | |
1345 | $line .= $to_text; | |
1346 | } | |
1347 | $line .= " $prefix</span>" . | |
1348 | "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>"; | |
1349 | return "<div class=\"diff$diff_class\">$line</div>\n"; | |
59e3b14e | 1350 | } |
6255ef08 | 1351 | return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n"; |
eee08903 JN |
1352 | } |
1353 | ||
a3c8ab30 MM |
1354 | # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)", |
1355 | # linked. Pass the hash of the tree/commit to snapshot. | |
1356 | sub format_snapshot_links { | |
1357 | my ($hash) = @_; | |
1358 | my @snapshot_fmts = gitweb_check_feature('snapshot'); | |
a781785d | 1359 | @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts); |
a3c8ab30 MM |
1360 | my $num_fmts = @snapshot_fmts; |
1361 | if ($num_fmts > 1) { | |
1362 | # A parenthesized list of links bearing format names. | |
a781785d | 1363 | # e.g. "snapshot (_tar.gz_ _zip_)" |
a3c8ab30 MM |
1364 | return "snapshot (" . join(' ', map |
1365 | $cgi->a({ | |
1366 | -href => href( | |
1367 | action=>"snapshot", | |
1368 | hash=>$hash, | |
1369 | snapshot_format=>$_ | |
1370 | ) | |
1371 | }, $known_snapshot_formats{$_}{'display'}) | |
1372 | , @snapshot_fmts) . ")"; | |
1373 | } elsif ($num_fmts == 1) { | |
1374 | # A single "snapshot" link whose tooltip bears the format name. | |
a781785d | 1375 | # i.e. "_snapshot_" |
a3c8ab30 | 1376 | my ($fmt) = @snapshot_fmts; |
a781785d JN |
1377 | return |
1378 | $cgi->a({ | |
a3c8ab30 MM |
1379 | -href => href( |
1380 | action=>"snapshot", | |
1381 | hash=>$hash, | |
1382 | snapshot_format=>$fmt | |
1383 | ), | |
1384 | -title => "in format: $known_snapshot_formats{$fmt}{'display'}" | |
1385 | }, "snapshot"); | |
1386 | } else { # $num_fmts == 0 | |
1387 | return undef; | |
1388 | } | |
1389 | } | |
1390 | ||
717b8311 JN |
1391 | ## ---------------------------------------------------------------------- |
1392 | ## git utility subroutines, invoking git commands | |
42f7eb94 | 1393 | |
25691fbe DS |
1394 | # returns path to the core git executable and the --git-dir parameter as list |
1395 | sub git_cmd { | |
1396 | return $GIT, '--git-dir='.$git_dir; | |
1397 | } | |
1398 | ||
1399 | # returns path to the core git executable and the --git-dir parameter as string | |
1400 | sub git_cmd_str { | |
1401 | return join(' ', git_cmd()); | |
1402 | } | |
1403 | ||
717b8311 | 1404 | # get HEAD ref of given project as hash |
847e01fb | 1405 | sub git_get_head_hash { |
df2c37a5 | 1406 | my $project = shift; |
25691fbe | 1407 | my $o_git_dir = $git_dir; |
df2c37a5 | 1408 | my $retval = undef; |
25691fbe DS |
1409 | $git_dir = "$projectroot/$project"; |
1410 | if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") { | |
df2c37a5 JH |
1411 | my $head = <$fd>; |
1412 | close $fd; | |
2c5c008b KS |
1413 | if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) { |
1414 | $retval = $1; | |
df2c37a5 JH |
1415 | } |
1416 | } | |
25691fbe DS |
1417 | if (defined $o_git_dir) { |
1418 | $git_dir = $o_git_dir; | |
2c5c008b | 1419 | } |
df2c37a5 JH |
1420 | return $retval; |
1421 | } | |
1422 | ||
717b8311 JN |
1423 | # get type of given object |
1424 | sub git_get_type { | |
1425 | my $hash = shift; | |
1426 | ||
25691fbe | 1427 | open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return; |
717b8311 JN |
1428 | my $type = <$fd>; |
1429 | close $fd or return; | |
1430 | chomp $type; | |
1431 | return $type; | |
1432 | } | |
1433 | ||
b201927a JN |
1434 | # repository configuration |
1435 | our $config_file = ''; | |
1436 | our %config; | |
1437 | ||
1438 | # store multiple values for single key as anonymous array reference | |
1439 | # single values stored directly in the hash, not as [ <value> ] | |
1440 | sub hash_set_multi { | |
1441 | my ($hash, $key, $value) = @_; | |
1442 | ||
1443 | if (!exists $hash->{$key}) { | |
1444 | $hash->{$key} = $value; | |
1445 | } elsif (!ref $hash->{$key}) { | |
1446 | $hash->{$key} = [ $hash->{$key}, $value ]; | |
1447 | } else { | |
1448 | push @{$hash->{$key}}, $value; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | # return hash of git project configuration | |
1453 | # optionally limited to some section, e.g. 'gitweb' | |
1454 | sub git_parse_project_config { | |
1455 | my $section_regexp = shift; | |
1456 | my %config; | |
1457 | ||
1458 | local $/ = "\0"; | |
1459 | ||
1460 | open my $fh, "-|", git_cmd(), "config", '-z', '-l', | |
1461 | or return; | |
1462 | ||
1463 | while (my $keyval = <$fh>) { | |
1464 | chomp $keyval; | |
1465 | my ($key, $value) = split(/\n/, $keyval, 2); | |
1466 | ||
1467 | hash_set_multi(\%config, $key, $value) | |
1468 | if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o); | |
1469 | } | |
1470 | close $fh; | |
1471 | ||
1472 | return %config; | |
1473 | } | |
1474 | ||
1475 | # convert config value to boolean, 'true' or 'false' | |
1476 | # no value, number > 0, 'true' and 'yes' values are true | |
1477 | # rest of values are treated as false (never as error) | |
1478 | sub config_to_bool { | |
1479 | my $val = shift; | |
1480 | ||
1481 | # strip leading and trailing whitespace | |
1482 | $val =~ s/^\s+//; | |
1483 | $val =~ s/\s+$//; | |
1484 | ||
1485 | return (!defined $val || # section.key | |
1486 | ($val =~ /^\d+$/ && $val) || # section.key = 1 | |
1487 | ($val =~ /^(?:true|yes)$/i)); # section.key = true | |
1488 | } | |
1489 | ||
1490 | # convert config value to simple decimal number | |
1491 | # an optional value suffix of 'k', 'm', or 'g' will cause the value | |
1492 | # to be multiplied by 1024, 1048576, or 1073741824 | |
1493 | sub config_to_int { | |
1494 | my $val = shift; | |
1495 | ||
1496 | # strip leading and trailing whitespace | |
1497 | $val =~ s/^\s+//; | |
1498 | $val =~ s/\s+$//; | |
1499 | ||
1500 | if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) { | |
1501 | $unit = lc($unit); | |
1502 | # unknown unit is treated as 1 | |
1503 | return $num * ($unit eq 'g' ? 1073741824 : | |
1504 | $unit eq 'm' ? 1048576 : | |
1505 | $unit eq 'k' ? 1024 : 1); | |
1506 | } | |
1507 | return $val; | |
1508 | } | |
1509 | ||
1510 | # convert config value to array reference, if needed | |
1511 | sub config_to_multi { | |
1512 | my $val = shift; | |
1513 | ||
1514 | return ref($val) ? $val : [ $val ]; | |
1515 | } | |
1516 | ||
717b8311 | 1517 | sub git_get_project_config { |
ddb8d900 | 1518 | my ($key, $type) = @_; |
717b8311 | 1519 | |
b201927a | 1520 | # key sanity check |
717b8311 JN |
1521 | return unless ($key); |
1522 | $key =~ s/^gitweb\.//; | |
1523 | return if ($key =~ m/\W/); | |
1524 | ||
b201927a JN |
1525 | # type sanity check |
1526 | if (defined $type) { | |
1527 | $type =~ s/^--//; | |
1528 | $type = undef | |
1529 | unless ($type eq 'bool' || $type eq 'int'); | |
1530 | } | |
1531 | ||
1532 | # get config | |
1533 | if (!defined $config_file || | |
1534 | $config_file ne "$git_dir/config") { | |
1535 | %config = git_parse_project_config('gitweb'); | |
1536 | $config_file = "$git_dir/config"; | |
1537 | } | |
1538 | ||
1539 | # ensure given type | |
1540 | if (!defined $type) { | |
1541 | return $config{"gitweb.$key"}; | |
1542 | } elsif ($type eq 'bool') { | |
1543 | # backward compatibility: 'git config --bool' returns true/false | |
1544 | return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false'; | |
1545 | } elsif ($type eq 'int') { | |
1546 | return config_to_int($config{"gitweb.$key"}); | |
1547 | } | |
1548 | return $config{"gitweb.$key"}; | |
717b8311 JN |
1549 | } |
1550 | ||
717b8311 JN |
1551 | # get hash of given path at given ref |
1552 | sub git_get_hash_by_path { | |
1553 | my $base = shift; | |
1554 | my $path = shift || return undef; | |
1d782b03 | 1555 | my $type = shift; |
717b8311 | 1556 | |
4b02f483 | 1557 | $path =~ s,/+$,,; |
717b8311 | 1558 | |
25691fbe | 1559 | open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path |
cac4bd94 | 1560 | or die_error(undef, "Open git-ls-tree failed"); |
717b8311 JN |
1561 | my $line = <$fd>; |
1562 | close $fd or return undef; | |
1563 | ||
198a2a8a JN |
1564 | if (!defined $line) { |
1565 | # there is no tree or hash given by $path at $base | |
1566 | return undef; | |
1567 | } | |
1568 | ||
717b8311 | 1569 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' |
8b4b94cc | 1570 | $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/; |
1d782b03 JN |
1571 | if (defined $type && $type ne $2) { |
1572 | # type doesn't match | |
1573 | return undef; | |
1574 | } | |
717b8311 JN |
1575 | return $3; |
1576 | } | |
1577 | ||
ed224dea JN |
1578 | # get path of entry with given hash at given tree-ish (ref) |
1579 | # used to get 'from' filename for combined diff (merge commit) for renames | |
1580 | sub git_get_path_by_hash { | |
1581 | my $base = shift || return; | |
1582 | my $hash = shift || return; | |
1583 | ||
1584 | local $/ = "\0"; | |
1585 | ||
1586 | open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base | |
1587 | or return undef; | |
1588 | while (my $line = <$fd>) { | |
1589 | chomp $line; | |
1590 | ||
1591 | #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb' | |
1592 | #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README' | |
1593 | if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) { | |
1594 | close $fd; | |
1595 | return $1; | |
1596 | } | |
1597 | } | |
1598 | close $fd; | |
1599 | return undef; | |
1600 | } | |
1601 | ||
717b8311 JN |
1602 | ## ...................................................................... |
1603 | ## git utility functions, directly accessing git repository | |
1604 | ||
847e01fb | 1605 | sub git_get_project_description { |
b87d78d6 | 1606 | my $path = shift; |
09bd7898 | 1607 | |
0e121a2c JN |
1608 | $git_dir = "$projectroot/$path"; |
1609 | open my $fd, "$projectroot/$path/description" | |
1610 | or return git_get_project_config('description'); | |
b87d78d6 KS |
1611 | my $descr = <$fd>; |
1612 | close $fd; | |
2eb54efc JH |
1613 | if (defined $descr) { |
1614 | chomp $descr; | |
1615 | } | |
b87d78d6 | 1616 | return $descr; |
12a88f2f KS |
1617 | } |
1618 | ||
e79ca7cc JN |
1619 | sub git_get_project_url_list { |
1620 | my $path = shift; | |
1621 | ||
0e121a2c JN |
1622 | $git_dir = "$projectroot/$path"; |
1623 | open my $fd, "$projectroot/$path/cloneurl" | |
1624 | or return wantarray ? | |
1625 | @{ config_to_multi(git_get_project_config('url')) } : | |
1626 | config_to_multi(git_get_project_config('url')); | |
e79ca7cc JN |
1627 | my @git_project_url_list = map { chomp; $_ } <$fd>; |
1628 | close $fd; | |
1629 | ||
1630 | return wantarray ? @git_project_url_list : \@git_project_url_list; | |
1631 | } | |
1632 | ||
847e01fb | 1633 | sub git_get_projects_list { |
e30496df | 1634 | my ($filter) = @_; |
717b8311 JN |
1635 | my @list; |
1636 | ||
e30496df PB |
1637 | $filter ||= ''; |
1638 | $filter =~ s/\.git$//; | |
1639 | ||
c2b8b134 FL |
1640 | my ($check_forks) = gitweb_check_feature('forks'); |
1641 | ||
717b8311 JN |
1642 | if (-d $projects_list) { |
1643 | # search in directory | |
e30496df | 1644 | my $dir = $projects_list . ($filter ? "/$filter" : ''); |
6768d6b8 AK |
1645 | # remove the trailing "/" |
1646 | $dir =~ s!/+$!!; | |
c0011ff8 | 1647 | my $pfxlen = length("$dir"); |
ca5e9495 | 1648 | my $pfxdepth = ($dir =~ tr!/!!); |
c0011ff8 JN |
1649 | |
1650 | File::Find::find({ | |
1651 | follow_fast => 1, # follow symbolic links | |
d20602ee | 1652 | follow_skip => 2, # ignore duplicates |
c0011ff8 JN |
1653 | dangling_symlinks => 0, # ignore dangling symlinks, silently |
1654 | wanted => sub { | |
1655 | # skip project-list toplevel, if we get it. | |
1656 | return if (m!^[/.]$!); | |
1657 | # only directories can be git repositories | |
1658 | return unless (-d $_); | |
ca5e9495 LL |
1659 | # don't traverse too deep (Find is super slow on os x) |
1660 | if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) { | |
1661 | $File::Find::prune = 1; | |
1662 | return; | |
1663 | } | |
c0011ff8 JN |
1664 | |
1665 | my $subdir = substr($File::Find::name, $pfxlen + 1); | |
1666 | # we check related file in $projectroot | |
e30496df PB |
1667 | if ($check_forks and $subdir =~ m#/.#) { |
1668 | $File::Find::prune = 1; | |
1669 | } elsif (check_export_ok("$projectroot/$filter/$subdir")) { | |
1670 | push @list, { path => ($filter ? "$filter/" : '') . $subdir }; | |
c0011ff8 JN |
1671 | $File::Find::prune = 1; |
1672 | } | |
1673 | }, | |
1674 | }, "$dir"); | |
1675 | ||
717b8311 JN |
1676 | } elsif (-f $projects_list) { |
1677 | # read from file(url-encoded): | |
1678 | # 'git%2Fgit.git Linus+Torvalds' | |
1679 | # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin' | |
1680 | # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman' | |
c2b8b134 | 1681 | my %paths; |
dd1ad5f1 | 1682 | open my ($fd), $projects_list or return; |
c2b8b134 | 1683 | PROJECT: |
717b8311 JN |
1684 | while (my $line = <$fd>) { |
1685 | chomp $line; | |
1686 | my ($path, $owner) = split ' ', $line; | |
1687 | $path = unescape($path); | |
1688 | $owner = unescape($owner); | |
1689 | if (!defined $path) { | |
1690 | next; | |
1691 | } | |
83ee94c1 JH |
1692 | if ($filter ne '') { |
1693 | # looking for forks; | |
1694 | my $pfx = substr($path, 0, length($filter)); | |
1695 | if ($pfx ne $filter) { | |
c2b8b134 | 1696 | next PROJECT; |
83ee94c1 JH |
1697 | } |
1698 | my $sfx = substr($path, length($filter)); | |
1699 | if ($sfx !~ /^\/.*\.git$/) { | |
c2b8b134 FL |
1700 | next PROJECT; |
1701 | } | |
1702 | } elsif ($check_forks) { | |
1703 | PATH: | |
1704 | foreach my $filter (keys %paths) { | |
1705 | # looking for forks; | |
1706 | my $pfx = substr($path, 0, length($filter)); | |
1707 | if ($pfx ne $filter) { | |
1708 | next PATH; | |
1709 | } | |
1710 | my $sfx = substr($path, length($filter)); | |
1711 | if ($sfx !~ /^\/.*\.git$/) { | |
1712 | next PATH; | |
1713 | } | |
1714 | # is a fork, don't include it in | |
1715 | # the list | |
1716 | next PROJECT; | |
83ee94c1 JH |
1717 | } |
1718 | } | |
2172ce4b | 1719 | if (check_export_ok("$projectroot/$path")) { |
717b8311 JN |
1720 | my $pr = { |
1721 | path => $path, | |
00f429af | 1722 | owner => to_utf8($owner), |
717b8311 | 1723 | }; |
c2b8b134 FL |
1724 | push @list, $pr; |
1725 | (my $forks_path = $path) =~ s/\.git$//; | |
1726 | $paths{$forks_path}++; | |
717b8311 JN |
1727 | } |
1728 | } | |
1729 | close $fd; | |
1730 | } | |
717b8311 JN |
1731 | return @list; |
1732 | } | |
1733 | ||
47852450 JH |
1734 | our $gitweb_project_owner = undef; |
1735 | sub git_get_project_list_from_file { | |
1e0cf030 | 1736 | |
47852450 | 1737 | return if (defined $gitweb_project_owner); |
1e0cf030 | 1738 | |
47852450 | 1739 | $gitweb_project_owner = {}; |
1e0cf030 JN |
1740 | # read from file (url-encoded): |
1741 | # 'git%2Fgit.git Linus+Torvalds' | |
1742 | # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin' | |
1743 | # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman' | |
1744 | if (-f $projects_list) { | |
1745 | open (my $fd , $projects_list); | |
1746 | while (my $line = <$fd>) { | |
1747 | chomp $line; | |
1748 | my ($pr, $ow) = split ' ', $line; | |
1749 | $pr = unescape($pr); | |
1750 | $ow = unescape($ow); | |
47852450 | 1751 | $gitweb_project_owner->{$pr} = to_utf8($ow); |
1e0cf030 JN |
1752 | } |
1753 | close $fd; | |
1754 | } | |
47852450 JH |
1755 | } |
1756 | ||
1757 | sub git_get_project_owner { | |
1758 | my $project = shift; | |
1759 | my $owner; | |
1760 | ||
1761 | return undef unless $project; | |
1762 | ||
1763 | if (!defined $gitweb_project_owner) { | |
1764 | git_get_project_list_from_file(); | |
1765 | } | |
1766 | ||
1767 | if (exists $gitweb_project_owner->{$project}) { | |
1768 | $owner = $gitweb_project_owner->{$project}; | |
1769 | } | |
1e0cf030 JN |
1770 | if (!defined $owner) { |
1771 | $owner = get_file_owner("$projectroot/$project"); | |
1772 | } | |
1773 | ||
1774 | return $owner; | |
1775 | } | |
1776 | ||
c60c56cc JN |
1777 | sub git_get_last_activity { |
1778 | my ($path) = @_; | |
1779 | my $fd; | |
1780 | ||
1781 | $git_dir = "$projectroot/$path"; | |
1782 | open($fd, "-|", git_cmd(), 'for-each-ref', | |
0ff5ec70 | 1783 | '--format=%(committer)', |
c60c56cc | 1784 | '--sort=-committerdate', |
0ff5ec70 | 1785 | '--count=1', |
c60c56cc JN |
1786 | 'refs/heads') or return; |
1787 | my $most_recent = <$fd>; | |
1788 | close $fd or return; | |
785cdea9 JN |
1789 | if (defined $most_recent && |
1790 | $most_recent =~ / (\d+) [-+][01]\d\d\d$/) { | |
c60c56cc JN |
1791 | my $timestamp = $1; |
1792 | my $age = time - $timestamp; | |
1793 | return ($age, age_string($age)); | |
1794 | } | |
c956395e | 1795 | return (undef, undef); |
c60c56cc JN |
1796 | } |
1797 | ||
847e01fb | 1798 | sub git_get_references { |
717b8311 JN |
1799 | my $type = shift || ""; |
1800 | my %refs; | |
28b9d9f7 JN |
1801 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
1802 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} | |
1803 | open my $fd, "-|", git_cmd(), "show-ref", "--dereference", | |
1804 | ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type | |
9704d75d | 1805 | or return; |
d294e1ca | 1806 | |
717b8311 JN |
1807 | while (my $line = <$fd>) { |
1808 | chomp $line; | |
28b9d9f7 | 1809 | if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) { |
717b8311 | 1810 | if (defined $refs{$1}) { |
d294e1ca | 1811 | push @{$refs{$1}}, $2; |
717b8311 | 1812 | } else { |
d294e1ca | 1813 | $refs{$1} = [ $2 ]; |
717b8311 JN |
1814 | } |
1815 | } | |
1816 | } | |
1817 | close $fd or return; | |
1818 | return \%refs; | |
1819 | } | |
1820 | ||
56a322f1 JN |
1821 | sub git_get_rev_name_tags { |
1822 | my $hash = shift || return undef; | |
1823 | ||
25691fbe | 1824 | open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash |
56a322f1 JN |
1825 | or return; |
1826 | my $name_rev = <$fd>; | |
1827 | close $fd; | |
1828 | ||
1829 | if ($name_rev =~ m|^$hash tags/(.*)$|) { | |
1830 | return $1; | |
1831 | } else { | |
1832 | # catches also '$hash undefined' output | |
1833 | return undef; | |
1834 | } | |
1835 | } | |
1836 | ||
717b8311 JN |
1837 | ## ---------------------------------------------------------------------- |
1838 | ## parse to hash functions | |
1839 | ||
847e01fb | 1840 | sub parse_date { |
717b8311 JN |
1841 | my $epoch = shift; |
1842 | my $tz = shift || "-0000"; | |
1843 | ||
1844 | my %date; | |
1845 | my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); | |
1846 | my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); | |
1847 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch); | |
1848 | $date{'hour'} = $hour; | |
1849 | $date{'minute'} = $min; | |
1850 | $date{'mday'} = $mday; | |
1851 | $date{'day'} = $days[$wday]; | |
1852 | $date{'month'} = $months[$mon]; | |
af6feeb2 JN |
1853 | $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", |
1854 | $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec; | |
952c65fc JN |
1855 | $date{'mday-time'} = sprintf "%d %s %02d:%02d", |
1856 | $mday, $months[$mon], $hour ,$min; | |
af6feeb2 | 1857 | $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ", |
a62d6d84 | 1858 | 1900+$year, 1+$mon, $mday, $hour ,$min, $sec; |
717b8311 JN |
1859 | |
1860 | $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/; | |
1861 | my $local = $epoch + ((int $1 + ($2/60)) * 3600); | |
1862 | ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local); | |
1863 | $date{'hour_local'} = $hour; | |
1864 | $date{'minute_local'} = $min; | |
1865 | $date{'tz_local'} = $tz; | |
af6feeb2 JN |
1866 | $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", |
1867 | 1900+$year, $mon+1, $mday, | |
1868 | $hour, $min, $sec, $tz); | |
717b8311 JN |
1869 | return %date; |
1870 | } | |
1871 | ||
847e01fb | 1872 | sub parse_tag { |
ede5e100 KS |
1873 | my $tag_id = shift; |
1874 | my %tag; | |
d8a20ba9 | 1875 | my @comment; |
ede5e100 | 1876 | |
25691fbe | 1877 | open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return; |
d8a20ba9 | 1878 | $tag{'id'} = $tag_id; |
ede5e100 KS |
1879 | while (my $line = <$fd>) { |
1880 | chomp $line; | |
1881 | if ($line =~ m/^object ([0-9a-fA-F]{40})$/) { | |
1882 | $tag{'object'} = $1; | |
7ab0d2b6 | 1883 | } elsif ($line =~ m/^type (.+)$/) { |
ede5e100 | 1884 | $tag{'type'} = $1; |
7ab0d2b6 | 1885 | } elsif ($line =~ m/^tag (.+)$/) { |
ede5e100 | 1886 | $tag{'name'} = $1; |
d8a20ba9 KS |
1887 | } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) { |
1888 | $tag{'author'} = $1; | |
1889 | $tag{'epoch'} = $2; | |
1890 | $tag{'tz'} = $3; | |
1891 | } elsif ($line =~ m/--BEGIN/) { | |
1892 | push @comment, $line; | |
1893 | last; | |
1894 | } elsif ($line eq "") { | |
1895 | last; | |
ede5e100 KS |
1896 | } |
1897 | } | |
d8a20ba9 KS |
1898 | push @comment, <$fd>; |
1899 | $tag{'comment'} = \@comment; | |
19806691 | 1900 | close $fd or return; |
ede5e100 KS |
1901 | if (!defined $tag{'name'}) { |
1902 | return | |
1903 | }; | |
1904 | return %tag | |
1905 | } | |
1906 | ||
756bbf54 | 1907 | sub parse_commit_text { |
ccdfdea0 | 1908 | my ($commit_text, $withparents) = @_; |
756bbf54 | 1909 | my @commit_lines = split '\n', $commit_text; |
703ac710 | 1910 | my %co; |
703ac710 | 1911 | |
756bbf54 RF |
1912 | pop @commit_lines; # Remove '\0' |
1913 | ||
198a2a8a JN |
1914 | if (! @commit_lines) { |
1915 | return; | |
1916 | } | |
1917 | ||
25f422fb | 1918 | my $header = shift @commit_lines; |
198a2a8a | 1919 | if ($header !~ m/^[0-9a-fA-F]{40}/) { |
25f422fb KS |
1920 | return; |
1921 | } | |
ccdfdea0 | 1922 | ($co{'id'}, my @parents) = split ' ', $header; |
19806691 | 1923 | while (my $line = shift @commit_lines) { |
b87d78d6 | 1924 | last if $line eq "\n"; |
7ab0d2b6 | 1925 | if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) { |
703ac710 | 1926 | $co{'tree'} = $1; |
ccdfdea0 | 1927 | } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) { |
208b2dff | 1928 | push @parents, $1; |
022be3d0 | 1929 | } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) { |
3f714537 | 1930 | $co{'author'} = $1; |
185f09e5 KS |
1931 | $co{'author_epoch'} = $2; |
1932 | $co{'author_tz'} = $3; | |
ba00b8c1 JN |
1933 | if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) { |
1934 | $co{'author_name'} = $1; | |
1935 | $co{'author_email'} = $2; | |
2bf7a52c KS |
1936 | } else { |
1937 | $co{'author_name'} = $co{'author'}; | |
1938 | } | |
86eed32d KS |
1939 | } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) { |
1940 | $co{'committer'} = $1; | |
185f09e5 KS |
1941 | $co{'committer_epoch'} = $2; |
1942 | $co{'committer_tz'} = $3; | |
991910a9 | 1943 | $co{'committer_name'} = $co{'committer'}; |
ba00b8c1 JN |
1944 | if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) { |
1945 | $co{'committer_name'} = $1; | |
1946 | $co{'committer_email'} = $2; | |
1947 | } else { | |
1948 | $co{'committer_name'} = $co{'committer'}; | |
1949 | } | |
703ac710 KS |
1950 | } |
1951 | } | |
ede5e100 | 1952 | if (!defined $co{'tree'}) { |
25f422fb | 1953 | return; |
ede5e100 | 1954 | }; |
208b2dff RF |
1955 | $co{'parents'} = \@parents; |
1956 | $co{'parent'} = $parents[0]; | |
25f422fb | 1957 | |
19806691 | 1958 | foreach my $title (@commit_lines) { |
c2488d06 | 1959 | $title =~ s/^ //; |
19806691 | 1960 | if ($title ne "") { |
48c771f4 | 1961 | $co{'title'} = chop_str($title, 80, 5); |
19806691 KS |
1962 | # remove leading stuff of merges to make the interesting part visible |
1963 | if (length($title) > 50) { | |
1964 | $title =~ s/^Automatic //; | |
1965 | $title =~ s/^merge (of|with) /Merge ... /i; | |
1966 | if (length($title) > 50) { | |
1967 | $title =~ s/(http|rsync):\/\///; | |
1968 | } | |
1969 | if (length($title) > 50) { | |
1970 | $title =~ s/(master|www|rsync)\.//; | |
1971 | } | |
1972 | if (length($title) > 50) { | |
1973 | $title =~ s/kernel.org:?//; | |
1974 | } | |
1975 | if (length($title) > 50) { | |
1976 | $title =~ s/\/pub\/scm//; | |
1977 | } | |
1978 | } | |
48c771f4 | 1979 | $co{'title_short'} = chop_str($title, 50, 5); |
19806691 KS |
1980 | last; |
1981 | } | |
1982 | } | |
7e0fe5c9 PB |
1983 | if ($co{'title'} eq "") { |
1984 | $co{'title'} = $co{'title_short'} = '(no commit message)'; | |
1985 | } | |
25f422fb KS |
1986 | # remove added spaces |
1987 | foreach my $line (@commit_lines) { | |
1988 | $line =~ s/^ //; | |
1989 | } | |
1990 | $co{'comment'} = \@commit_lines; | |
2ae100df KS |
1991 | |
1992 | my $age = time - $co{'committer_epoch'}; | |
1993 | $co{'age'} = $age; | |
d263a6bd | 1994 | $co{'age_string'} = age_string($age); |
71be1e79 KS |
1995 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'}); |
1996 | if ($age > 60*60*24*7*2) { | |
1b1cd421 | 1997 | $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; |
71be1e79 KS |
1998 | $co{'age_string_age'} = $co{'age_string'}; |
1999 | } else { | |
2000 | $co{'age_string_date'} = $co{'age_string'}; | |
1b1cd421 | 2001 | $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; |
71be1e79 | 2002 | } |
703ac710 KS |
2003 | return %co; |
2004 | } | |
2005 | ||
756bbf54 RF |
2006 | sub parse_commit { |
2007 | my ($commit_id) = @_; | |
2008 | my %co; | |
2009 | ||
2010 | local $/ = "\0"; | |
2011 | ||
2012 | open my $fd, "-|", git_cmd(), "rev-list", | |
ccdfdea0 | 2013 | "--parents", |
756bbf54 | 2014 | "--header", |
756bbf54 RF |
2015 | "--max-count=1", |
2016 | $commit_id, | |
2017 | "--", | |
2018 | or die_error(undef, "Open git-rev-list failed"); | |
ccdfdea0 | 2019 | %co = parse_commit_text(<$fd>, 1); |
756bbf54 RF |
2020 | close $fd; |
2021 | ||
2022 | return %co; | |
2023 | } | |
2024 | ||
2025 | sub parse_commits { | |
2026 | my ($commit_id, $maxcount, $skip, $arg, $filename) = @_; | |
2027 | my @cos; | |
2028 | ||
2029 | $maxcount ||= 1; | |
2030 | $skip ||= 0; | |
2031 | ||
756bbf54 RF |
2032 | local $/ = "\0"; |
2033 | ||
2034 | open my $fd, "-|", git_cmd(), "rev-list", | |
2035 | "--header", | |
756bbf54 RF |
2036 | ($arg ? ($arg) : ()), |
2037 | ("--max-count=" . $maxcount), | |
f47efbb7 | 2038 | ("--skip=" . $skip), |
868bc068 | 2039 | @extra_options, |
756bbf54 RF |
2040 | $commit_id, |
2041 | "--", | |
2042 | ($filename ? ($filename) : ()) | |
2043 | or die_error(undef, "Open git-rev-list failed"); | |
2044 | while (my $line = <$fd>) { | |
2045 | my %co = parse_commit_text($line); | |
2046 | push @cos, \%co; | |
2047 | } | |
2048 | close $fd; | |
2049 | ||
2050 | return wantarray ? @cos : \@cos; | |
2051 | } | |
2052 | ||
a446d6bb JN |
2053 | # parse ref from ref_file, given by ref_id, with given type |
2054 | sub parse_ref { | |
2055 | my $ref_file = shift; | |
2056 | my $ref_id = shift; | |
2057 | my $type = shift || git_get_type($ref_id); | |
2058 | my %ref_item; | |
2059 | ||
2060 | $ref_item{'type'} = $type; | |
2061 | $ref_item{'id'} = $ref_id; | |
2062 | $ref_item{'epoch'} = 0; | |
2063 | $ref_item{'age'} = "unknown"; | |
2064 | if ($type eq "tag") { | |
2065 | my %tag = parse_tag($ref_id); | |
2066 | $ref_item{'comment'} = $tag{'comment'}; | |
2067 | if ($tag{'type'} eq "commit") { | |
2068 | my %co = parse_commit($tag{'object'}); | |
2069 | $ref_item{'epoch'} = $co{'committer_epoch'}; | |
2070 | $ref_item{'age'} = $co{'age_string'}; | |
2071 | } elsif (defined($tag{'epoch'})) { | |
2072 | my $age = time - $tag{'epoch'}; | |
2073 | $ref_item{'epoch'} = $tag{'epoch'}; | |
2074 | $ref_item{'age'} = age_string($age); | |
2075 | } | |
2076 | $ref_item{'reftype'} = $tag{'type'}; | |
2077 | $ref_item{'name'} = $tag{'name'}; | |
2078 | $ref_item{'refid'} = $tag{'object'}; | |
2079 | } elsif ($type eq "commit"){ | |
2080 | my %co = parse_commit($ref_id); | |
2081 | $ref_item{'reftype'} = "commit"; | |
2082 | $ref_item{'name'} = $ref_file; | |
2083 | $ref_item{'title'} = $co{'title'}; | |
2084 | $ref_item{'refid'} = $ref_id; | |
2085 | $ref_item{'epoch'} = $co{'committer_epoch'}; | |
2086 | $ref_item{'age'} = $co{'age_string'}; | |
2087 | } else { | |
2088 | $ref_item{'reftype'} = $type; | |
2089 | $ref_item{'name'} = $ref_file; | |
2090 | $ref_item{'refid'} = $ref_id; | |
2091 | } | |
2092 | ||
2093 | return %ref_item; | |
2094 | } | |
2095 | ||
e8e41a93 | 2096 | # parse line of git-diff-tree "raw" output |
740e67f9 JN |
2097 | sub parse_difftree_raw_line { |
2098 | my $line = shift; | |
2099 | my %res; | |
2100 | ||
2101 | # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c' | |
2102 | # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c' | |
2103 | if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) { | |
2104 | $res{'from_mode'} = $1; | |
2105 | $res{'to_mode'} = $2; | |
2106 | $res{'from_id'} = $3; | |
2107 | $res{'to_id'} = $4; | |
6aa6f92f | 2108 | $res{'status'} = $res{'status_str'} = $5; |
740e67f9 JN |
2109 | $res{'similarity'} = $6; |
2110 | if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied | |
e8e41a93 | 2111 | ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7); |
740e67f9 | 2112 | } else { |
9d301456 | 2113 | $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7); |
740e67f9 JN |
2114 | } |
2115 | } | |
78bc403a JN |
2116 | # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh' |
2117 | # combined diff (for merge commit) | |
2118 | elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) { | |
2119 | $res{'nparents'} = length($1); | |
2120 | $res{'from_mode'} = [ split(' ', $2) ]; | |
2121 | $res{'to_mode'} = pop @{$res{'from_mode'}}; | |
2122 | $res{'from_id'} = [ split(' ', $3) ]; | |
2123 | $res{'to_id'} = pop @{$res{'from_id'}}; | |
6aa6f92f | 2124 | $res{'status_str'} = $4; |
78bc403a JN |
2125 | $res{'status'} = [ split('', $4) ]; |
2126 | $res{'to_file'} = unquote($5); | |
2127 | } | |
740e67f9 | 2128 | # 'c512b523472485aef4fff9e57b229d9d243c967f' |
0edcb37d JN |
2129 | elsif ($line =~ m/^([0-9a-fA-F]{40})$/) { |
2130 | $res{'commit'} = $1; | |
2131 | } | |
740e67f9 JN |
2132 | |
2133 | return wantarray ? %res : \%res; | |
2134 | } | |
2135 | ||
0cec6db5 JN |
2136 | # wrapper: return parsed line of git-diff-tree "raw" output |
2137 | # (the argument might be raw line, or parsed info) | |
2138 | sub parsed_difftree_line { | |
2139 | my $line_or_ref = shift; | |
2140 | ||
2141 | if (ref($line_or_ref) eq "HASH") { | |
2142 | # pre-parsed (or generated by hand) | |
2143 | return $line_or_ref; | |
2144 | } else { | |
2145 | return parse_difftree_raw_line($line_or_ref); | |
2146 | } | |
2147 | } | |
2148 | ||
cb849b46 JN |
2149 | # parse line of git-ls-tree output |
2150 | sub parse_ls_tree_line ($;%) { | |
2151 | my $line = shift; | |
2152 | my %opts = @_; | |
2153 | my %res; | |
2154 | ||
2155 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' | |
8b4b94cc | 2156 | $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s; |
cb849b46 JN |
2157 | |
2158 | $res{'mode'} = $1; | |
2159 | $res{'type'} = $2; | |
2160 | $res{'hash'} = $3; | |
2161 | if ($opts{'-z'}) { | |
2162 | $res{'name'} = $4; | |
2163 | } else { | |
2164 | $res{'name'} = unquote($4); | |
2165 | } | |
2166 | ||
2167 | return wantarray ? %res : \%res; | |
2168 | } | |
2169 | ||
90921740 JN |
2170 | # generates _two_ hashes, references to which are passed as 2 and 3 argument |
2171 | sub parse_from_to_diffinfo { | |
2172 | my ($diffinfo, $from, $to, @parents) = @_; | |
2173 | ||
2174 | if ($diffinfo->{'nparents'}) { | |
2175 | # combined diff | |
2176 | $from->{'file'} = []; | |
2177 | $from->{'href'} = []; | |
2178 | fill_from_file_info($diffinfo, @parents) | |
2179 | unless exists $diffinfo->{'from_file'}; | |
2180 | for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) { | |
9d301456 JN |
2181 | $from->{'file'}[$i] = |
2182 | defined $diffinfo->{'from_file'}[$i] ? | |
2183 | $diffinfo->{'from_file'}[$i] : | |
2184 | $diffinfo->{'to_file'}; | |
90921740 JN |
2185 | if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file |
2186 | $from->{'href'}[$i] = href(action=>"blob", | |
2187 | hash_base=>$parents[$i], | |
2188 | hash=>$diffinfo->{'from_id'}[$i], | |
2189 | file_name=>$from->{'file'}[$i]); | |
2190 | } else { | |
2191 | $from->{'href'}[$i] = undef; | |
2192 | } | |
2193 | } | |
2194 | } else { | |
0cec6db5 | 2195 | # ordinary (not combined) diff |
9d301456 | 2196 | $from->{'file'} = $diffinfo->{'from_file'}; |
90921740 JN |
2197 | if ($diffinfo->{'status'} ne "A") { # not new (added) file |
2198 | $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent, | |
2199 | hash=>$diffinfo->{'from_id'}, | |
2200 | file_name=>$from->{'file'}); | |
2201 | } else { | |
2202 | delete $from->{'href'}; | |
2203 | } | |
2204 | } | |
2205 | ||
9d301456 | 2206 | $to->{'file'} = $diffinfo->{'to_file'}; |
90921740 JN |
2207 | if (!is_deleted($diffinfo)) { # file exists in result |
2208 | $to->{'href'} = href(action=>"blob", hash_base=>$hash, | |
2209 | hash=>$diffinfo->{'to_id'}, | |
2210 | file_name=>$to->{'file'}); | |
2211 | } else { | |
2212 | delete $to->{'href'}; | |
2213 | } | |
2214 | } | |
2215 | ||
717b8311 JN |
2216 | ## ...................................................................... |
2217 | ## parse to array of hashes functions | |
4c02e3c5 | 2218 | |
cd146408 JN |
2219 | sub git_get_heads_list { |
2220 | my $limit = shift; | |
2221 | my @headslist; | |
2222 | ||
2223 | open my $fd, '-|', git_cmd(), 'for-each-ref', | |
2224 | ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate', | |
2225 | '--format=%(objectname) %(refname) %(subject)%00%(committer)', | |
2226 | 'refs/heads' | |
c83a77e4 JN |
2227 | or return; |
2228 | while (my $line = <$fd>) { | |
cd146408 | 2229 | my %ref_item; |
120ddde2 | 2230 | |
cd146408 JN |
2231 | chomp $line; |
2232 | my ($refinfo, $committerinfo) = split(/\0/, $line); | |
2233 | my ($hash, $name, $title) = split(' ', $refinfo, 3); | |
2234 | my ($committer, $epoch, $tz) = | |
2235 | ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/); | |
bf901f8e | 2236 | $ref_item{'fullname'} = $name; |
cd146408 JN |
2237 | $name =~ s!^refs/heads/!!; |
2238 | ||
2239 | $ref_item{'name'} = $name; | |
2240 | $ref_item{'id'} = $hash; | |
2241 | $ref_item{'title'} = $title || '(no commit message)'; | |
2242 | $ref_item{'epoch'} = $epoch; | |
2243 | if ($epoch) { | |
2244 | $ref_item{'age'} = age_string(time - $ref_item{'epoch'}); | |
2245 | } else { | |
2246 | $ref_item{'age'} = "unknown"; | |
717b8311 | 2247 | } |
cd146408 JN |
2248 | |
2249 | push @headslist, \%ref_item; | |
c83a77e4 JN |
2250 | } |
2251 | close $fd; | |
2252 | ||
cd146408 JN |
2253 | return wantarray ? @headslist : \@headslist; |
2254 | } | |
2255 | ||
2256 | sub git_get_tags_list { | |
2257 | my $limit = shift; | |
2258 | my @tagslist; | |
2259 | ||
2260 | open my $fd, '-|', git_cmd(), 'for-each-ref', | |
2261 | ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate', | |
2262 | '--format=%(objectname) %(objecttype) %(refname) '. | |
2263 | '%(*objectname) %(*objecttype) %(subject)%00%(creator)', | |
2264 | 'refs/tags' | |
2265 | or return; | |
2266 | while (my $line = <$fd>) { | |
2267 | my %ref_item; | |
7a13b999 | 2268 | |
cd146408 JN |
2269 | chomp $line; |
2270 | my ($refinfo, $creatorinfo) = split(/\0/, $line); | |
2271 | my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6); | |
2272 | my ($creator, $epoch, $tz) = | |
2273 | ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/); | |
bf901f8e | 2274 | $ref_item{'fullname'} = $name; |
cd146408 JN |
2275 | $name =~ s!^refs/tags/!!; |
2276 | ||
2277 | $ref_item{'type'} = $type; | |
2278 | $ref_item{'id'} = $id; | |
2279 | $ref_item{'name'} = $name; | |
2280 | if ($type eq "tag") { | |
2281 | $ref_item{'subject'} = $title; | |
2282 | $ref_item{'reftype'} = $reftype; | |
2283 | $ref_item{'refid'} = $refid; | |
2284 | } else { | |
2285 | $ref_item{'reftype'} = $type; | |
2286 | $ref_item{'refid'} = $id; | |
2287 | } | |
2288 | ||
2289 | if ($type eq "tag" || $type eq "commit") { | |
2290 | $ref_item{'epoch'} = $epoch; | |
2291 | if ($epoch) { | |
2292 | $ref_item{'age'} = age_string(time - $ref_item{'epoch'}); | |
2293 | } else { | |
2294 | $ref_item{'age'} = "unknown"; | |
2295 | } | |
2296 | } | |
991910a9 | 2297 | |
cd146408 | 2298 | push @tagslist, \%ref_item; |
717b8311 | 2299 | } |
cd146408 JN |
2300 | close $fd; |
2301 | ||
2302 | return wantarray ? @tagslist : \@tagslist; | |
86eed32d KS |
2303 | } |
2304 | ||
717b8311 JN |
2305 | ## ---------------------------------------------------------------------- |
2306 | ## filesystem-related functions | |
022be3d0 | 2307 | |
c07ad4b9 KS |
2308 | sub get_file_owner { |
2309 | my $path = shift; | |
2310 | ||
2311 | my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path); | |
2312 | my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid); | |
2313 | if (!defined $gcos) { | |
2314 | return undef; | |
2315 | } | |
2316 | my $owner = $gcos; | |
2317 | $owner =~ s/[,;].*$//; | |
00f429af | 2318 | return to_utf8($owner); |
c07ad4b9 KS |
2319 | } |
2320 | ||
717b8311 JN |
2321 | ## ...................................................................... |
2322 | ## mimetype related functions | |
09bd7898 | 2323 | |
717b8311 JN |
2324 | sub mimetype_guess_file { |
2325 | my $filename = shift; | |
2326 | my $mimemap = shift; | |
2327 | -r $mimemap or return undef; | |
2328 | ||
2329 | my %mimemap; | |
2330 | open(MIME, $mimemap) or return undef; | |
2331 | while (<MIME>) { | |
618918e5 | 2332 | next if m/^#/; # skip comments |
717b8311 | 2333 | my ($mime, $exts) = split(/\t+/); |
46b059d7 JH |
2334 | if (defined $exts) { |
2335 | my @exts = split(/\s+/, $exts); | |
2336 | foreach my $ext (@exts) { | |
2337 | $mimemap{$ext} = $mime; | |
2338 | } | |
09bd7898 | 2339 | } |
09bd7898 | 2340 | } |
717b8311 | 2341 | close(MIME); |
09bd7898 | 2342 | |
8059319a | 2343 | $filename =~ /\.([^.]*)$/; |
717b8311 JN |
2344 | return $mimemap{$1}; |
2345 | } | |
5996ca08 | 2346 | |
717b8311 JN |
2347 | sub mimetype_guess { |
2348 | my $filename = shift; | |
2349 | my $mime; | |
2350 | $filename =~ /\./ or return undef; | |
5996ca08 | 2351 | |
717b8311 JN |
2352 | if ($mimetypes_file) { |
2353 | my $file = $mimetypes_file; | |
d5aa50de JN |
2354 | if ($file !~ m!^/!) { # if it is relative path |
2355 | # it is relative to project | |
2356 | $file = "$projectroot/$project/$file"; | |
2357 | } | |
717b8311 JN |
2358 | $mime = mimetype_guess_file($filename, $file); |
2359 | } | |
2360 | $mime ||= mimetype_guess_file($filename, '/etc/mime.types'); | |
2361 | return $mime; | |
5996ca08 FF |
2362 | } |
2363 | ||
847e01fb | 2364 | sub blob_mimetype { |
717b8311 JN |
2365 | my $fd = shift; |
2366 | my $filename = shift; | |
5996ca08 | 2367 | |
717b8311 JN |
2368 | if ($filename) { |
2369 | my $mime = mimetype_guess($filename); | |
2370 | $mime and return $mime; | |
d8d17b5d | 2371 | } |
717b8311 JN |
2372 | |
2373 | # just in case | |
2374 | return $default_blob_plain_mimetype unless $fd; | |
2375 | ||
2376 | if (-T $fd) { | |
2377 | return 'text/plain' . | |
2378 | ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : ''); | |
2379 | } elsif (! $filename) { | |
2380 | return 'application/octet-stream'; | |
2381 | } elsif ($filename =~ m/\.png$/i) { | |
2382 | return 'image/png'; | |
2383 | } elsif ($filename =~ m/\.gif$/i) { | |
2384 | return 'image/gif'; | |
2385 | } elsif ($filename =~ m/\.jpe?g$/i) { | |
2386 | return 'image/jpeg'; | |
d8d17b5d | 2387 | } else { |
717b8311 | 2388 | return 'application/octet-stream'; |
f7ab660c | 2389 | } |
717b8311 JN |
2390 | } |
2391 | ||
2392 | ## ====================================================================== | |
2393 | ## functions printing HTML: header, footer, error page | |
2394 | ||
2395 | sub git_header_html { | |
2396 | my $status = shift || "200 OK"; | |
2397 | my $expires = shift; | |
2398 | ||
8be2890c | 2399 | my $title = "$site_name"; |
717b8311 | 2400 | if (defined $project) { |
00f429af | 2401 | $title .= " - " . to_utf8($project); |
717b8311 JN |
2402 | if (defined $action) { |
2403 | $title .= "/$action"; | |
2404 | if (defined $file_name) { | |
403d0906 | 2405 | $title .= " - " . esc_path($file_name); |
717b8311 JN |
2406 | if ($action eq "tree" && $file_name !~ m|/$|) { |
2407 | $title .= "/"; | |
2408 | } | |
2409 | } | |
2410 | } | |
f7ab660c | 2411 | } |
717b8311 JN |
2412 | my $content_type; |
2413 | # require explicit support from the UA if we are to send the page as | |
2414 | # 'application/xhtml+xml', otherwise send it as plain old 'text/html'. | |
2415 | # we have to do this because MSIE sometimes globs '*/*', pretending to | |
2416 | # support xhtml+xml but choking when it gets what it asked for. | |
952c65fc JN |
2417 | if (defined $cgi->http('HTTP_ACCEPT') && |
2418 | $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && | |
2419 | $cgi->Accept('application/xhtml+xml') != 0) { | |
717b8311 | 2420 | $content_type = 'application/xhtml+xml'; |
f7ab660c | 2421 | } else { |
717b8311 | 2422 | $content_type = 'text/html'; |
f7ab660c | 2423 | } |
952c65fc JN |
2424 | print $cgi->header(-type=>$content_type, -charset => 'utf-8', |
2425 | -status=> $status, -expires => $expires); | |
45c9a758 | 2426 | my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : ''; |
717b8311 JN |
2427 | print <<EOF; |
2428 | <?xml version="1.0" encoding="utf-8"?> | |
2429 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
2430 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> | |
d4baf9ea | 2431 | <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke --> |
717b8311 JN |
2432 | <!-- git core binaries version $git_version --> |
2433 | <head> | |
2434 | <meta http-equiv="content-type" content="$content_type; charset=utf-8"/> | |
45c9a758 | 2435 | <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/> |
717b8311 JN |
2436 | <meta name="robots" content="index, nofollow"/> |
2437 | <title>$title</title> | |
717b8311 | 2438 | EOF |
b2d3476e AC |
2439 | # print out each stylesheet that exist |
2440 | if (defined $stylesheet) { | |
2441 | #provides backwards capability for those people who define style sheet in a config file | |
2442 | print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n"; | |
2443 | } else { | |
2444 | foreach my $stylesheet (@stylesheets) { | |
2445 | next unless $stylesheet; | |
2446 | print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n"; | |
2447 | } | |
2448 | } | |
dd04c428 | 2449 | if (defined $project) { |
af6feeb2 JN |
2450 | printf('<link rel="alternate" title="%s log RSS feed" '. |
2451 | 'href="%s" type="application/rss+xml" />'."\n", | |
1c2a4f5a | 2452 | esc_param($project), href(action=>"rss")); |
12075103 JN |
2453 | printf('<link rel="alternate" title="%s log RSS feed (no merges)" '. |
2454 | 'href="%s" type="application/rss+xml" />'."\n", | |
2455 | esc_param($project), href(action=>"rss", | |
2456 | extra_options=>"--no-merges")); | |
af6feeb2 JN |
2457 | printf('<link rel="alternate" title="%s log Atom feed" '. |
2458 | 'href="%s" type="application/atom+xml" />'."\n", | |
2459 | esc_param($project), href(action=>"atom")); | |
12075103 JN |
2460 | printf('<link rel="alternate" title="%s log Atom feed (no merges)" '. |
2461 | 'href="%s" type="application/atom+xml" />'."\n", | |
2462 | esc_param($project), href(action=>"atom", | |
2463 | extra_options=>"--no-merges")); | |
9d0734ae JN |
2464 | } else { |
2465 | printf('<link rel="alternate" title="%s projects list" '. | |
2466 | 'href="%s" type="text/plain; charset=utf-8"/>'."\n", | |
2467 | $site_name, href(project=>undef, action=>"project_index")); | |
af6feeb2 | 2468 | printf('<link rel="alternate" title="%s projects feeds" '. |
9d0734ae JN |
2469 | 'href="%s" type="text/x-opml"/>'."\n", |
2470 | $site_name, href(project=>undef, action=>"opml")); | |
dd04c428 | 2471 | } |
0b5deba1 JN |
2472 | if (defined $favicon) { |
2473 | print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n); | |
2474 | } | |
10161355 | 2475 | |
dd04c428 | 2476 | print "</head>\n" . |
b2d3476e AC |
2477 | "<body>\n"; |
2478 | ||
2479 | if (-f $site_header) { | |
2480 | open (my $fd, $site_header); | |
2481 | print <$fd>; | |
2482 | close $fd; | |
2483 | } | |
2484 | ||
2485 | print "<div class=\"page_header\">\n" . | |
9a7a62ff JN |
2486 | $cgi->a({-href => esc_url($logo_url), |
2487 | -title => $logo_label}, | |
2488 | qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>)); | |
f93bff8d | 2489 | print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / "; |
717b8311 | 2490 | if (defined $project) { |
1c2a4f5a | 2491 | print $cgi->a({-href => href(action=>"summary")}, esc_html($project)); |
717b8311 JN |
2492 | if (defined $action) { |
2493 | print " / $action"; | |
2494 | } | |
2495 | print "\n"; | |
6be93511 | 2496 | } |
d77b5673 PB |
2497 | print "</div>\n"; |
2498 | ||
6be93511 RF |
2499 | my ($have_search) = gitweb_check_feature('search'); |
2500 | if ((defined $project) && ($have_search)) { | |
717b8311 JN |
2501 | if (!defined $searchtext) { |
2502 | $searchtext = ""; | |
2503 | } | |
2504 | my $search_hash; | |
2505 | if (defined $hash_base) { | |
2506 | $search_hash = $hash_base; | |
2507 | } elsif (defined $hash) { | |
2508 | $search_hash = $hash; | |
bddec01d | 2509 | } else { |
717b8311 | 2510 | $search_hash = "HEAD"; |
bddec01d | 2511 | } |
40375a83 MM |
2512 | my $action = $my_uri; |
2513 | my ($use_pathinfo) = gitweb_check_feature('pathinfo'); | |
2514 | if ($use_pathinfo) { | |
2515 | $action .= "/$project"; | |
2516 | } else { | |
2517 | $cgi->param("p", $project); | |
2518 | } | |
717b8311 JN |
2519 | $cgi->param("a", "search"); |
2520 | $cgi->param("h", $search_hash); | |
40375a83 | 2521 | print $cgi->startform(-method => "get", -action => $action) . |
717b8311 | 2522 | "<div class=\"search\">\n" . |
40375a83 | 2523 | (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") . |
717b8311 JN |
2524 | $cgi->hidden(-name => "a") . "\n" . |
2525 | $cgi->hidden(-name => "h") . "\n" . | |
88ad729b | 2526 | $cgi->popup_menu(-name => 'st', -default => 'commit', |
e7738553 | 2527 | -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) . |
88ad729b PB |
2528 | $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) . |
2529 | " search:\n", | |
717b8311 JN |
2530 | $cgi->textfield(-name => "s", -value => $searchtext) . "\n" . |
2531 | "</div>" . | |
2532 | $cgi->end_form() . "\n"; | |
b87d78d6 | 2533 | } |
717b8311 JN |
2534 | } |
2535 | ||
2536 | sub git_footer_html { | |
2537 | print "<div class=\"page_footer\">\n"; | |
2538 | if (defined $project) { | |
847e01fb | 2539 | my $descr = git_get_project_description($project); |
717b8311 JN |
2540 | if (defined $descr) { |
2541 | print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n"; | |
2542 | } | |
a1565c44 | 2543 | print $cgi->a({-href => href(action=>"rss"), |
af6feeb2 JN |
2544 | -class => "rss_logo"}, "RSS") . " "; |
2545 | print $cgi->a({-href => href(action=>"atom"), | |
2546 | -class => "rss_logo"}, "Atom") . "\n"; | |
717b8311 | 2547 | } else { |
a1565c44 | 2548 | print $cgi->a({-href => href(project=>undef, action=>"opml"), |
9d0734ae JN |
2549 | -class => "rss_logo"}, "OPML") . " "; |
2550 | print $cgi->a({-href => href(project=>undef, action=>"project_index"), | |
2551 | -class => "rss_logo"}, "TXT") . "\n"; | |
717b8311 | 2552 | } |
b2d3476e AC |
2553 | print "</div>\n" ; |
2554 | ||
2555 | if (-f $site_footer) { | |
2556 | open (my $fd, $site_footer); | |
2557 | print <$fd>; | |
2558 | close $fd; | |
2559 | } | |
2560 | ||
2561 | print "</body>\n" . | |
717b8311 JN |
2562 | "</html>"; |
2563 | } | |
2564 | ||
2565 | sub die_error { | |
2566 | my $status = shift || "403 Forbidden"; | |
2567 | my $error = shift || "Malformed query, file missing or permission denied"; | |
2568 | ||
2569 | git_header_html($status); | |
59b9f61a JN |
2570 | print <<EOF; |
2571 | <div class="page_body"> | |
2572 | <br /><br /> | |
2573 | $status - $error | |
2574 | <br /> | |
2575 | </div> | |
2576 | EOF | |
b87d78d6 | 2577 | git_footer_html(); |
717b8311 | 2578 | exit; |
161332a5 KS |
2579 | } |
2580 | ||
717b8311 JN |
2581 | ## ---------------------------------------------------------------------- |
2582 | ## functions printing or outputting HTML: navigation | |
2583 | ||
847e01fb | 2584 | sub git_print_page_nav { |
717b8311 JN |
2585 | my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_; |
2586 | $extra = '' if !defined $extra; # pager or formats | |
2587 | ||
2588 | my @navs = qw(summary shortlog log commit commitdiff tree); | |
2589 | if ($suppress) { | |
2590 | @navs = grep { $_ ne $suppress } @navs; | |
2591 | } | |
2592 | ||
1c2a4f5a | 2593 | my %arg = map { $_ => {action=>$_} } @navs; |
717b8311 JN |
2594 | if (defined $head) { |
2595 | for (qw(commit commitdiff)) { | |
3be8e720 | 2596 | $arg{$_}{'hash'} = $head; |
717b8311 JN |
2597 | } |
2598 | if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) { | |
2599 | for (qw(shortlog log)) { | |
3be8e720 | 2600 | $arg{$_}{'hash'} = $head; |
045e531a | 2601 | } |
6a928415 KS |
2602 | } |
2603 | } | |
3be8e720 JN |
2604 | $arg{'tree'}{'hash'} = $treehead if defined $treehead; |
2605 | $arg{'tree'}{'hash_base'} = $treebase if defined $treebase; | |
717b8311 JN |
2606 | |
2607 | print "<div class=\"page_nav\">\n" . | |
2608 | (join " | ", | |
1c2a4f5a MW |
2609 | map { $_ eq $current ? |
2610 | $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_") | |
2611 | } @navs); | |
717b8311 JN |
2612 | print "<br/>\n$extra<br/>\n" . |
2613 | "</div>\n"; | |
6a928415 KS |
2614 | } |
2615 | ||
847e01fb | 2616 | sub format_paging_nav { |
717b8311 JN |
2617 | my ($action, $hash, $head, $page, $nrevs) = @_; |
2618 | my $paging_nav; | |
594e212b | 2619 | |
717b8311 JN |
2620 | |
2621 | if ($hash ne $head || $page) { | |
1c2a4f5a | 2622 | $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD"); |
594e212b | 2623 | } else { |
717b8311 JN |
2624 | $paging_nav .= "HEAD"; |
2625 | } | |
2626 | ||
2627 | if ($page > 0) { | |
2628 | $paging_nav .= " ⋅ " . | |
7afd77bf | 2629 | $cgi->a({-href => href(-replay=>1, page=>$page-1), |
26298b5f | 2630 | -accesskey => "p", -title => "Alt-p"}, "prev"); |
717b8311 JN |
2631 | } else { |
2632 | $paging_nav .= " ⋅ prev"; | |
2633 | } | |
2634 | ||
2635 | if ($nrevs >= (100 * ($page+1)-1)) { | |
2636 | $paging_nav .= " ⋅ " . | |
7afd77bf | 2637 | $cgi->a({-href => href(-replay=>1, page=>$page+1), |
26298b5f | 2638 | -accesskey => "n", -title => "Alt-n"}, "next"); |
717b8311 JN |
2639 | } else { |
2640 | $paging_nav .= " ⋅ next"; | |
594e212b | 2641 | } |
717b8311 JN |
2642 | |
2643 | return $paging_nav; | |
594e212b JN |
2644 | } |
2645 | ||
717b8311 JN |
2646 | ## ...................................................................... |
2647 | ## functions printing or outputting HTML: div | |
2648 | ||
847e01fb | 2649 | sub git_print_header_div { |
717b8311 | 2650 | my ($action, $title, $hash, $hash_base) = @_; |
1c2a4f5a | 2651 | my %args = (); |
717b8311 | 2652 | |
3be8e720 JN |
2653 | $args{'action'} = $action; |
2654 | $args{'hash'} = $hash if $hash; | |
2655 | $args{'hash_base'} = $hash_base if $hash_base; | |
717b8311 JN |
2656 | |
2657 | print "<div class=\"header\">\n" . | |
1c2a4f5a MW |
2658 | $cgi->a({-href => href(%args), -class => "title"}, |
2659 | $title ? $title : $action) . | |
2660 | "\n</div>\n"; | |
717b8311 | 2661 | } |
ede5e100 | 2662 | |
6fd92a28 JN |
2663 | #sub git_print_authorship (\%) { |
2664 | sub git_print_authorship { | |
2665 | my $co = shift; | |
2666 | ||
a44465cc | 2667 | my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'}); |
6fd92a28 JN |
2668 | print "<div class=\"author_date\">" . |
2669 | esc_html($co->{'author_name'}) . | |
a44465cc JN |
2670 | " [$ad{'rfc2822'}"; |
2671 | if ($ad{'hour_local'} < 6) { | |
2672 | printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", | |
2673 | $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}); | |
2674 | } else { | |
2675 | printf(" (%02d:%02d %s)", | |
2676 | $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}); | |
2677 | } | |
2678 | print "]</div>\n"; | |
6fd92a28 JN |
2679 | } |
2680 | ||
717b8311 JN |
2681 | sub git_print_page_path { |
2682 | my $name = shift; | |
2683 | my $type = shift; | |
59fb1c94 | 2684 | my $hb = shift; |
ede5e100 | 2685 | |
4df118ed JN |
2686 | |
2687 | print "<div class=\"page_path\">"; | |
2688 | print $cgi->a({-href => href(action=>"tree", hash_base=>$hb), | |
00f429af | 2689 | -title => 'tree root'}, to_utf8("[$project]")); |
4df118ed JN |
2690 | print " / "; |
2691 | if (defined $name) { | |
762c7205 JN |
2692 | my @dirname = split '/', $name; |
2693 | my $basename = pop @dirname; | |
2694 | my $fullname = ''; | |
2695 | ||
762c7205 | 2696 | foreach my $dir (@dirname) { |
16fdb488 | 2697 | $fullname .= ($fullname ? '/' : '') . $dir; |
762c7205 JN |
2698 | print $cgi->a({-href => href(action=>"tree", file_name=>$fullname, |
2699 | hash_base=>$hb), | |
edc04e90 | 2700 | -title => $fullname}, esc_path($dir)); |
26d0a976 | 2701 | print " / "; |
762c7205 JN |
2702 | } |
2703 | if (defined $type && $type eq 'blob') { | |
952c65fc | 2704 | print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name, |
762c7205 | 2705 | hash_base=>$hb), |
edc04e90 | 2706 | -title => $name}, esc_path($basename)); |
762c7205 JN |
2707 | } elsif (defined $type && $type eq 'tree') { |
2708 | print $cgi->a({-href => href(action=>"tree", file_name=>$file_name, | |
2709 | hash_base=>$hb), | |
edc04e90 | 2710 | -title => $name}, esc_path($basename)); |
4df118ed | 2711 | print " / "; |
59fb1c94 | 2712 | } else { |
403d0906 | 2713 | print esc_path($basename); |
59fb1c94 | 2714 | } |
ede5e100 | 2715 | } |
4df118ed | 2716 | print "<br/></div>\n"; |
ede5e100 KS |
2717 | } |
2718 | ||
b7f9253d JN |
2719 | # sub git_print_log (\@;%) { |
2720 | sub git_print_log ($;%) { | |
d16d093c | 2721 | my $log = shift; |
b7f9253d | 2722 | my %opts = @_; |
d16d093c | 2723 | |
b7f9253d JN |
2724 | if ($opts{'-remove_title'}) { |
2725 | # remove title, i.e. first line of log | |
2726 | shift @$log; | |
2727 | } | |
d16d093c JN |
2728 | # remove leading empty lines |
2729 | while (defined $log->[0] && $log->[0] eq "") { | |
2730 | shift @$log; | |
2731 | } | |
2732 | ||
2733 | # print log | |
2734 | my $signoff = 0; | |
2735 | my $empty = 0; | |
2736 | foreach my $line (@$log) { | |
b7f9253d JN |
2737 | if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) { |
2738 | $signoff = 1; | |
fba20b42 | 2739 | $empty = 0; |
b7f9253d JN |
2740 | if (! $opts{'-remove_signoff'}) { |
2741 | print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n"; | |
2742 | next; | |
2743 | } else { | |
2744 | # remove signoff lines | |
2745 | next; | |
2746 | } | |
2747 | } else { | |
2748 | $signoff = 0; | |
2749 | } | |
2750 | ||
d16d093c JN |
2751 | # print only one empty line |
2752 | # do not print empty line after signoff | |
2753 | if ($line eq "") { | |
2754 | next if ($empty || $signoff); | |
2755 | $empty = 1; | |
2756 | } else { | |
2757 | $empty = 0; | |
2758 | } | |
b7f9253d JN |
2759 | |
2760 | print format_log_line_html($line) . "<br/>\n"; | |
2761 | } | |
2762 | ||
2763 | if ($opts{'-final_empty_line'}) { | |
2764 | # end with single empty line | |
2765 | print "<br/>\n" unless $empty; | |
d16d093c JN |
2766 | } |
2767 | } | |
2768 | ||
e33fba4c JN |
2769 | # return link target (what link points to) |
2770 | sub git_get_link_target { | |
2771 | my $hash = shift; | |
2772 | my $link_target; | |
2773 | ||
2774 | # read link | |
2775 | open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash | |
2776 | or return; | |
2777 | { | |
2778 | local $/; | |
2779 | $link_target = <$fd>; | |
2780 | } | |
2781 | close $fd | |
2782 | or return; | |
2783 | ||
2784 | return $link_target; | |
2785 | } | |
2786 | ||
3bf9d570 JN |
2787 | # given link target, and the directory (basedir) the link is in, |
2788 | # return target of link relative to top directory (top tree); | |
2789 | # return undef if it is not possible (including absolute links). | |
2790 | sub normalize_link_target { | |
2791 | my ($link_target, $basedir, $hash_base) = @_; | |
2792 | ||
2793 | # we can normalize symlink target only if $hash_base is provided | |
2794 | return unless $hash_base; | |
2795 | ||
2796 | # absolute symlinks (beginning with '/') cannot be normalized | |
2797 | return if (substr($link_target, 0, 1) eq '/'); | |
2798 | ||
2799 | # normalize link target to path from top (root) tree (dir) | |
2800 | my $path; | |
2801 | if ($basedir) { | |
2802 | $path = $basedir . '/' . $link_target; | |
2803 | } else { | |
2804 | # we are in top (root) tree (dir) | |
2805 | $path = $link_target; | |
2806 | } | |
2807 | ||
2808 | # remove //, /./, and /../ | |
2809 | my @path_parts; | |
2810 | foreach my $part (split('/', $path)) { | |
2811 | # discard '.' and '' | |
2812 | next if (!$part || $part eq '.'); | |
2813 | # handle '..' | |
2814 | if ($part eq '..') { | |
2815 | if (@path_parts) { | |
2816 | pop @path_parts; | |
2817 | } else { | |
2818 | # link leads outside repository (outside top dir) | |
2819 | return; | |
2820 | } | |
2821 | } else { | |
2822 | push @path_parts, $part; | |
2823 | } | |
2824 | } | |
2825 | $path = join('/', @path_parts); | |
2826 | ||
2827 | return $path; | |
2828 | } | |
e33fba4c | 2829 | |
fa702003 JN |
2830 | # print tree entry (row of git_tree), but without encompassing <tr> element |
2831 | sub git_print_tree_entry { | |
2832 | my ($t, $basedir, $hash_base, $have_blame) = @_; | |
2833 | ||
2834 | my %base_key = (); | |
e33fba4c | 2835 | $base_key{'hash_base'} = $hash_base if defined $hash_base; |
fa702003 | 2836 | |
4de741b3 LT |
2837 | # The format of a table row is: mode list link. Where mode is |
2838 | # the mode of the entry, list is the name of the entry, an href, | |
2839 | # and link is the action links of the entry. | |
2840 | ||
fa702003 JN |
2841 | print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n"; |
2842 | if ($t->{'type'} eq "blob") { | |
2843 | print "<td class=\"list\">" . | |
4de741b3 | 2844 | $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'}, |
e7fb022a | 2845 | file_name=>"$basedir$t->{'name'}", %base_key), |
e33fba4c JN |
2846 | -class => "list"}, esc_path($t->{'name'})); |
2847 | if (S_ISLNK(oct $t->{'mode'})) { | |
2848 | my $link_target = git_get_link_target($t->{'hash'}); | |
2849 | if ($link_target) { | |
3bf9d570 JN |
2850 | my $norm_target = normalize_link_target($link_target, $basedir, $hash_base); |
2851 | if (defined $norm_target) { | |
2852 | print " -> " . | |
2853 | $cgi->a({-href => href(action=>"object", hash_base=>$hash_base, | |
2854 | file_name=>$norm_target), | |
2855 | -title => $norm_target}, esc_path($link_target)); | |
2856 | } else { | |
2857 | print " -> " . esc_path($link_target); | |
2858 | } | |
e33fba4c JN |
2859 | } |
2860 | } | |
2861 | print "</td>\n"; | |
4de741b3 | 2862 | print "<td class=\"link\">"; |
4777b014 | 2863 | print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'}, |
e33fba4c JN |
2864 | file_name=>"$basedir$t->{'name'}", %base_key)}, |
2865 | "blob"); | |
fa702003 | 2866 | if ($have_blame) { |
4777b014 PB |
2867 | print " | " . |
2868 | $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'}, | |
e33fba4c JN |
2869 | file_name=>"$basedir$t->{'name'}", %base_key)}, |
2870 | "blame"); | |
fa702003 JN |
2871 | } |
2872 | if (defined $hash_base) { | |
4777b014 PB |
2873 | print " | " . |
2874 | $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, | |
fa702003 JN |
2875 | hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")}, |
2876 | "history"); | |
2877 | } | |
2878 | print " | " . | |
6f7ea5fb | 2879 | $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base, |
e7fb022a JN |
2880 | file_name=>"$basedir$t->{'name'}")}, |
2881 | "raw"); | |
4de741b3 | 2882 | print "</td>\n"; |
fa702003 JN |
2883 | |
2884 | } elsif ($t->{'type'} eq "tree") { | |
0fa105e7 LT |
2885 | print "<td class=\"list\">"; |
2886 | print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'}, | |
fa702003 | 2887 | file_name=>"$basedir$t->{'name'}", %base_key)}, |
403d0906 | 2888 | esc_path($t->{'name'})); |
0fa105e7 LT |
2889 | print "</td>\n"; |
2890 | print "<td class=\"link\">"; | |
4777b014 | 2891 | print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'}, |
e33fba4c JN |
2892 | file_name=>"$basedir$t->{'name'}", %base_key)}, |
2893 | "tree"); | |
fa702003 | 2894 | if (defined $hash_base) { |
4777b014 PB |
2895 | print " | " . |
2896 | $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, | |
fa702003 | 2897 | file_name=>"$basedir$t->{'name'}")}, |
01ac1e38 JN |
2898 | "history"); |
2899 | } | |
2900 | print "</td>\n"; | |
2901 | } else { | |
2902 | # unknown object: we can only present history for it | |
2903 | # (this includes 'commit' object, i.e. submodule support) | |
2904 | print "<td class=\"list\">" . | |
2905 | esc_path($t->{'name'}) . | |
2906 | "</td>\n"; | |
2907 | print "<td class=\"link\">"; | |
2908 | if (defined $hash_base) { | |
2909 | print $cgi->a({-href => href(action=>"history", | |
2910 | hash_base=>$hash_base, | |
2911 | file_name=>"$basedir$t->{'name'}")}, | |
fa702003 JN |
2912 | "history"); |
2913 | } | |
2914 | print "</td>\n"; | |
2915 | } | |
2916 | } | |
2917 | ||
717b8311 JN |
2918 | ## ...................................................................... |
2919 | ## functions printing large fragments of HTML | |
2920 | ||
0cec6db5 | 2921 | # get pre-image filenames for merge (combined) diff |
e72c0eaf JN |
2922 | sub fill_from_file_info { |
2923 | my ($diff, @parents) = @_; | |
2924 | ||
2925 | $diff->{'from_file'} = [ ]; | |
2926 | $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef; | |
2927 | for (my $i = 0; $i < $diff->{'nparents'}; $i++) { | |
2928 | if ($diff->{'status'}[$i] eq 'R' || | |
2929 | $diff->{'status'}[$i] eq 'C') { | |
2930 | $diff->{'from_file'}[$i] = | |
2931 | git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]); | |
2932 | } | |
2933 | } | |
2934 | ||
2935 | return $diff; | |
2936 | } | |
2937 | ||
0cec6db5 | 2938 | # is current raw difftree line of file deletion |
90921740 JN |
2939 | sub is_deleted { |
2940 | my $diffinfo = shift; | |
2941 | ||
6aa6f92f | 2942 | return $diffinfo->{'status_str'} =~ /D/; |
90921740 | 2943 | } |
e72c0eaf | 2944 | |
0cec6db5 JN |
2945 | # does patch correspond to [previous] difftree raw line |
2946 | # $diffinfo - hashref of parsed raw diff format | |
2947 | # $patchinfo - hashref of parsed patch diff format | |
2948 | # (the same keys as in $diffinfo) | |
2949 | sub is_patch_split { | |
2950 | my ($diffinfo, $patchinfo) = @_; | |
2951 | ||
2952 | return defined $diffinfo && defined $patchinfo | |
9d301456 | 2953 | && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'}; |
0cec6db5 JN |
2954 | } |
2955 | ||
2956 | ||
4a4a1a53 | 2957 | sub git_difftree_body { |
ed224dea JN |
2958 | my ($difftree, $hash, @parents) = @_; |
2959 | my ($parent) = $parents[0]; | |
2b2a8c78 | 2960 | my ($have_blame) = gitweb_check_feature('blame'); |
4a4a1a53 JN |
2961 | print "<div class=\"list_head\">\n"; |
2962 | if ($#{$difftree} > 10) { | |
2963 | print(($#{$difftree} + 1) . " files changed:\n"); | |
2964 | } | |
2965 | print "</div>\n"; | |
2966 | ||
ed224dea JN |
2967 | print "<table class=\"" . |
2968 | (@parents > 1 ? "combined " : "") . | |
2969 | "diff_tree\">\n"; | |
47598d7a JN |
2970 | |
2971 | # header only for combined diff in 'commitdiff' view | |
3ef408ae | 2972 | my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff'; |
47598d7a JN |
2973 | if ($has_header) { |
2974 | # table header | |
2975 | print "<thead><tr>\n" . | |
2976 | "<th></th><th></th>\n"; # filename, patchN link | |
2977 | for (my $i = 0; $i < @parents; $i++) { | |
2978 | my $par = $parents[$i]; | |
2979 | print "<th>" . | |
2980 | $cgi->a({-href => href(action=>"commitdiff", | |
2981 | hash=>$hash, hash_parent=>$par), | |
2982 | -title => 'commitdiff to parent number ' . | |
2983 | ($i+1) . ': ' . substr($par,0,7)}, | |
2984 | $i+1) . | |
2985 | " </th>\n"; | |
2986 | } | |
2987 | print "</tr></thead>\n<tbody>\n"; | |
2988 | } | |
2989 | ||
6dd36acd | 2990 | my $alternate = 1; |
b4657e77 | 2991 | my $patchno = 0; |
4a4a1a53 | 2992 | foreach my $line (@{$difftree}) { |
0cec6db5 | 2993 | my $diff = parsed_difftree_line($line); |
4a4a1a53 JN |
2994 | |
2995 | if ($alternate) { | |
2996 | print "<tr class=\"dark\">\n"; | |
2997 | } else { | |
2998 | print "<tr class=\"light\">\n"; | |
2999 | } | |
3000 | $alternate ^= 1; | |
3001 | ||
493e01db | 3002 | if (exists $diff->{'nparents'}) { # combined diff |
ed224dea | 3003 | |
493e01db JN |
3004 | fill_from_file_info($diff, @parents) |
3005 | unless exists $diff->{'from_file'}; | |
e72c0eaf | 3006 | |
90921740 | 3007 | if (!is_deleted($diff)) { |
ed224dea JN |
3008 | # file exists in the result (child) commit |
3009 | print "<td>" . | |
493e01db JN |
3010 | $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3011 | file_name=>$diff->{'to_file'}, | |
ed224dea | 3012 | hash_base=>$hash), |
493e01db | 3013 | -class => "list"}, esc_path($diff->{'to_file'})) . |
ed224dea JN |
3014 | "</td>\n"; |
3015 | } else { | |
3016 | print "<td>" . | |
493e01db | 3017 | esc_path($diff->{'to_file'}) . |
ed224dea JN |
3018 | "</td>\n"; |
3019 | } | |
3020 | ||
3021 | if ($action eq 'commitdiff') { | |
3022 | # link to patch | |
3023 | $patchno++; | |
3024 | print "<td class=\"link\">" . | |
3025 | $cgi->a({-href => "#patch$patchno"}, "patch") . | |
3026 | " | " . | |
3027 | "</td>\n"; | |
3028 | } | |
3029 | ||
3030 | my $has_history = 0; | |
3031 | my $not_deleted = 0; | |
493e01db | 3032 | for (my $i = 0; $i < $diff->{'nparents'}; $i++) { |
ed224dea | 3033 | my $hash_parent = $parents[$i]; |
493e01db JN |
3034 | my $from_hash = $diff->{'from_id'}[$i]; |
3035 | my $from_path = $diff->{'from_file'}[$i]; | |
3036 | my $status = $diff->{'status'}[$i]; | |
ed224dea JN |
3037 | |
3038 | $has_history ||= ($status ne 'A'); | |
3039 | $not_deleted ||= ($status ne 'D'); | |
3040 | ||
ed224dea JN |
3041 | if ($status eq 'A') { |
3042 | print "<td class=\"link\" align=\"right\"> | </td>\n"; | |
3043 | } elsif ($status eq 'D') { | |
3044 | print "<td class=\"link\">" . | |
3045 | $cgi->a({-href => href(action=>"blob", | |
3046 | hash_base=>$hash, | |
3047 | hash=>$from_hash, | |
3048 | file_name=>$from_path)}, | |
3049 | "blob" . ($i+1)) . | |
3050 | " | </td>\n"; | |
3051 | } else { | |
493e01db | 3052 | if ($diff->{'to_id'} eq $from_hash) { |
ed224dea JN |
3053 | print "<td class=\"link nochange\">"; |
3054 | } else { | |
3055 | print "<td class=\"link\">"; | |
3056 | } | |
3057 | print $cgi->a({-href => href(action=>"blobdiff", | |
493e01db | 3058 | hash=>$diff->{'to_id'}, |
ed224dea JN |
3059 | hash_parent=>$from_hash, |
3060 | hash_base=>$hash, | |
3061 | hash_parent_base=>$hash_parent, | |
493e01db | 3062 | file_name=>$diff->{'to_file'}, |
ed224dea JN |
3063 | file_parent=>$from_path)}, |
3064 | "diff" . ($i+1)) . | |
3065 | " | </td>\n"; | |
3066 | } | |
3067 | } | |
3068 | ||
3069 | print "<td class=\"link\">"; | |
3070 | if ($not_deleted) { | |
3071 | print $cgi->a({-href => href(action=>"blob", | |
493e01db JN |
3072 | hash=>$diff->{'to_id'}, |
3073 | file_name=>$diff->{'to_file'}, | |
ed224dea JN |
3074 | hash_base=>$hash)}, |
3075 | "blob"); | |
3076 | print " | " if ($has_history); | |
3077 | } | |
3078 | if ($has_history) { | |
3079 | print $cgi->a({-href => href(action=>"history", | |
493e01db | 3080 | file_name=>$diff->{'to_file'}, |
ed224dea JN |
3081 | hash_base=>$hash)}, |
3082 | "history"); | |
3083 | } | |
3084 | print "</td>\n"; | |
3085 | ||
3086 | print "</tr>\n"; | |
3087 | next; # instead of 'else' clause, to avoid extra indent | |
3088 | } | |
3089 | # else ordinary diff | |
3090 | ||
e8e41a93 JN |
3091 | my ($to_mode_oct, $to_mode_str, $to_file_type); |
3092 | my ($from_mode_oct, $from_mode_str, $from_file_type); | |
493e01db JN |
3093 | if ($diff->{'to_mode'} ne ('0' x 6)) { |
3094 | $to_mode_oct = oct $diff->{'to_mode'}; | |
e8e41a93 JN |
3095 | if (S_ISREG($to_mode_oct)) { # only for regular file |
3096 | $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits | |
3097 | } | |
493e01db | 3098 | $to_file_type = file_type($diff->{'to_mode'}); |
e8e41a93 | 3099 | } |
493e01db JN |
3100 | if ($diff->{'from_mode'} ne ('0' x 6)) { |
3101 | $from_mode_oct = oct $diff->{'from_mode'}; | |
e8e41a93 JN |
3102 | if (S_ISREG($to_mode_oct)) { # only for regular file |
3103 | $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits | |
4a4a1a53 | 3104 | } |
493e01db | 3105 | $from_file_type = file_type($diff->{'from_mode'}); |
e8e41a93 JN |
3106 | } |
3107 | ||
493e01db | 3108 | if ($diff->{'status'} eq "A") { # created |
e8e41a93 JN |
3109 | my $mode_chng = "<span class=\"file_status new\">[new $to_file_type"; |
3110 | $mode_chng .= " with mode: $to_mode_str" if $to_mode_str; | |
3111 | $mode_chng .= "]</span>"; | |
499faeda | 3112 | print "<td>"; |
493e01db JN |
3113 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3114 | hash_base=>$hash, file_name=>$diff->{'file'}), | |
3115 | -class => "list"}, esc_path($diff->{'file'})); | |
499faeda LT |
3116 | print "</td>\n"; |
3117 | print "<td>$mode_chng</td>\n"; | |
3118 | print "<td class=\"link\">"; | |
72dbafa1 | 3119 | if ($action eq 'commitdiff') { |
b4657e77 JN |
3120 | # link to patch |
3121 | $patchno++; | |
499faeda | 3122 | print $cgi->a({-href => "#patch$patchno"}, "patch"); |
897d1d2e | 3123 | print " | "; |
b4657e77 | 3124 | } |
493e01db JN |
3125 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3126 | hash_base=>$hash, file_name=>$diff->{'file'})}, | |
3faa541f | 3127 | "blob"); |
b4657e77 | 3128 | print "</td>\n"; |
4a4a1a53 | 3129 | |
493e01db | 3130 | } elsif ($diff->{'status'} eq "D") { # deleted |
e8e41a93 | 3131 | my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>"; |
499faeda | 3132 | print "<td>"; |
493e01db JN |
3133 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'}, |
3134 | hash_base=>$parent, file_name=>$diff->{'file'}), | |
3135 | -class => "list"}, esc_path($diff->{'file'})); | |
499faeda LT |
3136 | print "</td>\n"; |
3137 | print "<td>$mode_chng</td>\n"; | |
3138 | print "<td class=\"link\">"; | |
72dbafa1 | 3139 | if ($action eq 'commitdiff') { |
b4657e77 JN |
3140 | # link to patch |
3141 | $patchno++; | |
499faeda LT |
3142 | print $cgi->a({-href => "#patch$patchno"}, "patch"); |
3143 | print " | "; | |
b4657e77 | 3144 | } |
493e01db JN |
3145 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'}, |
3146 | hash_base=>$parent, file_name=>$diff->{'file'})}, | |
897d1d2e | 3147 | "blob") . " | "; |
2b2a8c78 | 3148 | if ($have_blame) { |
897d1d2e | 3149 | print $cgi->a({-href => href(action=>"blame", hash_base=>$parent, |
493e01db | 3150 | file_name=>$diff->{'file'})}, |
897d1d2e | 3151 | "blame") . " | "; |
2b2a8c78 | 3152 | } |
b4657e77 | 3153 | print $cgi->a({-href => href(action=>"history", hash_base=>$parent, |
493e01db | 3154 | file_name=>$diff->{'file'})}, |
e7fb022a | 3155 | "history"); |
499faeda | 3156 | print "</td>\n"; |
4a4a1a53 | 3157 | |
493e01db | 3158 | } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed |
4a4a1a53 | 3159 | my $mode_chnge = ""; |
493e01db | 3160 | if ($diff->{'from_mode'} != $diff->{'to_mode'}) { |
e8e41a93 | 3161 | $mode_chnge = "<span class=\"file_status mode_chnge\">[changed"; |
6e72cf43 | 3162 | if ($from_file_type ne $to_file_type) { |
e8e41a93 | 3163 | $mode_chnge .= " from $from_file_type to $to_file_type"; |
4a4a1a53 | 3164 | } |
e8e41a93 JN |
3165 | if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) { |
3166 | if ($from_mode_str && $to_mode_str) { | |
3167 | $mode_chnge .= " mode: $from_mode_str->$to_mode_str"; | |
3168 | } elsif ($to_mode_str) { | |
3169 | $mode_chnge .= " mode: $to_mode_str"; | |
4a4a1a53 JN |
3170 | } |
3171 | } | |
3172 | $mode_chnge .= "]</span>\n"; | |
3173 | } | |
3174 | print "<td>"; | |
493e01db JN |
3175 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3176 | hash_base=>$hash, file_name=>$diff->{'file'}), | |
3177 | -class => "list"}, esc_path($diff->{'file'})); | |
499faeda LT |
3178 | print "</td>\n"; |
3179 | print "<td>$mode_chnge</td>\n"; | |
3180 | print "<td class=\"link\">"; | |
241cc599 JN |
3181 | if ($action eq 'commitdiff') { |
3182 | # link to patch | |
3183 | $patchno++; | |
3184 | print $cgi->a({-href => "#patch$patchno"}, "patch") . | |
3185 | " | "; | |
493e01db | 3186 | } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) { |
241cc599 JN |
3187 | # "commit" view and modified file (not onlu mode changed) |
3188 | print $cgi->a({-href => href(action=>"blobdiff", | |
493e01db | 3189 | hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'}, |
241cc599 | 3190 | hash_base=>$hash, hash_parent_base=>$parent, |
493e01db | 3191 | file_name=>$diff->{'file'})}, |
241cc599 JN |
3192 | "diff") . |
3193 | " | "; | |
4a4a1a53 | 3194 | } |
493e01db JN |
3195 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3196 | hash_base=>$hash, file_name=>$diff->{'file'})}, | |
897d1d2e | 3197 | "blob") . " | "; |
2b2a8c78 | 3198 | if ($have_blame) { |
897d1d2e | 3199 | print $cgi->a({-href => href(action=>"blame", hash_base=>$hash, |
493e01db | 3200 | file_name=>$diff->{'file'})}, |
897d1d2e | 3201 | "blame") . " | "; |
2b2a8c78 | 3202 | } |
eb51ec9c | 3203 | print $cgi->a({-href => href(action=>"history", hash_base=>$hash, |
493e01db | 3204 | file_name=>$diff->{'file'})}, |
e7fb022a | 3205 | "history"); |
4a4a1a53 JN |
3206 | print "</td>\n"; |
3207 | ||
493e01db | 3208 | } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied |
e8e41a93 | 3209 | my %status_name = ('R' => 'moved', 'C' => 'copied'); |
493e01db | 3210 | my $nstatus = $status_name{$diff->{'status'}}; |
4a4a1a53 | 3211 | my $mode_chng = ""; |
493e01db | 3212 | if ($diff->{'from_mode'} != $diff->{'to_mode'}) { |
e8e41a93 JN |
3213 | # mode also for directories, so we cannot use $to_mode_str |
3214 | $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777); | |
4a4a1a53 JN |
3215 | } |
3216 | print "<td>" . | |
e8e41a93 | 3217 | $cgi->a({-href => href(action=>"blob", hash_base=>$hash, |
493e01db JN |
3218 | hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}), |
3219 | -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" . | |
e8e41a93 JN |
3220 | "<td><span class=\"file_status $nstatus\">[$nstatus from " . |
3221 | $cgi->a({-href => href(action=>"blob", hash_base=>$parent, | |
493e01db JN |
3222 | hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}), |
3223 | -class => "list"}, esc_path($diff->{'from_file'})) . | |
3224 | " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" . | |
499faeda | 3225 | "<td class=\"link\">"; |
241cc599 JN |
3226 | if ($action eq 'commitdiff') { |
3227 | # link to patch | |
3228 | $patchno++; | |
3229 | print $cgi->a({-href => "#patch$patchno"}, "patch") . | |
3230 | " | "; | |
493e01db | 3231 | } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) { |
241cc599 JN |
3232 | # "commit" view and modified file (not only pure rename or copy) |
3233 | print $cgi->a({-href => href(action=>"blobdiff", | |
493e01db | 3234 | hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'}, |
241cc599 | 3235 | hash_base=>$hash, hash_parent_base=>$parent, |
493e01db | 3236 | file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})}, |
241cc599 JN |
3237 | "diff") . |
3238 | " | "; | |
4a4a1a53 | 3239 | } |
493e01db JN |
3240 | print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'}, |
3241 | hash_base=>$parent, file_name=>$diff->{'to_file'})}, | |
897d1d2e | 3242 | "blob") . " | "; |
2b2a8c78 | 3243 | if ($have_blame) { |
897d1d2e | 3244 | print $cgi->a({-href => href(action=>"blame", hash_base=>$hash, |
493e01db | 3245 | file_name=>$diff->{'to_file'})}, |
897d1d2e | 3246 | "blame") . " | "; |
2b2a8c78 | 3247 | } |
897d1d2e | 3248 | print $cgi->a({-href => href(action=>"history", hash_base=>$hash, |
493e01db | 3249 | file_name=>$diff->{'to_file'})}, |
e7fb022a | 3250 | "history"); |
4a4a1a53 | 3251 | print "</td>\n"; |
e8e41a93 | 3252 | |
4a4a1a53 JN |
3253 | } # we should not encounter Unmerged (U) or Unknown (X) status |
3254 | print "</tr>\n"; | |
3255 | } | |
47598d7a | 3256 | print "</tbody>" if $has_header; |
4a4a1a53 JN |
3257 | print "</table>\n"; |
3258 | } | |
3259 | ||
eee08903 | 3260 | sub git_patchset_body { |
e72c0eaf JN |
3261 | my ($fd, $difftree, $hash, @hash_parents) = @_; |
3262 | my ($hash_parent) = $hash_parents[0]; | |
eee08903 | 3263 | |
0cec6db5 | 3264 | my $is_combined = (@hash_parents > 1); |
eee08903 | 3265 | my $patch_idx = 0; |
4280cde9 | 3266 | my $patch_number = 0; |
6d55f055 | 3267 | my $patch_line; |
fe87585e | 3268 | my $diffinfo; |
0cec6db5 | 3269 | my $to_name; |
744d0ac3 | 3270 | my (%from, %to); |
eee08903 JN |
3271 | |
3272 | print "<div class=\"patchset\">\n"; | |
3273 | ||
6d55f055 JN |
3274 | # skip to first patch |
3275 | while ($patch_line = <$fd>) { | |
157e43b4 | 3276 | chomp $patch_line; |
eee08903 | 3277 | |
6d55f055 JN |
3278 | last if ($patch_line =~ m/^diff /); |
3279 | } | |
3280 | ||
3281 | PATCH: | |
3282 | while ($patch_line) { | |
6d55f055 | 3283 | |
0cec6db5 JN |
3284 | # parse "git diff" header line |
3285 | if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) { | |
3286 | # $1 is from_name, which we do not use | |
3287 | $to_name = unquote($2); | |
3288 | $to_name =~ s!^b/!!; | |
3289 | } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) { | |
3290 | # $1 is 'cc' or 'combined', which we do not use | |
3291 | $to_name = unquote($2); | |
3292 | } else { | |
3293 | $to_name = undef; | |
6d55f055 | 3294 | } |
6d55f055 JN |
3295 | |
3296 | # check if current patch belong to current raw line | |
3297 | # and parse raw git-diff line if needed | |
0cec6db5 | 3298 | if (is_patch_split($diffinfo, { 'to_file' => $to_name })) { |
2206537c | 3299 | # this is continuation of a split patch |
6d55f055 JN |
3300 | print "<div class=\"patch cont\">\n"; |
3301 | } else { | |
3302 | # advance raw git-diff output if needed | |
3303 | $patch_idx++ if defined $diffinfo; | |
eee08903 | 3304 | |
0cec6db5 JN |
3305 | # read and prepare patch information |
3306 | $diffinfo = parsed_difftree_line($difftree->[$patch_idx]); | |
cd030c3a | 3307 | |
0cec6db5 JN |
3308 | # compact combined diff output can have some patches skipped |
3309 | # find which patch (using pathname of result) we are at now; | |
3310 | if ($is_combined) { | |
3311 | while ($to_name ne $diffinfo->{'to_file'}) { | |
cd030c3a JN |
3312 | print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" . |
3313 | format_diff_cc_simplified($diffinfo, @hash_parents) . | |
3314 | "</div>\n"; # class="patch" | |
3315 | ||
3316 | $patch_idx++; | |
3317 | $patch_number++; | |
0cec6db5 JN |
3318 | |
3319 | last if $patch_idx > $#$difftree; | |
3320 | $diffinfo = parsed_difftree_line($difftree->[$patch_idx]); | |
cd030c3a | 3321 | } |
0cec6db5 | 3322 | } |
711fa742 | 3323 | |
90921740 JN |
3324 | # modifies %from, %to hashes |
3325 | parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents); | |
5f855052 | 3326 | |
6d55f055 JN |
3327 | # this is first patch for raw difftree line with $patch_idx index |
3328 | # we index @$difftree array from 0, but number patches from 1 | |
3329 | print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n"; | |
744d0ac3 | 3330 | } |
eee08903 | 3331 | |
0cec6db5 JN |
3332 | # git diff header |
3333 | #assert($patch_line =~ m/^diff /) if DEBUG; | |
3334 | #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed | |
3335 | $patch_number++; | |
6d55f055 | 3336 | # print "git diff" header |
90921740 JN |
3337 | print format_git_diff_header_line($patch_line, $diffinfo, |
3338 | \%from, \%to); | |
6d55f055 JN |
3339 | |
3340 | # print extended diff header | |
0cec6db5 | 3341 | print "<div class=\"diff extended_header\">\n"; |
6d55f055 | 3342 | EXTENDED_HEADER: |
0cec6db5 JN |
3343 | while ($patch_line = <$fd>) { |
3344 | chomp $patch_line; | |
3345 | ||
3346 | last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /); | |
3347 | ||
90921740 JN |
3348 | print format_extended_diff_header_line($patch_line, $diffinfo, |
3349 | \%from, \%to); | |
6d55f055 | 3350 | } |
0cec6db5 | 3351 | print "</div>\n"; # class="diff extended_header" |
6d55f055 JN |
3352 | |
3353 | # from-file/to-file diff header | |
0bdb28c9 JN |
3354 | if (! $patch_line) { |
3355 | print "</div>\n"; # class="patch" | |
3356 | last PATCH; | |
3357 | } | |
66399eff | 3358 | next PATCH if ($patch_line =~ m/^diff /); |
6d55f055 | 3359 | #assert($patch_line =~ m/^---/) if DEBUG; |
744d0ac3 | 3360 | |
0cec6db5 | 3361 | my $last_patch_line = $patch_line; |
6d55f055 | 3362 | $patch_line = <$fd>; |
6d55f055 | 3363 | chomp $patch_line; |
90921740 | 3364 | #assert($patch_line =~ m/^\+\+\+/) if DEBUG; |
e4e4f825 | 3365 | |
90921740 | 3366 | print format_diff_from_to_header($last_patch_line, $patch_line, |
91af4ce4 JN |
3367 | $diffinfo, \%from, \%to, |
3368 | @hash_parents); | |
e4e4f825 | 3369 | |
6d55f055 JN |
3370 | # the patch itself |
3371 | LINE: | |
3372 | while ($patch_line = <$fd>) { | |
3373 | chomp $patch_line; | |
e4e4f825 | 3374 | |
6d55f055 | 3375 | next PATCH if ($patch_line =~ m/^diff /); |
e4e4f825 | 3376 | |
59e3b14e | 3377 | print format_diff_line($patch_line, \%from, \%to); |
eee08903 | 3378 | } |
eee08903 | 3379 | |
6d55f055 JN |
3380 | } continue { |
3381 | print "</div>\n"; # class="patch" | |
eee08903 | 3382 | } |
d26c4264 | 3383 | |
cd030c3a JN |
3384 | # for compact combined (--cc) format, with chunk and patch simpliciaction |
3385 | # patchset might be empty, but there might be unprocessed raw lines | |
0cec6db5 | 3386 | for (++$patch_idx if $patch_number > 0; |
cd030c3a | 3387 | $patch_idx < @$difftree; |
0cec6db5 | 3388 | ++$patch_idx) { |
cd030c3a | 3389 | # read and prepare patch information |
0cec6db5 | 3390 | $diffinfo = parsed_difftree_line($difftree->[$patch_idx]); |
cd030c3a JN |
3391 | |
3392 | # generate anchor for "patch" links in difftree / whatchanged part | |
3393 | print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" . | |
3394 | format_diff_cc_simplified($diffinfo, @hash_parents) . | |
3395 | "</div>\n"; # class="patch" | |
3396 | ||
3397 | $patch_number++; | |
3398 | } | |
3399 | ||
d26c4264 JN |
3400 | if ($patch_number == 0) { |
3401 | if (@hash_parents > 1) { | |
3402 | print "<div class=\"diff nodifferences\">Trivial merge</div>\n"; | |
3403 | } else { | |
3404 | print "<div class=\"diff nodifferences\">No differences found</div>\n"; | |
3405 | } | |
3406 | } | |
eee08903 JN |
3407 | |
3408 | print "</div>\n"; # class="patchset" | |
3409 | } | |
3410 | ||
3411 | # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |
3412 | ||
e30496df PB |
3413 | sub git_project_list_body { |
3414 | my ($projlist, $order, $from, $to, $extra, $no_header) = @_; | |
3415 | ||
5dd5ed09 | 3416 | my ($check_forks) = gitweb_check_feature('forks'); |
e30496df PB |
3417 | |
3418 | my @projects; | |
3419 | foreach my $pr (@$projlist) { | |
3420 | my (@aa) = git_get_last_activity($pr->{'path'}); | |
3421 | unless (@aa) { | |
3422 | next; | |
3423 | } | |
3424 | ($pr->{'age'}, $pr->{'age_string'}) = @aa; | |
3425 | if (!defined $pr->{'descr'}) { | |
3426 | my $descr = git_get_project_description($pr->{'path'}) || ""; | |
00f429af | 3427 | $pr->{'descr_long'} = to_utf8($descr); |
55feb120 | 3428 | $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5); |
e30496df PB |
3429 | } |
3430 | if (!defined $pr->{'owner'}) { | |
76e4f5d0 | 3431 | $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || ""; |
e30496df PB |
3432 | } |
3433 | if ($check_forks) { | |
3434 | my $pname = $pr->{'path'}; | |
83ee94c1 JH |
3435 | if (($pname =~ s/\.git$//) && |
3436 | ($pname !~ /\/$/) && | |
3437 | (-d "$projectroot/$pname")) { | |
3438 | $pr->{'forks'} = "-d $projectroot/$pname"; | |
3439 | } | |
3440 | else { | |
3441 | $pr->{'forks'} = 0; | |
3442 | } | |
e30496df PB |
3443 | } |
3444 | push @projects, $pr; | |
3445 | } | |
3446 | ||
b06dcf8c | 3447 | $order ||= $default_projects_order; |
e30496df PB |
3448 | $from = 0 unless defined $from; |
3449 | $to = $#projects if (!defined $to || $#projects < $to); | |
3450 | ||
3451 | print "<table class=\"project_list\">\n"; | |
3452 | unless ($no_header) { | |
3453 | print "<tr>\n"; | |
3454 | if ($check_forks) { | |
3455 | print "<th></th>\n"; | |
3456 | } | |
3457 | if ($order eq "project") { | |
3458 | @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects; | |
3459 | print "<th>Project</th>\n"; | |
3460 | } else { | |
3461 | print "<th>" . | |
3462 | $cgi->a({-href => href(project=>undef, order=>'project'), | |
e88ce8a4 | 3463 | -class => "header"}, "Project") . |
e30496df PB |
3464 | "</th>\n"; |
3465 | } | |
3466 | if ($order eq "descr") { | |
3467 | @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects; | |
3468 | print "<th>Description</th>\n"; | |
3469 | } else { | |
3470 | print "<th>" . | |
3471 | $cgi->a({-href => href(project=>undef, order=>'descr'), | |
e88ce8a4 | 3472 | -class => "header"}, "Description") . |
e30496df PB |
3473 | "</th>\n"; |
3474 | } | |
3475 | if ($order eq "owner") { | |
3476 | @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects; | |
3477 | print "<th>Owner</th>\n"; | |
3478 | } else { | |
3479 | print "<th>" . | |
3480 | $cgi->a({-href => href(project=>undef, order=>'owner'), | |
e88ce8a4 | 3481 | -class => "header"}, "Owner") . |
e30496df PB |
3482 | "</th>\n"; |
3483 | } | |
3484 | if ($order eq "age") { | |
3485 | @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects; | |
3486 | print "<th>Last Change</th>\n"; | |
3487 | } else { | |
3488 | print "<th>" . | |
3489 | $cgi->a({-href => href(project=>undef, order=>'age'), | |
e88ce8a4 | 3490 | -class => "header"}, "Last Change") . |
e30496df PB |
3491 | "</th>\n"; |
3492 | } | |
3493 | print "<th></th>\n" . | |
3494 | "</tr>\n"; | |
3495 | } | |
3496 | my $alternate = 1; | |
3497 | for (my $i = $from; $i <= $to; $i++) { | |
3498 | my $pr = $projects[$i]; | |
3499 | if ($alternate) { | |
3500 | print "<tr class=\"dark\">\n"; | |
3501 | } else { | |
3502 | print "<tr class=\"light\">\n"; | |
3503 | } | |
3504 | $alternate ^= 1; | |
3505 | if ($check_forks) { | |
3506 | print "<td>"; | |
3507 | if ($pr->{'forks'}) { | |
83ee94c1 | 3508 | print "<!-- $pr->{'forks'} -->\n"; |
e30496df PB |
3509 | print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+"); |
3510 | } | |
3511 | print "</td>\n"; | |
3512 | } | |
3513 | print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"), | |
3514 | -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" . | |
e88ce8a4 JN |
3515 | "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"), |
3516 | -class => "list", -title => $pr->{'descr_long'}}, | |
3517 | esc_html($pr->{'descr'})) . "</td>\n" . | |
d3cd2495 | 3518 | "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n"; |
e30496df | 3519 | print "<td class=\"". age_class($pr->{'age'}) . "\">" . |
785cdea9 | 3520 | (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" . |
e30496df PB |
3521 | "<td class=\"link\">" . |
3522 | $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " . | |
faa1bbfd | 3523 | $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " . |
e30496df PB |
3524 | $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " . |
3525 | $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") . | |
3526 | ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') . | |
3527 | "</td>\n" . | |
3528 | "</tr>\n"; | |
3529 | } | |
3530 | if (defined $extra) { | |
3531 | print "<tr>\n"; | |
3532 | if ($check_forks) { | |
3533 | print "<td></td>\n"; | |
3534 | } | |
3535 | print "<td colspan=\"5\">$extra</td>\n" . | |
3536 | "</tr>\n"; | |
3537 | } | |
3538 | print "</table>\n"; | |
3539 | } | |
3540 | ||
9f5dcb81 JN |
3541 | sub git_shortlog_body { |
3542 | # uses global variable $project | |
190d7fdc | 3543 | my ($commitlist, $from, $to, $refs, $extra) = @_; |
ddb8d900 | 3544 | |
9f5dcb81 | 3545 | $from = 0 unless defined $from; |
190d7fdc | 3546 | $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to); |
9f5dcb81 | 3547 | |
591ebf65 | 3548 | print "<table class=\"shortlog\">\n"; |
6dd36acd | 3549 | my $alternate = 1; |
9f5dcb81 | 3550 | for (my $i = $from; $i <= $to; $i++) { |
190d7fdc RF |
3551 | my %co = %{$commitlist->[$i]}; |
3552 | my $commit = $co{'id'}; | |
847e01fb | 3553 | my $ref = format_ref_marker($refs, $commit); |
9f5dcb81 JN |
3554 | if ($alternate) { |
3555 | print "<tr class=\"dark\">\n"; | |
3556 | } else { | |
3557 | print "<tr class=\"light\">\n"; | |
3558 | } | |
3559 | $alternate ^= 1; | |
ce58ec91 | 3560 | my $author = chop_and_escape_str($co{'author_name'}, 10); |
9f5dcb81 JN |
3561 | # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" . |
3562 | print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" . | |
e076a0e7 | 3563 | "<td><i>" . $author . "</i></td>\n" . |
9f5dcb81 | 3564 | "<td>"; |
952c65fc JN |
3565 | print format_subject_html($co{'title'}, $co{'title_short'}, |
3566 | href(action=>"commit", hash=>$commit), $ref); | |
9f5dcb81 JN |
3567 | print "</td>\n" . |
3568 | "<td class=\"link\">" . | |
4777b014 | 3569 | $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " . |
35749ae5 | 3570 | $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " . |
55ff35cb | 3571 | $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree"); |
a3c8ab30 MM |
3572 | my $snapshot_links = format_snapshot_links($commit); |
3573 | if (defined $snapshot_links) { | |
3574 | print " | " . $snapshot_links; | |
55ff35cb | 3575 | } |
cb9c6e5b | 3576 | print "</td>\n" . |
9f5dcb81 JN |
3577 | "</tr>\n"; |
3578 | } | |
3579 | if (defined $extra) { | |
3580 | print "<tr>\n" . | |
3581 | "<td colspan=\"4\">$extra</td>\n" . | |
3582 | "</tr>\n"; | |
3583 | } | |
3584 | print "</table>\n"; | |
3585 | } | |
3586 | ||
581860e1 JN |
3587 | sub git_history_body { |
3588 | # Warning: assumes constant type (blob or tree) during history | |
a8b983bf | 3589 | my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_; |
8be68352 JN |
3590 | |
3591 | $from = 0 unless defined $from; | |
a8b983bf | 3592 | $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist}); |
581860e1 | 3593 | |
591ebf65 | 3594 | print "<table class=\"history\">\n"; |
6dd36acd | 3595 | my $alternate = 1; |
8be68352 | 3596 | for (my $i = $from; $i <= $to; $i++) { |
a8b983bf | 3597 | my %co = %{$commitlist->[$i]}; |
581860e1 JN |
3598 | if (!%co) { |
3599 | next; | |
3600 | } | |
a8b983bf | 3601 | my $commit = $co{'id'}; |
581860e1 JN |
3602 | |
3603 | my $ref = format_ref_marker($refs, $commit); | |
3604 | ||
3605 | if ($alternate) { | |
3606 | print "<tr class=\"dark\">\n"; | |
3607 | } else { | |
3608 | print "<tr class=\"light\">\n"; | |
3609 | } | |
3610 | $alternate ^= 1; | |
e076a0e7 | 3611 | # shortlog uses chop_str($co{'author_name'}, 10) |
ce58ec91 | 3612 | my $author = chop_and_escape_str($co{'author_name'}, 15, 3); |
581860e1 | 3613 | print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" . |
e076a0e7 | 3614 | "<td><i>" . $author . "</i></td>\n" . |
581860e1 JN |
3615 | "<td>"; |
3616 | # originally git_history used chop_str($co{'title'}, 50) | |
952c65fc JN |
3617 | print format_subject_html($co{'title'}, $co{'title_short'}, |
3618 | href(action=>"commit", hash=>$commit), $ref); | |
581860e1 JN |
3619 | print "</td>\n" . |
3620 | "<td class=\"link\">" . | |
6d81c5a2 LT |
3621 | $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " . |
3622 | $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff"); | |
581860e1 JN |
3623 | |
3624 | if ($ftype eq 'blob') { | |
3625 | my $blob_current = git_get_hash_by_path($hash_base, $file_name); | |
3626 | my $blob_parent = git_get_hash_by_path($commit, $file_name); | |
3627 | if (defined $blob_current && defined $blob_parent && | |
3628 | $blob_current ne $blob_parent) { | |
3629 | print " | " . | |
420e92f2 JN |
3630 | $cgi->a({-href => href(action=>"blobdiff", |
3631 | hash=>$blob_current, hash_parent=>$blob_parent, | |
3632 | hash_base=>$hash_base, hash_parent_base=>$commit, | |
3633 | file_name=>$file_name)}, | |
581860e1 JN |
3634 | "diff to current"); |
3635 | } | |
3636 | } | |
3637 | print "</td>\n" . | |
3638 | "</tr>\n"; | |
3639 | } | |
3640 | if (defined $extra) { | |
3641 | print "<tr>\n" . | |
3642 | "<td colspan=\"4\">$extra</td>\n" . | |
3643 | "</tr>\n"; | |
3644 | } | |
3645 | print "</table>\n"; | |
3646 | } | |
3647 | ||
717b8311 JN |
3648 | sub git_tags_body { |
3649 | # uses global variable $project | |
3650 | my ($taglist, $from, $to, $extra) = @_; | |
3651 | $from = 0 unless defined $from; | |
3652 | $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to); | |
3653 | ||
591ebf65 | 3654 | print "<table class=\"tags\">\n"; |
6dd36acd | 3655 | my $alternate = 1; |
717b8311 JN |
3656 | for (my $i = $from; $i <= $to; $i++) { |
3657 | my $entry = $taglist->[$i]; | |
3658 | my %tag = %$entry; | |
cd146408 | 3659 | my $comment = $tag{'subject'}; |
717b8311 JN |
3660 | my $comment_short; |
3661 | if (defined $comment) { | |
3662 | $comment_short = chop_str($comment, 30, 5); | |
3663 | } | |
3664 | if ($alternate) { | |
3665 | print "<tr class=\"dark\">\n"; | |
3666 | } else { | |
3667 | print "<tr class=\"light\">\n"; | |
3668 | } | |
3669 | $alternate ^= 1; | |
27dd1a83 JN |
3670 | if (defined $tag{'age'}) { |
3671 | print "<td><i>$tag{'age'}</i></td>\n"; | |
3672 | } else { | |
3673 | print "<td></td>\n"; | |
3674 | } | |
3675 | print "<td>" . | |
1c2a4f5a | 3676 | $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}), |
63e4220b | 3677 | -class => "list name"}, esc_html($tag{'name'})) . |
717b8311 JN |
3678 | "</td>\n" . |
3679 | "<td>"; | |
3680 | if (defined $comment) { | |
952c65fc JN |
3681 | print format_subject_html($comment, $comment_short, |
3682 | href(action=>"tag", hash=>$tag{'id'})); | |
717b8311 JN |
3683 | } |
3684 | print "</td>\n" . | |
3685 | "<td class=\"selflink\">"; | |
3686 | if ($tag{'type'} eq "tag") { | |
1c2a4f5a | 3687 | print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag"); |
717b8311 JN |
3688 | } else { |
3689 | print " "; | |
3690 | } | |
3691 | print "</td>\n" . | |
3692 | "<td class=\"link\">" . " | " . | |
1c2a4f5a | 3693 | $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'}); |
717b8311 | 3694 | if ($tag{'reftype'} eq "commit") { |
bf901f8e JN |
3695 | print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") . |
3696 | " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log"); | |
717b8311 | 3697 | } elsif ($tag{'reftype'} eq "blob") { |
1c2a4f5a | 3698 | print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw"); |
717b8311 JN |
3699 | } |
3700 | print "</td>\n" . | |
3701 | "</tr>"; | |
3702 | } | |
3703 | if (defined $extra) { | |
3704 | print "<tr>\n" . | |
3705 | "<td colspan=\"5\">$extra</td>\n" . | |
3706 | "</tr>\n"; | |
3707 | } | |
3708 | print "</table>\n"; | |
3709 | } | |
3710 | ||
3711 | sub git_heads_body { | |
3712 | # uses global variable $project | |
120ddde2 | 3713 | my ($headlist, $head, $from, $to, $extra) = @_; |
717b8311 | 3714 | $from = 0 unless defined $from; |
120ddde2 | 3715 | $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to); |
717b8311 | 3716 | |
591ebf65 | 3717 | print "<table class=\"heads\">\n"; |
6dd36acd | 3718 | my $alternate = 1; |
717b8311 | 3719 | for (my $i = $from; $i <= $to; $i++) { |
120ddde2 | 3720 | my $entry = $headlist->[$i]; |
cd146408 JN |
3721 | my %ref = %$entry; |
3722 | my $curr = $ref{'id'} eq $head; | |
717b8311 JN |
3723 | if ($alternate) { |
3724 | print "<tr class=\"dark\">\n"; | |
3725 | } else { | |
3726 | print "<tr class=\"light\">\n"; | |
3727 | } | |
3728 | $alternate ^= 1; | |
cd146408 JN |
3729 | print "<td><i>$ref{'age'}</i></td>\n" . |
3730 | ($curr ? "<td class=\"current_head\">" : "<td>") . | |
bf901f8e | 3731 | $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}), |
cd146408 | 3732 | -class => "list name"},esc_html($ref{'name'})) . |
717b8311 JN |
3733 | "</td>\n" . |
3734 | "<td class=\"link\">" . | |
bf901f8e JN |
3735 | $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " . |
3736 | $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " . | |
3737 | $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") . | |
717b8311 JN |
3738 | "</td>\n" . |
3739 | "</tr>"; | |
3740 | } | |
3741 | if (defined $extra) { | |
3742 | print "<tr>\n" . | |
3743 | "<td colspan=\"3\">$extra</td>\n" . | |
3744 | "</tr>\n"; | |
3745 | } | |
3746 | print "</table>\n"; | |
3747 | } | |
3748 | ||
8dbc0fce | 3749 | sub git_search_grep_body { |
5ad66088 | 3750 | my ($commitlist, $from, $to, $extra) = @_; |
8dbc0fce | 3751 | $from = 0 unless defined $from; |
5ad66088 | 3752 | $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to); |
8dbc0fce | 3753 | |
591ebf65 | 3754 | print "<table class=\"commit_search\">\n"; |
8dbc0fce RF |
3755 | my $alternate = 1; |
3756 | for (my $i = $from; $i <= $to; $i++) { | |
5ad66088 | 3757 | my %co = %{$commitlist->[$i]}; |
8dbc0fce RF |
3758 | if (!%co) { |
3759 | next; | |
3760 | } | |
5ad66088 | 3761 | my $commit = $co{'id'}; |
8dbc0fce RF |
3762 | if ($alternate) { |
3763 | print "<tr class=\"dark\">\n"; | |
3764 | } else { | |
3765 | print "<tr class=\"light\">\n"; | |
3766 | } | |
3767 | $alternate ^= 1; | |
ce58ec91 | 3768 | my $author = chop_and_escape_str($co{'author_name'}, 15, 5); |
8dbc0fce | 3769 | print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" . |
e076a0e7 | 3770 | "<td><i>" . $author . "</i></td>\n" . |
8dbc0fce RF |
3771 | "<td>" . |
3772 | $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"}, | |
d3cd2495 | 3773 | chop_and_escape_str($co{'title'}, 50) . "<br/>"); |
8dbc0fce RF |
3774 | my $comment = $co{'comment'}; |
3775 | foreach my $line (@$comment) { | |
7e431ef9 | 3776 | if ($line =~ m/^(.*)($search_regexp)(.*)$/i) { |
8dbc0fce RF |
3777 | my $lead = esc_html($1) || ""; |
3778 | $lead = chop_str($lead, 30, 10); | |
3779 | my $match = esc_html($2) || ""; | |
3780 | my $trail = esc_html($3) || ""; | |
3781 | $trail = chop_str($trail, 30, 10); | |
3782 | my $text = "$lead<span class=\"match\">$match</span>$trail"; | |
3783 | print chop_str($text, 80, 5) . "<br/>\n"; | |
3784 | } | |
3785 | } | |
3786 | print "</td>\n" . | |
3787 | "<td class=\"link\">" . | |
3788 | $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") . | |
3789 | " | " . | |
f1fe8f5c CR |
3790 | $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") . |
3791 | " | " . | |
8dbc0fce RF |
3792 | $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree"); |
3793 | print "</td>\n" . | |
3794 | "</tr>\n"; | |
3795 | } | |
3796 | if (defined $extra) { | |
3797 | print "<tr>\n" . | |
3798 | "<td colspan=\"3\">$extra</td>\n" . | |
3799 | "</tr>\n"; | |
3800 | } | |
3801 | print "</table>\n"; | |
3802 | } | |
3803 | ||
717b8311 JN |
3804 | ## ====================================================================== |
3805 | ## ====================================================================== | |
3806 | ## actions | |
3807 | ||
717b8311 | 3808 | sub git_project_list { |
6326b60c | 3809 | my $order = $cgi->param('o'); |
b06dcf8c | 3810 | if (defined $order && $order !~ m/none|project|descr|owner|age/) { |
e2860ead | 3811 | die_error(undef, "Unknown order parameter"); |
6326b60c JN |
3812 | } |
3813 | ||
847e01fb | 3814 | my @list = git_get_projects_list(); |
717b8311 | 3815 | if (!@list) { |
cac4bd94 | 3816 | die_error(undef, "No projects found"); |
717b8311 | 3817 | } |
6326b60c | 3818 | |
717b8311 JN |
3819 | git_header_html(); |
3820 | if (-f $home_text) { | |
3821 | print "<div class=\"index_include\">\n"; | |
3822 | open (my $fd, $home_text); | |
3823 | print <$fd>; | |
3824 | close $fd; | |
3825 | print "</div>\n"; | |
9f5dcb81 | 3826 | } |
e30496df PB |
3827 | git_project_list_body(\@list, $order); |
3828 | git_footer_html(); | |
3829 | } | |
3830 | ||
3831 | sub git_forks { | |
3832 | my $order = $cgi->param('o'); | |
b06dcf8c | 3833 | if (defined $order && $order !~ m/none|project|descr|owner|age/) { |
e30496df | 3834 | die_error(undef, "Unknown order parameter"); |
717b8311 | 3835 | } |
e30496df PB |
3836 | |
3837 | my @list = git_get_projects_list($project); | |
3838 | if (!@list) { | |
3839 | die_error(undef, "No forks found"); | |
9f5dcb81 | 3840 | } |
e30496df PB |
3841 | |
3842 | git_header_html(); | |
3843 | git_print_page_nav('',''); | |
3844 | git_print_header_div('summary', "$project forks"); | |
3845 | git_project_list_body(\@list, $order); | |
717b8311 | 3846 | git_footer_html(); |
9f5dcb81 JN |
3847 | } |
3848 | ||
fc2b2be0 | 3849 | sub git_project_index { |
e30496df | 3850 | my @projects = git_get_projects_list($project); |
fc2b2be0 JN |
3851 | |
3852 | print $cgi->header( | |
3853 | -type => 'text/plain', | |
3854 | -charset => 'utf-8', | |
ab41dfbf | 3855 | -content_disposition => 'inline; filename="index.aux"'); |
fc2b2be0 JN |
3856 | |
3857 | foreach my $pr (@projects) { | |
3858 | if (!exists $pr->{'owner'}) { | |
76e4f5d0 | 3859 | $pr->{'owner'} = git_get_project_owner("$pr->{'path'}"); |
fc2b2be0 JN |
3860 | } |
3861 | ||
3862 | my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'}); | |
3863 | # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' ' | |
3864 | $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg; | |
3865 | $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg; | |
3866 | $path =~ s/ /\+/g; | |
3867 | $owner =~ s/ /\+/g; | |
3868 | ||
3869 | print "$path $owner\n"; | |
3870 | } | |
3871 | } | |
3872 | ||
ede5e100 | 3873 | sub git_summary { |
847e01fb | 3874 | my $descr = git_get_project_description($project) || "none"; |
a979d128 | 3875 | my %co = parse_commit("HEAD"); |
785cdea9 | 3876 | my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : (); |
a979d128 | 3877 | my $head = $co{'id'}; |
ede5e100 | 3878 | |
1e0cf030 | 3879 | my $owner = git_get_project_owner($project); |
ede5e100 | 3880 | |
cd146408 | 3881 | my $refs = git_get_references(); |
313ce8ce RF |
3882 | # These get_*_list functions return one more to allow us to see if |
3883 | # there are more ... | |
3884 | my @taglist = git_get_tags_list(16); | |
3885 | my @headlist = git_get_heads_list(16); | |
e30496df | 3886 | my @forklist; |
5dd5ed09 JH |
3887 | my ($check_forks) = gitweb_check_feature('forks'); |
3888 | ||
3889 | if ($check_forks) { | |
e30496df PB |
3890 | @forklist = git_get_projects_list($project); |
3891 | } | |
120ddde2 | 3892 | |
ede5e100 | 3893 | git_header_html(); |
847e01fb | 3894 | git_print_page_nav('summary','', $head); |
9f5dcb81 | 3895 | |
19806691 | 3896 | print "<div class=\"title\"> </div>\n"; |
591ebf65 | 3897 | print "<table class=\"projects_list\">\n" . |
40c13813 | 3898 | "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" . |
ce312aff | 3899 | "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n"; |
785cdea9 JN |
3900 | if (defined $cd{'rfc2822'}) { |
3901 | print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n"; | |
3902 | } | |
3903 | ||
e79ca7cc JN |
3904 | # use per project git URL list in $projectroot/$project/cloneurl |
3905 | # or make project git URL from git base URL and project name | |
19a8721e | 3906 | my $url_tag = "URL"; |
e79ca7cc JN |
3907 | my @url_list = git_get_project_url_list($project); |
3908 | @url_list = map { "$_/$project" } @git_base_url_list unless @url_list; | |
3909 | foreach my $git_url (@url_list) { | |
3910 | next unless $git_url; | |
3911 | print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n"; | |
19a8721e JN |
3912 | $url_tag = ""; |
3913 | } | |
3914 | print "</table>\n"; | |
9f5dcb81 | 3915 | |
447ef09a PB |
3916 | if (-s "$projectroot/$project/README.html") { |
3917 | if (open my $fd, "$projectroot/$project/README.html") { | |
9d066745 JN |
3918 | print "<div class=\"title\">readme</div>\n" . |
3919 | "<div class=\"readme\">\n"; | |
447ef09a | 3920 | print $_ while (<$fd>); |
9d066745 | 3921 | print "\n</div>\n"; # class="readme" |
447ef09a PB |
3922 | close $fd; |
3923 | } | |
3924 | } | |
3925 | ||
313ce8ce RF |
3926 | # we need to request one more than 16 (0..15) to check if |
3927 | # those 16 are all | |
785cdea9 JN |
3928 | my @commitlist = $head ? parse_commits($head, 17) : (); |
3929 | if (@commitlist) { | |
3930 | git_print_header_div('shortlog'); | |
3931 | git_shortlog_body(\@commitlist, 0, 15, $refs, | |
3932 | $#commitlist <= 15 ? undef : | |
3933 | $cgi->a({-href => href(action=>"shortlog")}, "...")); | |
3934 | } | |
ede5e100 | 3935 | |
120ddde2 | 3936 | if (@taglist) { |
847e01fb | 3937 | git_print_header_div('tags'); |
120ddde2 | 3938 | git_tags_body(\@taglist, 0, 15, |
313ce8ce | 3939 | $#taglist <= 15 ? undef : |
1c2a4f5a | 3940 | $cgi->a({-href => href(action=>"tags")}, "...")); |
ede5e100 | 3941 | } |
0db37973 | 3942 | |
120ddde2 | 3943 | if (@headlist) { |
847e01fb | 3944 | git_print_header_div('heads'); |
120ddde2 | 3945 | git_heads_body(\@headlist, $head, 0, 15, |
313ce8ce | 3946 | $#headlist <= 15 ? undef : |
1c2a4f5a | 3947 | $cgi->a({-href => href(action=>"heads")}, "...")); |
0db37973 | 3948 | } |
9f5dcb81 | 3949 | |
e30496df PB |
3950 | if (@forklist) { |
3951 | git_print_header_div('forks'); | |
3952 | git_project_list_body(\@forklist, undef, 0, 15, | |
aaca9675 | 3953 | $#forklist <= 15 ? undef : |
e30496df | 3954 | $cgi->a({-href => href(action=>"forks")}, "..."), |
a23f0a73 | 3955 | 'noheader'); |
e30496df PB |
3956 | } |
3957 | ||
ede5e100 KS |
3958 | git_footer_html(); |
3959 | } | |
3960 | ||
d8a20ba9 | 3961 | sub git_tag { |
847e01fb | 3962 | my $head = git_get_head_hash($project); |
d8a20ba9 | 3963 | git_header_html(); |
847e01fb JN |
3964 | git_print_page_nav('','', $head,undef,$head); |
3965 | my %tag = parse_tag($hash); | |
198a2a8a JN |
3966 | |
3967 | if (! %tag) { | |
3968 | die_error(undef, "Unknown tag object"); | |
3969 | } | |
3970 | ||
847e01fb | 3971 | git_print_header_div('commit', esc_html($tag{'name'}), $hash); |
d8a20ba9 | 3972 | print "<div class=\"title_text\">\n" . |
591ebf65 | 3973 | "<table class=\"object_header\">\n" . |
e4669df9 KS |
3974 | "<tr>\n" . |
3975 | "<td>object</td>\n" . | |
952c65fc JN |
3976 | "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})}, |
3977 | $tag{'object'}) . "</td>\n" . | |
3978 | "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})}, | |
3979 | $tag{'type'}) . "</td>\n" . | |
e4669df9 | 3980 | "</tr>\n"; |
d8a20ba9 | 3981 | if (defined($tag{'author'})) { |
847e01fb | 3982 | my %ad = parse_date($tag{'epoch'}, $tag{'tz'}); |
40c13813 | 3983 | print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n"; |
952c65fc JN |
3984 | print "<tr><td></td><td>" . $ad{'rfc2822'} . |
3985 | sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . | |
3986 | "</td></tr>\n"; | |
d8a20ba9 KS |
3987 | } |
3988 | print "</table>\n\n" . | |
3989 | "</div>\n"; | |
3990 | print "<div class=\"page_body\">"; | |
3991 | my $comment = $tag{'comment'}; | |
3992 | foreach my $line (@$comment) { | |
7002243f | 3993 | chomp $line; |
793c400c | 3994 | print esc_html($line, -nbsp=>1) . "<br/>\n"; |
d8a20ba9 KS |
3995 | } |
3996 | print "</div>\n"; | |
3997 | git_footer_html(); | |
3998 | } | |
3999 | ||
1f2857ea LT |
4000 | sub git_blame2 { |
4001 | my $fd; | |
4002 | my $ftype; | |
ddb8d900 | 4003 | |
1d3fc68a AK |
4004 | my ($have_blame) = gitweb_check_feature('blame'); |
4005 | if (!$have_blame) { | |
ddb8d900 AK |
4006 | die_error('403 Permission denied', "Permission denied"); |
4007 | } | |
1f2857ea | 4008 | die_error('404 Not Found', "File name not defined") if (!$file_name); |
847e01fb | 4009 | $hash_base ||= git_get_head_hash($project); |
cac4bd94 | 4010 | die_error(undef, "Couldn't find base commit") unless ($hash_base); |
847e01fb | 4011 | my %co = parse_commit($hash_base) |
1f2857ea LT |
4012 | or die_error(undef, "Reading commit failed"); |
4013 | if (!defined $hash) { | |
4014 | $hash = git_get_hash_by_path($hash_base, $file_name, "blob") | |
4015 | or die_error(undef, "Error looking up file"); | |
4016 | } | |
4017 | $ftype = git_get_type($hash); | |
4018 | if ($ftype !~ "blob") { | |
f73bbb2d | 4019 | die_error('400 Bad Request', "Object is not a blob"); |
1f2857ea | 4020 | } |
48fd688a | 4021 | open ($fd, "-|", git_cmd(), "blame", '-p', '--', |
eeef88cd | 4022 | $file_name, $hash_base) |
cac4bd94 | 4023 | or die_error(undef, "Open git-blame failed"); |
1f2857ea | 4024 | git_header_html(); |
0d83ddc4 | 4025 | my $formats_nav = |
a3823e5a | 4026 | $cgi->a({-href => href(action=>"blob", -replay=>1)}, |
952c65fc JN |
4027 | "blob") . |
4028 | " | " . | |
a3823e5a JN |
4029 | $cgi->a({-href => href(action=>"history", -replay=>1)}, |
4030 | "history") . | |
cae1862a | 4031 | " | " . |
952c65fc | 4032 | $cgi->a({-href => href(action=>"blame", file_name=>$file_name)}, |
f35274da | 4033 | "HEAD"); |
847e01fb JN |
4034 | git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav); |
4035 | git_print_header_div('commit', esc_html($co{'title'}), $hash_base); | |
59fb1c94 | 4036 | git_print_page_path($file_name, $ftype, $hash_base); |
82f930de | 4037 | my @rev_color = (qw(light2 dark2)); |
cc1bf97e LT |
4038 | my $num_colors = scalar(@rev_color); |
4039 | my $current_color = 0; | |
4040 | my $last_rev; | |
59b9f61a JN |
4041 | print <<HTML; |
4042 | <div class="page_body"> | |
4043 | <table class="blame"> | |
4044 | <tr><th>Commit</th><th>Line</th><th>Data</th></tr> | |
4045 | HTML | |
eeef88cd JH |
4046 | my %metainfo = (); |
4047 | while (1) { | |
4048 | $_ = <$fd>; | |
4049 | last unless defined $_; | |
d15c55aa | 4050 | my ($full_rev, $orig_lineno, $lineno, $group_size) = |
eeef88cd JH |
4051 | /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/; |
4052 | if (!exists $metainfo{$full_rev}) { | |
4053 | $metainfo{$full_rev} = {}; | |
4054 | } | |
4055 | my $meta = $metainfo{$full_rev}; | |
4056 | while (<$fd>) { | |
4057 | last if (s/^\t//); | |
4058 | if (/^(\S+) (.*)$/) { | |
4059 | $meta->{$1} = $2; | |
4060 | } | |
4061 | } | |
4062 | my $data = $_; | |
7002243f | 4063 | chomp $data; |
1f2857ea | 4064 | my $rev = substr($full_rev, 0, 8); |
eeef88cd JH |
4065 | my $author = $meta->{'author'}; |
4066 | my %date = parse_date($meta->{'author-time'}, | |
a23f0a73 | 4067 | $meta->{'author-tz'}); |
eeef88cd JH |
4068 | my $date = $date{'iso-tz'}; |
4069 | if ($group_size) { | |
cc1bf97e LT |
4070 | $current_color = ++$current_color % $num_colors; |
4071 | } | |
4072 | print "<tr class=\"$rev_color[$current_color]\">\n"; | |
eeef88cd JH |
4073 | if ($group_size) { |
4074 | print "<td class=\"sha1\""; | |
5ad0828c | 4075 | print " title=\"". esc_html($author) . ", $date\""; |
eeef88cd JH |
4076 | print " rowspan=\"$group_size\"" if ($group_size > 1); |
4077 | print ">"; | |
4078 | print $cgi->a({-href => href(action=>"commit", | |
a23f0a73 JN |
4079 | hash=>$full_rev, |
4080 | file_name=>$file_name)}, | |
4081 | esc_html($rev)); | |
eeef88cd | 4082 | print "</td>\n"; |
9dc5f8c9 | 4083 | } |
244a70e6 | 4084 | open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^") |
f73bbb2d | 4085 | or die_error(undef, "Open git-rev-parse failed"); |
244a70e6 LT |
4086 | my $parent_commit = <$dd>; |
4087 | close $dd; | |
4088 | chomp($parent_commit); | |
eeef88cd | 4089 | my $blamed = href(action => 'blame', |
a23f0a73 JN |
4090 | file_name => $meta->{'filename'}, |
4091 | hash_base => $parent_commit); | |
eeef88cd JH |
4092 | print "<td class=\"linenr\">"; |
4093 | print $cgi->a({ -href => "$blamed#l$orig_lineno", | |
a23f0a73 JN |
4094 | -id => "l$lineno", |
4095 | -class => "linenr" }, | |
4096 | esc_html($lineno)); | |
eeef88cd | 4097 | print "</td>"; |
1f2857ea LT |
4098 | print "<td class=\"pre\">" . esc_html($data) . "</td>\n"; |
4099 | print "</tr>\n"; | |
4100 | } | |
4101 | print "</table>\n"; | |
4102 | print "</div>"; | |
952c65fc JN |
4103 | close $fd |
4104 | or print "Reading blob failed\n"; | |
1f2857ea LT |
4105 | git_footer_html(); |
4106 | } | |
4107 | ||
e34ef621 FF |
4108 | sub git_blame { |
4109 | my $fd; | |
ddb8d900 | 4110 | |
1d3fc68a AK |
4111 | my ($have_blame) = gitweb_check_feature('blame'); |
4112 | if (!$have_blame) { | |
ddb8d900 AK |
4113 | die_error('403 Permission denied', "Permission denied"); |
4114 | } | |
cac4bd94 | 4115 | die_error('404 Not Found', "File name not defined") if (!$file_name); |
847e01fb | 4116 | $hash_base ||= git_get_head_hash($project); |
cac4bd94 | 4117 | die_error(undef, "Couldn't find base commit") unless ($hash_base); |
847e01fb | 4118 | my %co = parse_commit($hash_base) |
cac4bd94 | 4119 | or die_error(undef, "Reading commit failed"); |
e34ef621 FF |
4120 | if (!defined $hash) { |
4121 | $hash = git_get_hash_by_path($hash_base, $file_name, "blob") | |
cac4bd94 | 4122 | or die_error(undef, "Error lookup file"); |
e34ef621 | 4123 | } |
25691fbe | 4124 | open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base) |
cac4bd94 | 4125 | or die_error(undef, "Open git-annotate failed"); |
e34ef621 | 4126 | git_header_html(); |
0d83ddc4 | 4127 | my $formats_nav = |
952c65fc JN |
4128 | $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)}, |
4129 | "blob") . | |
4130 | " | " . | |
cae1862a PB |
4131 | $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)}, |
4132 | "history") . | |
4133 | " | " . | |
952c65fc | 4134 | $cgi->a({-href => href(action=>"blame", file_name=>$file_name)}, |
f35274da | 4135 | "HEAD"); |
847e01fb JN |
4136 | git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav); |
4137 | git_print_header_div('commit', esc_html($co{'title'}), $hash_base); | |
59fb1c94 | 4138 | git_print_page_path($file_name, 'blob', $hash_base); |
e34ef621 FF |
4139 | print "<div class=\"page_body\">\n"; |
4140 | print <<HTML; | |
1f1ab5f0 | 4141 | <table class="blame"> |
e34ef621 FF |
4142 | <tr> |
4143 | <th>Commit</th> | |
4144 | <th>Age</th> | |
4145 | <th>Author</th> | |
4146 | <th>Line</th> | |
4147 | <th>Data</th> | |
4148 | </tr> | |
4149 | HTML | |
4150 | my @line_class = (qw(light dark)); | |
4151 | my $line_class_len = scalar (@line_class); | |
4152 | my $line_class_num = $#line_class; | |
4153 | while (my $line = <$fd>) { | |
4154 | my $long_rev; | |
4155 | my $short_rev; | |
4156 | my $author; | |
4157 | my $time; | |
4158 | my $lineno; | |
4159 | my $data; | |
4160 | my $age; | |
4161 | my $age_str; | |
1f1ab5f0 | 4162 | my $age_class; |
e34ef621 FF |
4163 | |
4164 | chomp $line; | |
4165 | $line_class_num = ($line_class_num + 1) % $line_class_len; | |
4166 | ||
030b5208 | 4167 | if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) { |
e34ef621 FF |
4168 | $long_rev = $1; |
4169 | $author = $2; | |
4170 | $time = $3; | |
4171 | $lineno = $4; | |
4172 | $data = $5; | |
4173 | } else { | |
1f1ab5f0 | 4174 | print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n); |
e34ef621 FF |
4175 | next; |
4176 | } | |
4177 | $short_rev = substr ($long_rev, 0, 8); | |
4178 | $age = time () - $time; | |
4179 | $age_str = age_string ($age); | |
4180 | $age_str =~ s/ / /g; | |
1f1ab5f0 | 4181 | $age_class = age_class($age); |
e34ef621 FF |
4182 | $author = esc_html ($author); |
4183 | $author =~ s/ / /g; | |
f16db173 JN |
4184 | |
4185 | $data = untabify($data); | |
717b8311 | 4186 | $data = esc_html ($data); |
2d007374 | 4187 | |
717b8311 JN |
4188 | print <<HTML; |
4189 | <tr class="$line_class[$line_class_num]"> | |
1c2a4f5a | 4190 | <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td> |
717b8311 JN |
4191 | <td class="$age_class">$age_str</td> |
4192 | <td>$author</td> | |
4193 | <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td> | |
4194 | <td class="pre">$data</td> | |
4195 | </tr> | |
4196 | HTML | |
4197 | } # while (my $line = <$fd>) | |
4198 | print "</table>\n\n"; | |
952c65fc JN |
4199 | close $fd |
4200 | or print "Reading blob failed.\n"; | |
717b8311 JN |
4201 | print "</div>"; |
4202 | git_footer_html(); | |
2d007374 PB |
4203 | } |
4204 | ||
717b8311 | 4205 | sub git_tags { |
847e01fb | 4206 | my $head = git_get_head_hash($project); |
717b8311 | 4207 | git_header_html(); |
847e01fb JN |
4208 | git_print_page_nav('','', $head,undef,$head); |
4209 | git_print_header_div('summary', $project); | |
2d007374 | 4210 | |
cd146408 JN |
4211 | my @tagslist = git_get_tags_list(); |
4212 | if (@tagslist) { | |
4213 | git_tags_body(\@tagslist); | |
2d007374 | 4214 | } |
717b8311 | 4215 | git_footer_html(); |
2d007374 PB |
4216 | } |
4217 | ||
717b8311 | 4218 | sub git_heads { |
847e01fb | 4219 | my $head = git_get_head_hash($project); |
717b8311 | 4220 | git_header_html(); |
847e01fb JN |
4221 | git_print_page_nav('','', $head,undef,$head); |
4222 | git_print_header_div('summary', $project); | |
930cf7dd | 4223 | |
cd146408 JN |
4224 | my @headslist = git_get_heads_list(); |
4225 | if (@headslist) { | |
4226 | git_heads_body(\@headslist, $head); | |
f5aa79d9 | 4227 | } |
717b8311 | 4228 | git_footer_html(); |
f5aa79d9 JN |
4229 | } |
4230 | ||
19806691 | 4231 | sub git_blob_plain { |
f2e73302 | 4232 | my $expires; |
f2e73302 | 4233 | |
cff0771b | 4234 | if (!defined $hash) { |
5be01bc8 | 4235 | if (defined $file_name) { |
847e01fb | 4236 | my $base = $hash_base || git_get_head_hash($project); |
5be01bc8 | 4237 | $hash = git_get_hash_by_path($base, $file_name, "blob") |
cac4bd94 | 4238 | or die_error(undef, "Error lookup file"); |
5be01bc8 | 4239 | } else { |
cac4bd94 | 4240 | die_error(undef, "No file name defined"); |
5be01bc8 | 4241 | } |
800764cf MW |
4242 | } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) { |
4243 | # blobs defined by non-textual hash id's can be cached | |
4244 | $expires = "+1d"; | |
5be01bc8 | 4245 | } |
800764cf | 4246 | |
930cf7dd | 4247 | my $type = shift; |
25691fbe | 4248 | open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash |
cfd82669 | 4249 | or die_error(undef, "Couldn't cat $file_name, $hash"); |
930cf7dd | 4250 | |
847e01fb | 4251 | $type ||= blob_mimetype($fd, $file_name); |
f5aa79d9 JN |
4252 | |
4253 | # save as filename, even when no $file_name is given | |
4254 | my $save_as = "$hash"; | |
9312944d KS |
4255 | if (defined $file_name) { |
4256 | $save_as = $file_name; | |
f5aa79d9 JN |
4257 | } elsif ($type =~ m/^text\//) { |
4258 | $save_as .= '.txt'; | |
9312944d | 4259 | } |
f5aa79d9 | 4260 | |
f2e73302 JN |
4261 | print $cgi->header( |
4262 | -type => "$type", | |
4263 | -expires=>$expires, | |
a2a3bf7b | 4264 | -content_disposition => 'inline; filename="' . "$save_as" . '"'); |
19806691 | 4265 | undef $/; |
ad14e931 | 4266 | binmode STDOUT, ':raw'; |
19806691 | 4267 | print <$fd>; |
ad14e931 | 4268 | binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi |
19806691 KS |
4269 | $/ = "\n"; |
4270 | close $fd; | |
4271 | } | |
4272 | ||
930cf7dd | 4273 | sub git_blob { |
f2e73302 | 4274 | my $expires; |
f2e73302 | 4275 | |
cff0771b | 4276 | if (!defined $hash) { |
5be01bc8 | 4277 | if (defined $file_name) { |
847e01fb | 4278 | my $base = $hash_base || git_get_head_hash($project); |
5be01bc8 | 4279 | $hash = git_get_hash_by_path($base, $file_name, "blob") |
cac4bd94 | 4280 | or die_error(undef, "Error lookup file"); |
5be01bc8 | 4281 | } else { |
cac4bd94 | 4282 | die_error(undef, "No file name defined"); |
5be01bc8 | 4283 | } |
800764cf MW |
4284 | } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) { |
4285 | # blobs defined by non-textual hash id's can be cached | |
4286 | $expires = "+1d"; | |
5be01bc8 | 4287 | } |
800764cf | 4288 | |
1d3fc68a | 4289 | my ($have_blame) = gitweb_check_feature('blame'); |
25691fbe | 4290 | open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash |
cac4bd94 | 4291 | or die_error(undef, "Couldn't cat $file_name, $hash"); |
847e01fb | 4292 | my $mimetype = blob_mimetype($fd, $file_name); |
5a4cf334 | 4293 | if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) { |
930cf7dd LT |
4294 | close $fd; |
4295 | return git_blob_plain($mimetype); | |
4296 | } | |
5a4cf334 JN |
4297 | # we can have blame only for text/* mimetype |
4298 | $have_blame &&= ($mimetype =~ m!^text/!); | |
4299 | ||
f2e73302 | 4300 | git_header_html(undef, $expires); |
0d83ddc4 | 4301 | my $formats_nav = ''; |
847e01fb | 4302 | if (defined $hash_base && (my %co = parse_commit($hash_base))) { |
930cf7dd LT |
4303 | if (defined $file_name) { |
4304 | if ($have_blame) { | |
952c65fc | 4305 | $formats_nav .= |
a3823e5a | 4306 | $cgi->a({-href => href(action=>"blame", -replay=>1)}, |
952c65fc JN |
4307 | "blame") . |
4308 | " | "; | |
930cf7dd | 4309 | } |
0d83ddc4 | 4310 | $formats_nav .= |
a3823e5a | 4311 | $cgi->a({-href => href(action=>"history", -replay=>1)}, |
cae1862a PB |
4312 | "history") . |
4313 | " | " . | |
a3823e5a | 4314 | $cgi->a({-href => href(action=>"blob_plain", -replay=>1)}, |
35329cc1 | 4315 | "raw") . |
952c65fc JN |
4316 | " | " . |
4317 | $cgi->a({-href => href(action=>"blob", | |
4318 | hash_base=>"HEAD", file_name=>$file_name)}, | |
f35274da | 4319 | "HEAD"); |
930cf7dd | 4320 | } else { |
952c65fc | 4321 | $formats_nav .= |
a3823e5a JN |
4322 | $cgi->a({-href => href(action=>"blob_plain", -replay=>1)}, |
4323 | "raw"); | |
930cf7dd | 4324 | } |
847e01fb JN |
4325 | git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav); |
4326 | git_print_header_div('commit', esc_html($co{'title'}), $hash_base); | |
930cf7dd LT |
4327 | } else { |
4328 | print "<div class=\"page_nav\">\n" . | |
4329 | "<br/><br/></div>\n" . | |
4330 | "<div class=\"title\">$hash</div>\n"; | |
4331 | } | |
59fb1c94 | 4332 | git_print_page_path($file_name, "blob", $hash_base); |
930cf7dd | 4333 | print "<div class=\"page_body\">\n"; |
5a4cf334 JN |
4334 | if ($mimetype =~ m!^text/!) { |
4335 | my $nr; | |
4336 | while (my $line = <$fd>) { | |
4337 | chomp $line; | |
4338 | $nr++; | |
4339 | $line = untabify($line); | |
4340 | printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", | |
4341 | $nr, $nr, $nr, esc_html($line, -nbsp=>1); | |
4342 | } | |
4343 | } elsif ($mimetype =~ m!^image/!) { | |
4344 | print qq!<img type="$mimetype"!; | |
4345 | if ($file_name) { | |
4346 | print qq! alt="$file_name" title="$file_name"!; | |
4347 | } | |
4348 | print qq! src="! . | |
4349 | href(action=>"blob_plain", hash=>$hash, | |
4350 | hash_base=>$hash_base, file_name=>$file_name) . | |
4351 | qq!" />\n!; | |
930cf7dd | 4352 | } |
952c65fc JN |
4353 | close $fd |
4354 | or print "Reading blob failed.\n"; | |
930cf7dd LT |
4355 | print "</div>"; |
4356 | git_footer_html(); | |
4357 | } | |
4358 | ||
09bd7898 | 4359 | sub git_tree { |
6f7ea5fb LT |
4360 | if (!defined $hash_base) { |
4361 | $hash_base = "HEAD"; | |
4362 | } | |
b87d78d6 | 4363 | if (!defined $hash) { |
09bd7898 | 4364 | if (defined $file_name) { |
6f7ea5fb LT |
4365 | $hash = git_get_hash_by_path($hash_base, $file_name, "tree"); |
4366 | } else { | |
4367 | $hash = $hash_base; | |
10dba28d | 4368 | } |
e925f38c | 4369 | } |
232ff553 | 4370 | $/ = "\0"; |
25691fbe | 4371 | open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash |
cac4bd94 | 4372 | or die_error(undef, "Open git-ls-tree failed"); |
0881d2d1 | 4373 | my @entries = map { chomp; $_ } <$fd>; |
cac4bd94 | 4374 | close $fd or die_error(undef, "Reading tree failed"); |
232ff553 | 4375 | $/ = "\n"; |
d63577da | 4376 | |
847e01fb JN |
4377 | my $refs = git_get_references(); |
4378 | my $ref = format_ref_marker($refs, $hash_base); | |
12a88f2f | 4379 | git_header_html(); |
300454fe | 4380 | my $basedir = ''; |
1d3fc68a | 4381 | my ($have_blame) = gitweb_check_feature('blame'); |
847e01fb | 4382 | if (defined $hash_base && (my %co = parse_commit($hash_base))) { |
cae1862a PB |
4383 | my @views_nav = (); |
4384 | if (defined $file_name) { | |
4385 | push @views_nav, | |
a3823e5a | 4386 | $cgi->a({-href => href(action=>"history", -replay=>1)}, |
cae1862a PB |
4387 | "history"), |
4388 | $cgi->a({-href => href(action=>"tree", | |
4389 | hash_base=>"HEAD", file_name=>$file_name)}, | |
f35274da | 4390 | "HEAD"), |
cae1862a | 4391 | } |
a3c8ab30 MM |
4392 | my $snapshot_links = format_snapshot_links($hash); |
4393 | if (defined $snapshot_links) { | |
cae1862a | 4394 | # FIXME: Should be available when we have no hash base as well. |
a3c8ab30 | 4395 | push @views_nav, $snapshot_links; |
cae1862a PB |
4396 | } |
4397 | git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav)); | |
847e01fb | 4398 | git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base); |
d63577da | 4399 | } else { |
fa702003 | 4400 | undef $hash_base; |
d63577da KS |
4401 | print "<div class=\"page_nav\">\n"; |
4402 | print "<br/><br/></div>\n"; | |
4403 | print "<div class=\"title\">$hash</div>\n"; | |
4404 | } | |
09bd7898 | 4405 | if (defined $file_name) { |
300454fe JN |
4406 | $basedir = $file_name; |
4407 | if ($basedir ne '' && substr($basedir, -1) ne '/') { | |
4408 | $basedir .= '/'; | |
4409 | } | |
09bd7898 | 4410 | } |
59fb1c94 | 4411 | git_print_page_path($file_name, 'tree', $hash_base); |
fbb592a9 | 4412 | print "<div class=\"page_body\">\n"; |
591ebf65 | 4413 | print "<table class=\"tree\">\n"; |
6dd36acd | 4414 | my $alternate = 1; |
b6b7fc72 JN |
4415 | # '..' (top directory) link if possible |
4416 | if (defined $hash_base && | |
4417 | defined $file_name && $file_name =~ m![^/]+$!) { | |
4418 | if ($alternate) { | |
4419 | print "<tr class=\"dark\">\n"; | |
4420 | } else { | |
4421 | print "<tr class=\"light\">\n"; | |
4422 | } | |
4423 | $alternate ^= 1; | |
4424 | ||
4425 | my $up = $file_name; | |
4426 | $up =~ s!/?[^/]+$!!; | |
4427 | undef $up unless $up; | |
4428 | # based on git_print_tree_entry | |
4429 | print '<td class="mode">' . mode_str('040000') . "</td>\n"; | |
4430 | print '<td class="list">'; | |
4431 | print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base, | |
4432 | file_name=>$up)}, | |
4433 | ".."); | |
4434 | print "</td>\n"; | |
4435 | print "<td class=\"link\"></td>\n"; | |
4436 | ||
4437 | print "</tr>\n"; | |
4438 | } | |
161332a5 | 4439 | foreach my $line (@entries) { |
cb849b46 JN |
4440 | my %t = parse_ls_tree_line($line, -z => 1); |
4441 | ||
bddec01d | 4442 | if ($alternate) { |
c994d620 | 4443 | print "<tr class=\"dark\">\n"; |
bddec01d | 4444 | } else { |
c994d620 | 4445 | print "<tr class=\"light\">\n"; |
bddec01d KS |
4446 | } |
4447 | $alternate ^= 1; | |
cb849b46 | 4448 | |
300454fe | 4449 | git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame); |
fa702003 | 4450 | |
42f7eb94 | 4451 | print "</tr>\n"; |
161332a5 | 4452 | } |
42f7eb94 KS |
4453 | print "</table>\n" . |
4454 | "</div>"; | |
12a88f2f | 4455 | git_footer_html(); |
09bd7898 KS |
4456 | } |
4457 | ||
cb9c6e5b | 4458 | sub git_snapshot { |
a3c8ab30 | 4459 | my @supported_fmts = gitweb_check_feature('snapshot'); |
a781785d | 4460 | @supported_fmts = filter_snapshot_fmts(@supported_fmts); |
a3c8ab30 MM |
4461 | |
4462 | my $format = $cgi->param('sf'); | |
3473e7df JN |
4463 | if (!@supported_fmts) { |
4464 | die_error('403 Permission denied', "Permission denied"); | |
4465 | } | |
4466 | # default to first supported snapshot format | |
4467 | $format ||= $supported_fmts[0]; | |
4468 | if ($format !~ m/^[a-z0-9]+$/) { | |
4469 | die_error(undef, "Invalid snapshot format parameter"); | |
4470 | } elsif (!exists($known_snapshot_formats{$format})) { | |
4471 | die_error(undef, "Unknown snapshot format"); | |
4472 | } elsif (!grep($_ eq $format, @supported_fmts)) { | |
a3c8ab30 | 4473 | die_error(undef, "Unsupported snapshot format"); |
ddb8d900 AK |
4474 | } |
4475 | ||
cb9c6e5b AK |
4476 | if (!defined $hash) { |
4477 | $hash = git_get_head_hash($project); | |
4478 | } | |
4479 | ||
a3c8ab30 | 4480 | my $git_command = git_cmd_str(); |
072570ee | 4481 | my $name = $project; |
9a7d9410 ML |
4482 | $name =~ s,([^/])/*\.git$,$1,; |
4483 | $name = basename($name); | |
4484 | my $filename = to_utf8($name); | |
072570ee | 4485 | $name =~ s/\047/\047\\\047\047/g; |
072570ee | 4486 | my $cmd; |
a3c8ab30 MM |
4487 | $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}"; |
4488 | $cmd = "$git_command archive " . | |
887c5266 | 4489 | "--format=$known_snapshot_formats{$format}{'format'} " . |
a3c8ab30 MM |
4490 | "--prefix=\'$name\'/ $hash"; |
4491 | if (exists $known_snapshot_formats{$format}{'compressor'}) { | |
4492 | $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}}; | |
072570ee | 4493 | } |
cb9c6e5b | 4494 | |
ab41dfbf | 4495 | print $cgi->header( |
a3c8ab30 | 4496 | -type => $known_snapshot_formats{$format}{'type'}, |
a2a3bf7b | 4497 | -content_disposition => 'inline; filename="' . "$filename" . '"', |
ab41dfbf | 4498 | -status => '200 OK'); |
cb9c6e5b | 4499 | |
072570ee ML |
4500 | open my $fd, "-|", $cmd |
4501 | or die_error(undef, "Execute git-archive failed"); | |
cb9c6e5b AK |
4502 | binmode STDOUT, ':raw'; |
4503 | print <$fd>; | |
4504 | binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi | |
4505 | close $fd; | |
cb9c6e5b AK |
4506 | } |
4507 | ||
09bd7898 | 4508 | sub git_log { |
847e01fb | 4509 | my $head = git_get_head_hash($project); |
0db37973 | 4510 | if (!defined $hash) { |
19806691 | 4511 | $hash = $head; |
0db37973 | 4512 | } |
ea4a6df4 KS |
4513 | if (!defined $page) { |
4514 | $page = 0; | |
b87d78d6 | 4515 | } |
847e01fb | 4516 | my $refs = git_get_references(); |
ea4a6df4 | 4517 | |
719dad28 | 4518 | my @commitlist = parse_commits($hash, 101, (100 * $page)); |
ea4a6df4 | 4519 | |
719dad28 | 4520 | my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1))); |
0d83ddc4 JN |
4521 | |
4522 | git_header_html(); | |
847e01fb | 4523 | git_print_page_nav('log','', $hash,undef,undef, $paging_nav); |
0d83ddc4 | 4524 | |
719dad28 | 4525 | if (!@commitlist) { |
847e01fb | 4526 | my %co = parse_commit($hash); |
27fb8c40 | 4527 | |
847e01fb | 4528 | git_print_header_div('summary', $project); |
e925f38c | 4529 | print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n"; |
161332a5 | 4530 | } |
719dad28 RF |
4531 | my $to = ($#commitlist >= 99) ? (99) : ($#commitlist); |
4532 | for (my $i = 0; $i <= $to; $i++) { | |
4533 | my %co = %{$commitlist[$i]}; | |
b87d78d6 | 4534 | next if !%co; |
719dad28 RF |
4535 | my $commit = $co{'id'}; |
4536 | my $ref = format_ref_marker($refs, $commit); | |
847e01fb JN |
4537 | my %ad = parse_date($co{'author_epoch'}); |
4538 | git_print_header_div('commit', | |
26298b5f JN |
4539 | "<span class=\"age\">$co{'age_string'}</span>" . |
4540 | esc_html($co{'title'}) . $ref, | |
4541 | $commit); | |
034df39e KS |
4542 | print "<div class=\"title_text\">\n" . |
4543 | "<div class=\"log_link\">\n" . | |
1c2a4f5a | 4544 | $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . |
952c65fc JN |
4545 | " | " . |
4546 | $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . | |
6ef4cb2e | 4547 | " | " . |
d7267207 | 4548 | $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . |
eb28240b | 4549 | "<br/>\n" . |
034df39e | 4550 | "</div>\n" . |
40c13813 | 4551 | "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" . |
d16d093c JN |
4552 | "</div>\n"; |
4553 | ||
4554 | print "<div class=\"log_body\">\n"; | |
f2069411 | 4555 | git_print_log($co{'comment'}, -final_empty_line=> 1); |
09bd7898 | 4556 | print "</div>\n"; |
719dad28 RF |
4557 | } |
4558 | if ($#commitlist >= 100) { | |
4559 | print "<div class=\"page_nav\">\n"; | |
7afd77bf | 4560 | print $cgi->a({-href => href(-replay=>1, page=>$page+1), |
719dad28 RF |
4561 | -accesskey => "n", -title => "Alt-n"}, "next"); |
4562 | print "</div>\n"; | |
e334d18c | 4563 | } |
034df39e | 4564 | git_footer_html(); |
09bd7898 KS |
4565 | } |
4566 | ||
4567 | sub git_commit { | |
9954f772 | 4568 | $hash ||= $hash_base || "HEAD"; |
847e01fb | 4569 | my %co = parse_commit($hash); |
034df39e | 4570 | if (!%co) { |
cac4bd94 | 4571 | die_error(undef, "Unknown commit object"); |
d63577da | 4572 | } |
847e01fb JN |
4573 | my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'}); |
4574 | my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'}); | |
161332a5 | 4575 | |
c9d193df JN |
4576 | my $parent = $co{'parent'}; |
4577 | my $parents = $co{'parents'}; # listref | |
4578 | ||
4579 | # we need to prepare $formats_nav before any parameter munging | |
4580 | my $formats_nav; | |
4581 | if (!defined $parent) { | |
4582 | # --root commitdiff | |
4583 | $formats_nav .= '(initial)'; | |
4584 | } elsif (@$parents == 1) { | |
4585 | # single parent commit | |
4586 | $formats_nav .= | |
4587 | '(parent: ' . | |
4588 | $cgi->a({-href => href(action=>"commit", | |
4589 | hash=>$parent)}, | |
4590 | esc_html(substr($parent, 0, 7))) . | |
4591 | ')'; | |
4592 | } else { | |
4593 | # merge commit | |
4594 | $formats_nav .= | |
4595 | '(merge: ' . | |
4596 | join(' ', map { | |
f9308a18 | 4597 | $cgi->a({-href => href(action=>"commit", |
c9d193df JN |
4598 | hash=>$_)}, |
4599 | esc_html(substr($_, 0, 7))); | |
4600 | } @$parents ) . | |
4601 | ')'; | |
4602 | } | |
4603 | ||
d8a20ba9 | 4604 | if (!defined $parent) { |
b9182987 | 4605 | $parent = "--root"; |
6191f8e1 | 4606 | } |
549ab4a3 | 4607 | my @difftree; |
208ecb2e JN |
4608 | open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id", |
4609 | @diff_opts, | |
4610 | (@$parents <= 1 ? $parent : '-c'), | |
4611 | $hash, "--" | |
4612 | or die_error(undef, "Open git-diff-tree failed"); | |
4613 | @difftree = map { chomp; $_ } <$fd>; | |
4614 | close $fd or die_error(undef, "Reading git-diff-tree failed"); | |
11044297 KS |
4615 | |
4616 | # non-textual hash id's can be cached | |
4617 | my $expires; | |
4618 | if ($hash =~ m/^[0-9a-fA-F]{40}$/) { | |
4619 | $expires = "+1d"; | |
4620 | } | |
847e01fb JN |
4621 | my $refs = git_get_references(); |
4622 | my $ref = format_ref_marker($refs, $co{'id'}); | |
ddb8d900 | 4623 | |
594e212b | 4624 | git_header_html(undef, $expires); |
a144154f | 4625 | git_print_page_nav('commit', '', |
952c65fc | 4626 | $hash, $co{'tree'}, $hash, |
c9d193df | 4627 | $formats_nav); |
4f7b34c9 | 4628 | |
b87d78d6 | 4629 | if (defined $co{'parent'}) { |
847e01fb | 4630 | git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash); |
b87d78d6 | 4631 | } else { |
847e01fb | 4632 | git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash); |
b87d78d6 | 4633 | } |
6191f8e1 | 4634 | print "<div class=\"title_text\">\n" . |
591ebf65 | 4635 | "<table class=\"object_header\">\n"; |
40c13813 | 4636 | print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n". |
bddec01d KS |
4637 | "<tr>" . |
4638 | "<td></td><td> $ad{'rfc2822'}"; | |
927dcec4 | 4639 | if ($ad{'hour_local'} < 6) { |
952c65fc JN |
4640 | printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", |
4641 | $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}); | |
b87d78d6 | 4642 | } else { |
952c65fc JN |
4643 | printf(" (%02d:%02d %s)", |
4644 | $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}); | |
b87d78d6 | 4645 | } |
bddec01d KS |
4646 | print "</td>" . |
4647 | "</tr>\n"; | |
40c13813 | 4648 | print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n"; |
952c65fc JN |
4649 | print "<tr><td></td><td> $cd{'rfc2822'}" . |
4650 | sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . | |
4651 | "</td></tr>\n"; | |
1f1ab5f0 | 4652 | print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n"; |
bddec01d KS |
4653 | print "<tr>" . |
4654 | "<td>tree</td>" . | |
1f1ab5f0 | 4655 | "<td class=\"sha1\">" . |
952c65fc JN |
4656 | $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash), |
4657 | class => "list"}, $co{'tree'}) . | |
19806691 | 4658 | "</td>" . |
952c65fc JN |
4659 | "<td class=\"link\">" . |
4660 | $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)}, | |
4661 | "tree"); | |
a3c8ab30 MM |
4662 | my $snapshot_links = format_snapshot_links($hash); |
4663 | if (defined $snapshot_links) { | |
4664 | print " | " . $snapshot_links; | |
cb9c6e5b AK |
4665 | } |
4666 | print "</td>" . | |
bddec01d | 4667 | "</tr>\n"; |
549ab4a3 | 4668 | |
3e029299 | 4669 | foreach my $par (@$parents) { |
bddec01d KS |
4670 | print "<tr>" . |
4671 | "<td>parent</td>" . | |
952c65fc JN |
4672 | "<td class=\"sha1\">" . |
4673 | $cgi->a({-href => href(action=>"commit", hash=>$par), | |
4674 | class => "list"}, $par) . | |
4675 | "</td>" . | |
bddec01d | 4676 | "<td class=\"link\">" . |
1c2a4f5a | 4677 | $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") . |
952c65fc | 4678 | " | " . |
f2e60947 | 4679 | $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") . |
bddec01d KS |
4680 | "</td>" . |
4681 | "</tr>\n"; | |
3e029299 | 4682 | } |
7a9b4c5f | 4683 | print "</table>". |
b87d78d6 | 4684 | "</div>\n"; |
d16d093c | 4685 | |
fbb592a9 | 4686 | print "<div class=\"page_body\">\n"; |
d16d093c | 4687 | git_print_log($co{'comment'}); |
927dcec4 | 4688 | print "</div>\n"; |
4a4a1a53 | 4689 | |
208ecb2e | 4690 | git_difftree_body(\@difftree, $hash, @$parents); |
4a4a1a53 | 4691 | |
12a88f2f | 4692 | git_footer_html(); |
09bd7898 KS |
4693 | } |
4694 | ||
ca94601c JN |
4695 | sub git_object { |
4696 | # object is defined by: | |
4697 | # - hash or hash_base alone | |
4698 | # - hash_base and file_name | |
4699 | my $type; | |
4700 | ||
4701 | # - hash or hash_base alone | |
4702 | if ($hash || ($hash_base && !defined $file_name)) { | |
4703 | my $object_id = $hash || $hash_base; | |
4704 | ||
4705 | my $git_command = git_cmd_str(); | |
4706 | open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null" | |
4707 | or die_error('404 Not Found', "Object does not exist"); | |
4708 | $type = <$fd>; | |
4709 | chomp $type; | |
4710 | close $fd | |
4711 | or die_error('404 Not Found', "Object does not exist"); | |
4712 | ||
4713 | # - hash_base and file_name | |
4714 | } elsif ($hash_base && defined $file_name) { | |
4715 | $file_name =~ s,/+$,,; | |
4716 | ||
4717 | system(git_cmd(), "cat-file", '-e', $hash_base) == 0 | |
4718 | or die_error('404 Not Found', "Base object does not exist"); | |
4719 | ||
4720 | # here errors should not hapen | |
4721 | open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name | |
4722 | or die_error(undef, "Open git-ls-tree failed"); | |
4723 | my $line = <$fd>; | |
4724 | close $fd; | |
4725 | ||
4726 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' | |
4727 | unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) { | |
4728 | die_error('404 Not Found', "File or directory for given base does not exist"); | |
4729 | } | |
4730 | $type = $2; | |
4731 | $hash = $3; | |
4732 | } else { | |
4733 | die_error('404 Not Found', "Not enough information to find object"); | |
4734 | } | |
4735 | ||
4736 | print $cgi->redirect(-uri => href(action=>$type, -full=>1, | |
4737 | hash=>$hash, hash_base=>$hash_base, | |
4738 | file_name=>$file_name), | |
4739 | -status => '302 Found'); | |
4740 | } | |
4741 | ||
09bd7898 | 4742 | sub git_blobdiff { |
9b71b1f6 JN |
4743 | my $format = shift || 'html'; |
4744 | ||
7c5e2ebb JN |
4745 | my $fd; |
4746 | my @difftree; | |
4747 | my %diffinfo; | |
9b71b1f6 | 4748 | my $expires; |
7c5e2ebb JN |
4749 | |
4750 | # preparing $fd and %diffinfo for git_patchset_body | |
4751 | # new style URI | |
4752 | if (defined $hash_base && defined $hash_parent_base) { | |
4753 | if (defined $file_name) { | |
4754 | # read raw output | |
45bd0c80 JN |
4755 | open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
4756 | $hash_parent_base, $hash_base, | |
5ae917ac | 4757 | "--", (defined $file_parent ? $file_parent : ()), $file_name |
7c5e2ebb JN |
4758 | or die_error(undef, "Open git-diff-tree failed"); |
4759 | @difftree = map { chomp; $_ } <$fd>; | |
4760 | close $fd | |
4761 | or die_error(undef, "Reading git-diff-tree failed"); | |
4762 | @difftree | |
4763 | or die_error('404 Not Found', "Blob diff not found"); | |
4764 | ||
0aea3376 JN |
4765 | } elsif (defined $hash && |
4766 | $hash =~ /[0-9a-fA-F]{40}/) { | |
4767 | # try to find filename from $hash | |
7c5e2ebb JN |
4768 | |
4769 | # read filtered raw output | |
45bd0c80 JN |
4770 | open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
4771 | $hash_parent_base, $hash_base, "--" | |
7c5e2ebb JN |
4772 | or die_error(undef, "Open git-diff-tree failed"); |
4773 | @difftree = | |
4774 | # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c' | |
4775 | # $hash == to_id | |
4776 | grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ } | |
4777 | map { chomp; $_ } <$fd>; | |
4778 | close $fd | |
4779 | or die_error(undef, "Reading git-diff-tree failed"); | |
4780 | @difftree | |
4781 | or die_error('404 Not Found', "Blob diff not found"); | |
4782 | ||
4783 | } else { | |
4784 | die_error('404 Not Found', "Missing one of the blob diff parameters"); | |
4785 | } | |
4786 | ||
4787 | if (@difftree > 1) { | |
4788 | die_error('404 Not Found', "Ambiguous blob diff specification"); | |
4789 | } | |
4790 | ||
4791 | %diffinfo = parse_difftree_raw_line($difftree[0]); | |
9d301456 JN |
4792 | $file_parent ||= $diffinfo{'from_file'} || $file_name; |
4793 | $file_name ||= $diffinfo{'to_file'}; | |
7c5e2ebb JN |
4794 | |
4795 | $hash_parent ||= $diffinfo{'from_id'}; | |
4796 | $hash ||= $diffinfo{'to_id'}; | |
4797 | ||
9b71b1f6 JN |
4798 | # non-textual hash id's can be cached |
4799 | if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ && | |
4800 | $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) { | |
4801 | $expires = '+1d'; | |
4802 | } | |
4803 | ||
7c5e2ebb | 4804 | # open patch output |
25691fbe | 4805 | open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
957d6ea7 JN |
4806 | '-p', ($format eq 'html' ? "--full-index" : ()), |
4807 | $hash_parent_base, $hash_base, | |
5ae917ac | 4808 | "--", (defined $file_parent ? $file_parent : ()), $file_name |
7c5e2ebb JN |
4809 | or die_error(undef, "Open git-diff-tree failed"); |
4810 | } | |
4811 | ||
4812 | # old/legacy style URI | |
4813 | if (!%diffinfo && # if new style URI failed | |
4814 | defined $hash && defined $hash_parent) { | |
4815 | # fake git-diff-tree raw output | |
4816 | $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob"; | |
4817 | $diffinfo{'from_id'} = $hash_parent; | |
4818 | $diffinfo{'to_id'} = $hash; | |
4819 | if (defined $file_name) { | |
4820 | if (defined $file_parent) { | |
4821 | $diffinfo{'status'} = '2'; | |
8391548e PB |
4822 | $diffinfo{'from_file'} = $file_parent; |
4823 | $diffinfo{'to_file'} = $file_name; | |
7c5e2ebb JN |
4824 | } else { # assume not renamed |
4825 | $diffinfo{'status'} = '1'; | |
8391548e PB |
4826 | $diffinfo{'from_file'} = $file_name; |
4827 | $diffinfo{'to_file'} = $file_name; | |
7c5e2ebb JN |
4828 | } |
4829 | } else { # no filename given | |
4830 | $diffinfo{'status'} = '2'; | |
4831 | $diffinfo{'from_file'} = $hash_parent; | |
4832 | $diffinfo{'to_file'} = $hash; | |
4833 | } | |
4834 | ||
9b71b1f6 JN |
4835 | # non-textual hash id's can be cached |
4836 | if ($hash =~ m/^[0-9a-fA-F]{40}$/ && | |
4837 | $hash_parent =~ m/^[0-9a-fA-F]{40}$/) { | |
4838 | $expires = '+1d'; | |
4839 | } | |
4840 | ||
4841 | # open patch output | |
957d6ea7 JN |
4842 | open $fd, "-|", git_cmd(), "diff", @diff_opts, |
4843 | '-p', ($format eq 'html' ? "--full-index" : ()), | |
45bd0c80 | 4844 | $hash_parent, $hash, "--" |
7c5e2ebb JN |
4845 | or die_error(undef, "Open git-diff failed"); |
4846 | } else { | |
4847 | die_error('404 Not Found', "Missing one of the blob diff parameters") | |
4848 | unless %diffinfo; | |
4849 | } | |
4850 | ||
4851 | # header | |
9b71b1f6 JN |
4852 | if ($format eq 'html') { |
4853 | my $formats_nav = | |
a3823e5a | 4854 | $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)}, |
35329cc1 | 4855 | "raw"); |
9b71b1f6 JN |
4856 | git_header_html(undef, $expires); |
4857 | if (defined $hash_base && (my %co = parse_commit($hash_base))) { | |
4858 | git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav); | |
4859 | git_print_header_div('commit', esc_html($co{'title'}), $hash_base); | |
4860 | } else { | |
4861 | print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n"; | |
4862 | print "<div class=\"title\">$hash vs $hash_parent</div>\n"; | |
4863 | } | |
4864 | if (defined $file_name) { | |
4865 | git_print_page_path($file_name, "blob", $hash_base); | |
4866 | } else { | |
4867 | print "<div class=\"page_path\"></div>\n"; | |
4868 | } | |
4869 | ||
4870 | } elsif ($format eq 'plain') { | |
4871 | print $cgi->header( | |
4872 | -type => 'text/plain', | |
4873 | -charset => 'utf-8', | |
4874 | -expires => $expires, | |
a2a3bf7b | 4875 | -content_disposition => 'inline; filename="' . "$file_name" . '.patch"'); |
9b71b1f6 JN |
4876 | |
4877 | print "X-Git-Url: " . $cgi->self_url() . "\n\n"; | |
4878 | ||
7c5e2ebb | 4879 | } else { |
9b71b1f6 | 4880 | die_error(undef, "Unknown blobdiff format"); |
7c5e2ebb JN |
4881 | } |
4882 | ||
4883 | # patch | |
9b71b1f6 JN |
4884 | if ($format eq 'html') { |
4885 | print "<div class=\"page_body\">\n"; | |
7c5e2ebb | 4886 | |
9b71b1f6 JN |
4887 | git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base); |
4888 | close $fd; | |
7c5e2ebb | 4889 | |
9b71b1f6 JN |
4890 | print "</div>\n"; # class="page_body" |
4891 | git_footer_html(); | |
4892 | ||
4893 | } else { | |
4894 | while (my $line = <$fd>) { | |
403d0906 JN |
4895 | $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg; |
4896 | $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg; | |
9b71b1f6 JN |
4897 | |
4898 | print $line; | |
4899 | ||
4900 | last if $line =~ m!^\+\+\+!; | |
4901 | } | |
4902 | local $/ = undef; | |
4903 | print <$fd>; | |
4904 | close $fd; | |
4905 | } | |
09bd7898 KS |
4906 | } |
4907 | ||
19806691 | 4908 | sub git_blobdiff_plain { |
9b71b1f6 | 4909 | git_blobdiff('plain'); |
19806691 KS |
4910 | } |
4911 | ||
09bd7898 | 4912 | sub git_commitdiff { |
eee08903 | 4913 | my $format = shift || 'html'; |
9954f772 | 4914 | $hash ||= $hash_base || "HEAD"; |
847e01fb | 4915 | my %co = parse_commit($hash); |
034df39e | 4916 | if (!%co) { |
cac4bd94 | 4917 | die_error(undef, "Unknown commit object"); |
d63577da | 4918 | } |
151602df | 4919 | |
cd030c3a JN |
4920 | # choose format for commitdiff for merge |
4921 | if (! defined $hash_parent && @{$co{'parents'}} > 1) { | |
4922 | $hash_parent = '--cc'; | |
4923 | } | |
4924 | # we need to prepare $formats_nav before almost any parameter munging | |
151602df JN |
4925 | my $formats_nav; |
4926 | if ($format eq 'html') { | |
4927 | $formats_nav = | |
a3823e5a | 4928 | $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)}, |
151602df JN |
4929 | "raw"); |
4930 | ||
cd030c3a JN |
4931 | if (defined $hash_parent && |
4932 | $hash_parent ne '-c' && $hash_parent ne '--cc') { | |
151602df JN |
4933 | # commitdiff with two commits given |
4934 | my $hash_parent_short = $hash_parent; | |
4935 | if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) { | |
4936 | $hash_parent_short = substr($hash_parent, 0, 7); | |
4937 | } | |
4938 | $formats_nav .= | |
ada3e1f7 JN |
4939 | ' (from'; |
4940 | for (my $i = 0; $i < @{$co{'parents'}}; $i++) { | |
4941 | if ($co{'parents'}[$i] eq $hash_parent) { | |
4942 | $formats_nav .= ' parent ' . ($i+1); | |
4943 | last; | |
4944 | } | |
4945 | } | |
4946 | $formats_nav .= ': ' . | |
151602df JN |
4947 | $cgi->a({-href => href(action=>"commitdiff", |
4948 | hash=>$hash_parent)}, | |
4949 | esc_html($hash_parent_short)) . | |
4950 | ')'; | |
4951 | } elsif (!$co{'parent'}) { | |
4952 | # --root commitdiff | |
4953 | $formats_nav .= ' (initial)'; | |
4954 | } elsif (scalar @{$co{'parents'}} == 1) { | |
4955 | # single parent commit | |
4956 | $formats_nav .= | |
4957 | ' (parent: ' . | |
4958 | $cgi->a({-href => href(action=>"commitdiff", | |
4959 | hash=>$co{'parent'})}, | |
4960 | esc_html(substr($co{'parent'}, 0, 7))) . | |
4961 | ')'; | |
4962 | } else { | |
4963 | # merge commit | |
cd030c3a JN |
4964 | if ($hash_parent eq '--cc') { |
4965 | $formats_nav .= ' | ' . | |
4966 | $cgi->a({-href => href(action=>"commitdiff", | |
4967 | hash=>$hash, hash_parent=>'-c')}, | |
4968 | 'combined'); | |
4969 | } else { # $hash_parent eq '-c' | |
4970 | $formats_nav .= ' | ' . | |
4971 | $cgi->a({-href => href(action=>"commitdiff", | |
4972 | hash=>$hash, hash_parent=>'--cc')}, | |
4973 | 'compact'); | |
4974 | } | |
151602df JN |
4975 | $formats_nav .= |
4976 | ' (merge: ' . | |
4977 | join(' ', map { | |
4978 | $cgi->a({-href => href(action=>"commitdiff", | |
4979 | hash=>$_)}, | |
4980 | esc_html(substr($_, 0, 7))); | |
4981 | } @{$co{'parents'}} ) . | |
4982 | ')'; | |
4983 | } | |
4984 | } | |
4985 | ||
fb1dde4a | 4986 | my $hash_parent_param = $hash_parent; |
cd030c3a JN |
4987 | if (!defined $hash_parent_param) { |
4988 | # --cc for multiple parents, --root for parentless | |
fb1dde4a | 4989 | $hash_parent_param = |
cd030c3a | 4990 | @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root'; |
bddec01d | 4991 | } |
eee08903 JN |
4992 | |
4993 | # read commitdiff | |
4994 | my $fd; | |
4995 | my @difftree; | |
eee08903 | 4996 | if ($format eq 'html') { |
25691fbe | 4997 | open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
45bd0c80 | 4998 | "--no-commit-id", "--patch-with-raw", "--full-index", |
fb1dde4a | 4999 | $hash_parent_param, $hash, "--" |
eee08903 JN |
5000 | or die_error(undef, "Open git-diff-tree failed"); |
5001 | ||
04408c35 JN |
5002 | while (my $line = <$fd>) { |
5003 | chomp $line; | |
eee08903 JN |
5004 | # empty line ends raw part of diff-tree output |
5005 | last unless $line; | |
493e01db | 5006 | push @difftree, scalar parse_difftree_raw_line($line); |
eee08903 | 5007 | } |
eee08903 | 5008 | |
eee08903 | 5009 | } elsif ($format eq 'plain') { |
25691fbe | 5010 | open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
fb1dde4a | 5011 | '-p', $hash_parent_param, $hash, "--" |
eee08903 | 5012 | or die_error(undef, "Open git-diff-tree failed"); |
157e43b4 | 5013 | |
eee08903 JN |
5014 | } else { |
5015 | die_error(undef, "Unknown commitdiff format"); | |
5016 | } | |
161332a5 | 5017 | |
11044297 KS |
5018 | # non-textual hash id's can be cached |
5019 | my $expires; | |
5020 | if ($hash =~ m/^[0-9a-fA-F]{40}$/) { | |
5021 | $expires = "+1d"; | |
5022 | } | |
09bd7898 | 5023 | |
eee08903 JN |
5024 | # write commit message |
5025 | if ($format eq 'html') { | |
5026 | my $refs = git_get_references(); | |
5027 | my $ref = format_ref_marker($refs, $co{'id'}); | |
1b1cd421 | 5028 | |
eee08903 JN |
5029 | git_header_html(undef, $expires); |
5030 | git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav); | |
5031 | git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash); | |
6fd92a28 | 5032 | git_print_authorship(\%co); |
eee08903 | 5033 | print "<div class=\"page_body\">\n"; |
82560983 JN |
5034 | if (@{$co{'comment'}} > 1) { |
5035 | print "<div class=\"log\">\n"; | |
5036 | git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1); | |
5037 | print "</div>\n"; # class="log" | |
5038 | } | |
eee08903 JN |
5039 | |
5040 | } elsif ($format eq 'plain') { | |
5041 | my $refs = git_get_references("tags"); | |
edf735ab | 5042 | my $tagname = git_get_rev_name_tags($hash); |
eee08903 JN |
5043 | my $filename = basename($project) . "-$hash.patch"; |
5044 | ||
5045 | print $cgi->header( | |
5046 | -type => 'text/plain', | |
5047 | -charset => 'utf-8', | |
5048 | -expires => $expires, | |
a2a3bf7b | 5049 | -content_disposition => 'inline; filename="' . "$filename" . '"'); |
eee08903 JN |
5050 | my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'}); |
5051 | print <<TEXT; | |
59b9f61a JN |
5052 | From: $co{'author'} |
5053 | Date: $ad{'rfc2822'} ($ad{'tz_local'}) | |
5054 | Subject: $co{'title'} | |
5055 | TEXT | |
edf735ab | 5056 | print "X-Git-Tag: $tagname\n" if $tagname; |
eee08903 | 5057 | print "X-Git-Url: " . $cgi->self_url() . "\n\n"; |
edf735ab | 5058 | |
eee08903 JN |
5059 | foreach my $line (@{$co{'comment'}}) { |
5060 | print "$line\n"; | |
5061 | } | |
5062 | print "---\n\n"; | |
1b1cd421 | 5063 | } |
1b1cd421 | 5064 | |
eee08903 JN |
5065 | # write patch |
5066 | if ($format eq 'html') { | |
cd030c3a JN |
5067 | my $use_parents = !defined $hash_parent || |
5068 | $hash_parent eq '-c' || $hash_parent eq '--cc'; | |
5069 | git_difftree_body(\@difftree, $hash, | |
5070 | $use_parents ? @{$co{'parents'}} : $hash_parent); | |
b4657e77 | 5071 | print "<br/>\n"; |
1b1cd421 | 5072 | |
cd030c3a JN |
5073 | git_patchset_body($fd, \@difftree, $hash, |
5074 | $use_parents ? @{$co{'parents'}} : $hash_parent); | |
157e43b4 | 5075 | close $fd; |
eee08903 JN |
5076 | print "</div>\n"; # class="page_body" |
5077 | git_footer_html(); | |
5078 | ||
5079 | } elsif ($format eq 'plain') { | |
5080 | local $/ = undef; | |
5081 | print <$fd>; | |
5082 | close $fd | |
5083 | or print "Reading git-diff-tree failed\n"; | |
19806691 KS |
5084 | } |
5085 | } | |
5086 | ||
eee08903 JN |
5087 | sub git_commitdiff_plain { |
5088 | git_commitdiff('plain'); | |
5089 | } | |
5090 | ||
09bd7898 | 5091 | sub git_history { |
c6e1d9ed | 5092 | if (!defined $hash_base) { |
847e01fb | 5093 | $hash_base = git_get_head_hash($project); |
09bd7898 | 5094 | } |
8be68352 JN |
5095 | if (!defined $page) { |
5096 | $page = 0; | |
5097 | } | |
63433106 | 5098 | my $ftype; |
847e01fb | 5099 | my %co = parse_commit($hash_base); |
09bd7898 | 5100 | if (!%co) { |
cac4bd94 | 5101 | die_error(undef, "Unknown commit object"); |
2ae100df | 5102 | } |
8be68352 | 5103 | |
847e01fb | 5104 | my $refs = git_get_references(); |
8be68352 JN |
5105 | my $limit = sprintf("--max-count=%i", (100 * ($page+1))); |
5106 | ||
93d5f061 LT |
5107 | if (!defined $hash && defined $file_name) { |
5108 | $hash = git_get_hash_by_path($hash_base, $file_name); | |
5109 | } | |
cff0771b | 5110 | if (defined $hash) { |
63433106 | 5111 | $ftype = git_get_type($hash); |
cff0771b | 5112 | } |
10dba28d | 5113 | |
a8b983bf | 5114 | my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name); |
25691fbe | 5115 | |
8be68352 JN |
5116 | my $paging_nav = ''; |
5117 | if ($page > 0) { | |
5118 | $paging_nav .= | |
5119 | $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, | |
5120 | file_name=>$file_name)}, | |
5121 | "first"); | |
5122 | $paging_nav .= " ⋅ " . | |
7afd77bf | 5123 | $cgi->a({-href => href(-replay=>1, page=>$page-1), |
8be68352 JN |
5124 | -accesskey => "p", -title => "Alt-p"}, "prev"); |
5125 | } else { | |
5126 | $paging_nav .= "first"; | |
5127 | $paging_nav .= " ⋅ prev"; | |
5128 | } | |
8be68352 | 5129 | my $next_link = ''; |
a8b983bf | 5130 | if ($#commitlist >= 100) { |
8be68352 | 5131 | $next_link = |
7afd77bf | 5132 | $cgi->a({-href => href(-replay=>1, page=>$page+1), |
a8b983bf | 5133 | -accesskey => "n", -title => "Alt-n"}, "next"); |
7afd77bf JN |
5134 | $paging_nav .= " ⋅ $next_link"; |
5135 | } else { | |
5136 | $paging_nav .= " ⋅ next"; | |
8be68352 JN |
5137 | } |
5138 | ||
5139 | git_header_html(); | |
5140 | git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav); | |
5141 | git_print_header_div('commit', esc_html($co{'title'}), $hash_base); | |
5142 | git_print_page_path($file_name, $ftype, $hash_base); | |
5143 | ||
a8b983bf | 5144 | git_history_body(\@commitlist, 0, 99, |
8be68352 | 5145 | $refs, $hash_base, $ftype, $next_link); |
581860e1 | 5146 | |
d51e902a | 5147 | git_footer_html(); |
161332a5 | 5148 | } |
19806691 KS |
5149 | |
5150 | sub git_search { | |
6be93511 RF |
5151 | my ($have_search) = gitweb_check_feature('search'); |
5152 | if (!$have_search) { | |
5153 | die_error('403 Permission denied', "Permission denied"); | |
5154 | } | |
19806691 | 5155 | if (!defined $searchtext) { |
cac4bd94 | 5156 | die_error(undef, "Text field empty"); |
19806691 KS |
5157 | } |
5158 | if (!defined $hash) { | |
847e01fb | 5159 | $hash = git_get_head_hash($project); |
19806691 | 5160 | } |
847e01fb | 5161 | my %co = parse_commit($hash); |
19806691 | 5162 | if (!%co) { |
cac4bd94 | 5163 | die_error(undef, "Unknown commit object"); |
19806691 | 5164 | } |
8dbc0fce RF |
5165 | if (!defined $page) { |
5166 | $page = 0; | |
5167 | } | |
04f7a94f | 5168 | |
88ad729b PB |
5169 | $searchtype ||= 'commit'; |
5170 | if ($searchtype eq 'pickaxe') { | |
04f7a94f JN |
5171 | # pickaxe may take all resources of your box and run for several minutes |
5172 | # with every query - so decide by yourself how public you make this feature | |
5173 | my ($have_pickaxe) = gitweb_check_feature('pickaxe'); | |
5174 | if (!$have_pickaxe) { | |
5175 | die_error('403 Permission denied', "Permission denied"); | |
5176 | } | |
c994d620 | 5177 | } |
e7738553 PB |
5178 | if ($searchtype eq 'grep') { |
5179 | my ($have_grep) = gitweb_check_feature('grep'); | |
5180 | if (!$have_grep) { | |
5181 | die_error('403 Permission denied', "Permission denied"); | |
5182 | } | |
5183 | } | |
88ad729b | 5184 | |
19806691 | 5185 | git_header_html(); |
19806691 | 5186 | |
88ad729b | 5187 | if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') { |
8e574fb5 RF |
5188 | my $greptype; |
5189 | if ($searchtype eq 'commit') { | |
5190 | $greptype = "--grep="; | |
5191 | } elsif ($searchtype eq 'author') { | |
5192 | $greptype = "--author="; | |
5193 | } elsif ($searchtype eq 'committer') { | |
5194 | $greptype = "--committer="; | |
5195 | } | |
7e431ef9 | 5196 | $greptype .= $search_regexp; |
5ad66088 | 5197 | my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype); |
8dbc0fce RF |
5198 | |
5199 | my $paging_nav = ''; | |
5200 | if ($page > 0) { | |
5201 | $paging_nav .= | |
5202 | $cgi->a({-href => href(action=>"search", hash=>$hash, | |
a23f0a73 JN |
5203 | searchtext=>$searchtext, searchtype=>$searchtype)}, |
5204 | "first"); | |
8dbc0fce | 5205 | $paging_nav .= " ⋅ " . |
7afd77bf | 5206 | $cgi->a({-href => href(-replay=>1, page=>$page-1), |
a23f0a73 | 5207 | -accesskey => "p", -title => "Alt-p"}, "prev"); |
8dbc0fce RF |
5208 | } else { |
5209 | $paging_nav .= "first"; | |
5210 | $paging_nav .= " ⋅ prev"; | |
5211 | } | |
7afd77bf | 5212 | my $next_link = ''; |
5ad66088 | 5213 | if ($#commitlist >= 100) { |
7afd77bf JN |
5214 | $next_link = |
5215 | $cgi->a({-href => href(-replay=>1, page=>$page+1), | |
a23f0a73 | 5216 | -accesskey => "n", -title => "Alt-n"}, "next"); |
7afd77bf | 5217 | $paging_nav .= " ⋅ $next_link"; |
8dbc0fce RF |
5218 | } else { |
5219 | $paging_nav .= " ⋅ next"; | |
5220 | } | |
7afd77bf | 5221 | |
5ad66088 | 5222 | if ($#commitlist >= 100) { |
8dbc0fce RF |
5223 | } |
5224 | ||
5225 | git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav); | |
5226 | git_print_header_div('commit', esc_html($co{'title'}), $hash); | |
5ad66088 | 5227 | git_search_grep_body(\@commitlist, 0, 99, $next_link); |
c994d620 KS |
5228 | } |
5229 | ||
88ad729b | 5230 | if ($searchtype eq 'pickaxe') { |
8dbc0fce RF |
5231 | git_print_page_nav('','', $hash,$co{'tree'},$hash); |
5232 | git_print_header_div('commit', esc_html($co{'title'}), $hash); | |
5233 | ||
591ebf65 | 5234 | print "<table class=\"pickaxe search\">\n"; |
8dbc0fce | 5235 | my $alternate = 1; |
c994d620 | 5236 | $/ = "\n"; |
25691fbe | 5237 | my $git_command = git_cmd_str(); |
4229aa51 PB |
5238 | my $searchqtext = $searchtext; |
5239 | $searchqtext =~ s/'/'\\''/; | |
25691fbe | 5240 | open my $fd, "-|", "$git_command rev-list $hash | " . |
4229aa51 | 5241 | "$git_command diff-tree -r --stdin -S\'$searchqtext\'"; |
c994d620 KS |
5242 | undef %co; |
5243 | my @files; | |
5244 | while (my $line = <$fd>) { | |
5245 | if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) { | |
5246 | my %set; | |
5247 | $set{'file'} = $6; | |
5248 | $set{'from_id'} = $3; | |
5249 | $set{'to_id'} = $4; | |
5250 | $set{'id'} = $set{'to_id'}; | |
5251 | if ($set{'id'} =~ m/0{40}/) { | |
5252 | $set{'id'} = $set{'from_id'}; | |
19806691 | 5253 | } |
c994d620 KS |
5254 | if ($set{'id'} =~ m/0{40}/) { |
5255 | next; | |
5256 | } | |
5257 | push @files, \%set; | |
53b89d8d | 5258 | } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){ |
c994d620 KS |
5259 | if (%co) { |
5260 | if ($alternate) { | |
5261 | print "<tr class=\"dark\">\n"; | |
5262 | } else { | |
5263 | print "<tr class=\"light\">\n"; | |
5264 | } | |
5265 | $alternate ^= 1; | |
ce58ec91 | 5266 | my $author = chop_and_escape_str($co{'author_name'}, 15, 5); |
71be1e79 | 5267 | print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" . |
e076a0e7 | 5268 | "<td><i>" . $author . "</i></td>\n" . |
c994d620 | 5269 | "<td>" . |
952c65fc JN |
5270 | $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), |
5271 | -class => "list subject"}, | |
d3cd2495 | 5272 | chop_and_escape_str($co{'title'}, 50) . "<br/>"); |
c994d620 KS |
5273 | while (my $setref = shift @files) { |
5274 | my %set = %$setref; | |
952c65fc JN |
5275 | print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'}, |
5276 | hash=>$set{'id'}, file_name=>$set{'file'}), | |
5277 | -class => "list"}, | |
403d0906 | 5278 | "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") . |
c994d620 KS |
5279 | "<br/>\n"; |
5280 | } | |
5281 | print "</td>\n" . | |
5282 | "<td class=\"link\">" . | |
756d2f06 | 5283 | $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") . |
952c65fc JN |
5284 | " | " . |
5285 | $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree"); | |
c994d620 KS |
5286 | print "</td>\n" . |
5287 | "</tr>\n"; | |
5288 | } | |
847e01fb | 5289 | %co = parse_commit($1); |
19806691 | 5290 | } |
19806691 | 5291 | } |
c994d620 | 5292 | close $fd; |
8dbc0fce RF |
5293 | |
5294 | print "</table>\n"; | |
19806691 | 5295 | } |
e7738553 PB |
5296 | |
5297 | if ($searchtype eq 'grep') { | |
5298 | git_print_page_nav('','', $hash,$co{'tree'},$hash); | |
5299 | git_print_header_div('commit', esc_html($co{'title'}), $hash); | |
5300 | ||
591ebf65 | 5301 | print "<table class=\"grep_search\">\n"; |
e7738553 PB |
5302 | my $alternate = 1; |
5303 | my $matches = 0; | |
5304 | $/ = "\n"; | |
5305 | open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'}; | |
5306 | my $lastfile = ''; | |
5307 | while (my $line = <$fd>) { | |
5308 | chomp $line; | |
5309 | my ($file, $lno, $ltext, $binary); | |
5310 | last if ($matches++ > 1000); | |
5311 | if ($line =~ /^Binary file (.+) matches$/) { | |
5312 | $file = $1; | |
5313 | $binary = 1; | |
5314 | } else { | |
5315 | (undef, $file, $lno, $ltext) = split(/:/, $line, 4); | |
5316 | } | |
5317 | if ($file ne $lastfile) { | |
5318 | $lastfile and print "</td></tr>\n"; | |
5319 | if ($alternate++) { | |
5320 | print "<tr class=\"dark\">\n"; | |
5321 | } else { | |
5322 | print "<tr class=\"light\">\n"; | |
5323 | } | |
5324 | print "<td class=\"list\">". | |
5325 | $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'}, | |
5326 | file_name=>"$file"), | |
5327 | -class => "list"}, esc_path($file)); | |
5328 | print "</td><td>\n"; | |
5329 | $lastfile = $file; | |
5330 | } | |
5331 | if ($binary) { | |
5332 | print "<div class=\"binary\">Binary file</div>\n"; | |
5333 | } else { | |
5334 | $ltext = untabify($ltext); | |
5335 | if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) { | |
5336 | $ltext = esc_html($1, -nbsp=>1); | |
5337 | $ltext .= '<span class="match">'; | |
5338 | $ltext .= esc_html($2, -nbsp=>1); | |
5339 | $ltext .= '</span>'; | |
5340 | $ltext .= esc_html($3, -nbsp=>1); | |
5341 | } else { | |
5342 | $ltext = esc_html($ltext, -nbsp=>1); | |
5343 | } | |
5344 | print "<div class=\"pre\">" . | |
5345 | $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'}, | |
5346 | file_name=>"$file").'#l'.$lno, | |
5347 | -class => "linenr"}, sprintf('%4i', $lno)) | |
5348 | . ' ' . $ltext . "</div>\n"; | |
5349 | } | |
5350 | } | |
5351 | if ($lastfile) { | |
5352 | print "</td></tr>\n"; | |
5353 | if ($matches > 1000) { | |
5354 | print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n"; | |
5355 | } | |
5356 | } else { | |
5357 | print "<div class=\"diff nodifferences\">No matches found</div>\n"; | |
5358 | } | |
5359 | close $fd; | |
5360 | ||
5361 | print "</table>\n"; | |
5362 | } | |
19806691 KS |
5363 | git_footer_html(); |
5364 | } | |
5365 | ||
88ad729b PB |
5366 | sub git_search_help { |
5367 | git_header_html(); | |
5368 | git_print_page_nav('','', $hash,$hash,$hash); | |
5369 | print <<EOT; | |
5370 | <dl> | |
5371 | <dt><b>commit</b></dt> | |
5372 | <dd>The commit messages and authorship information will be scanned for the given string.</dd> | |
e7738553 PB |
5373 | EOT |
5374 | my ($have_grep) = gitweb_check_feature('grep'); | |
5375 | if ($have_grep) { | |
5376 | print <<EOT; | |
5377 | <dt><b>grep</b></dt> | |
5378 | <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing | |
5379 | a different one) are searched for the given | |
5380 | <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> | |
5381 | (POSIX extended) and the matches are listed. On large | |
5382 | trees, this search can take a while and put some strain on the server, so please use it with | |
5383 | some consideration.</dd> | |
5384 | EOT | |
5385 | } | |
5386 | print <<EOT; | |
88ad729b PB |
5387 | <dt><b>author</b></dt> |
5388 | <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd> | |
5389 | <dt><b>committer</b></dt> | |
5390 | <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd> | |
5391 | EOT | |
5392 | my ($have_pickaxe) = gitweb_check_feature('pickaxe'); | |
5393 | if ($have_pickaxe) { | |
5394 | print <<EOT; | |
5395 | <dt><b>pickaxe</b></dt> | |
5396 | <dd>All commits that caused the string to appear or disappear from any file (changes that | |
5397 | added, removed or "modified" the string) will be listed. This search can take a while and | |
5398 | takes a lot of strain on the server, so please use it wisely.</dd> | |
5399 | EOT | |
5400 | } | |
5401 | print "</dl>\n"; | |
5402 | git_footer_html(); | |
5403 | } | |
5404 | ||
19806691 | 5405 | sub git_shortlog { |
847e01fb | 5406 | my $head = git_get_head_hash($project); |
19806691 KS |
5407 | if (!defined $hash) { |
5408 | $hash = $head; | |
5409 | } | |
ea4a6df4 KS |
5410 | if (!defined $page) { |
5411 | $page = 0; | |
5412 | } | |
847e01fb | 5413 | my $refs = git_get_references(); |
ea4a6df4 | 5414 | |
d222984e | 5415 | my @commitlist = parse_commits($hash, 101, (100 * $page)); |
ea4a6df4 | 5416 | |
190d7fdc | 5417 | my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1))); |
9f5dcb81 | 5418 | my $next_link = ''; |
190d7fdc | 5419 | if ($#commitlist >= 100) { |
9f5dcb81 | 5420 | $next_link = |
7afd77bf | 5421 | $cgi->a({-href => href(-replay=>1, page=>$page+1), |
190d7fdc | 5422 | -accesskey => "n", -title => "Alt-n"}, "next"); |
9f5dcb81 JN |
5423 | } |
5424 | ||
0d83ddc4 | 5425 | git_header_html(); |
847e01fb JN |
5426 | git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav); |
5427 | git_print_header_div('summary', $project); | |
0d83ddc4 | 5428 | |
190d7fdc | 5429 | git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link); |
9f5dcb81 | 5430 | |
19806691 KS |
5431 | git_footer_html(); |
5432 | } | |
717b8311 JN |
5433 | |
5434 | ## ...................................................................... | |
af6feeb2 | 5435 | ## feeds (RSS, Atom; OPML) |
717b8311 | 5436 | |
af6feeb2 JN |
5437 | sub git_feed { |
5438 | my $format = shift || 'atom'; | |
5439 | my ($have_blame) = gitweb_check_feature('blame'); | |
5440 | ||
5441 | # Atom: http://www.atomenabled.org/developers/syndication/ | |
5442 | # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ | |
5443 | if ($format ne 'rss' && $format ne 'atom') { | |
5444 | die_error(undef, "Unknown web feed format"); | |
5445 | } | |
5446 | ||
5447 | # log/feed of current (HEAD) branch, log of given branch, history of file/directory | |
5448 | my $head = $hash || 'HEAD'; | |
33f24330 | 5449 | my @commitlist = parse_commits($head, 150, 0, undef, $file_name); |
af6feeb2 JN |
5450 | |
5451 | my %latest_commit; | |
5452 | my %latest_date; | |
5453 | my $content_type = "application/$format+xml"; | |
5454 | if (defined $cgi->http('HTTP_ACCEPT') && | |
5455 | $cgi->Accept('text/xml') > $cgi->Accept($content_type)) { | |
5456 | # browser (feed reader) prefers text/xml | |
5457 | $content_type = 'text/xml'; | |
5458 | } | |
b6093a5c RF |
5459 | if (defined($commitlist[0])) { |
5460 | %latest_commit = %{$commitlist[0]}; | |
91fd2bf3 | 5461 | %latest_date = parse_date($latest_commit{'author_epoch'}); |
af6feeb2 JN |
5462 | print $cgi->header( |
5463 | -type => $content_type, | |
5464 | -charset => 'utf-8', | |
5465 | -last_modified => $latest_date{'rfc2822'}); | |
5466 | } else { | |
5467 | print $cgi->header( | |
5468 | -type => $content_type, | |
5469 | -charset => 'utf-8'); | |
5470 | } | |
5471 | ||
5472 | # Optimization: skip generating the body if client asks only | |
5473 | # for Last-Modified date. | |
5474 | return if ($cgi->request_method() eq 'HEAD'); | |
5475 | ||
5476 | # header variables | |
5477 | my $title = "$site_name - $project/$action"; | |
5478 | my $feed_type = 'log'; | |
5479 | if (defined $hash) { | |
5480 | $title .= " - '$hash'"; | |
5481 | $feed_type = 'branch log'; | |
5482 | if (defined $file_name) { | |
5483 | $title .= " :: $file_name"; | |
5484 | $feed_type = 'history'; | |
5485 | } | |
5486 | } elsif (defined $file_name) { | |
5487 | $title .= " - $file_name"; | |
5488 | $feed_type = 'history'; | |
5489 | } | |
5490 | $title .= " $feed_type"; | |
5491 | my $descr = git_get_project_description($project); | |
5492 | if (defined $descr) { | |
5493 | $descr = esc_html($descr); | |
5494 | } else { | |
5495 | $descr = "$project " . | |
5496 | ($format eq 'rss' ? 'RSS' : 'Atom') . | |
5497 | " feed"; | |
5498 | } | |
5499 | my $owner = git_get_project_owner($project); | |
5500 | $owner = esc_html($owner); | |
5501 | ||
5502 | #header | |
5503 | my $alt_url; | |
5504 | if (defined $file_name) { | |
5505 | $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name); | |
5506 | } elsif (defined $hash) { | |
5507 | $alt_url = href(-full=>1, action=>"log", hash=>$hash); | |
5508 | } else { | |
5509 | $alt_url = href(-full=>1, action=>"summary"); | |
5510 | } | |
5511 | print qq!<?xml version="1.0" encoding="utf-8"?>\n!; | |
5512 | if ($format eq 'rss') { | |
5513 | print <<XML; | |
59b9f61a JN |
5514 | <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"> |
5515 | <channel> | |
59b9f61a | 5516 | XML |
af6feeb2 JN |
5517 | print "<title>$title</title>\n" . |
5518 | "<link>$alt_url</link>\n" . | |
5519 | "<description>$descr</description>\n" . | |
5520 | "<language>en</language>\n"; | |
5521 | } elsif ($format eq 'atom') { | |
5522 | print <<XML; | |
5523 | <feed xmlns="http://www.w3.org/2005/Atom"> | |
5524 | XML | |
5525 | print "<title>$title</title>\n" . | |
5526 | "<subtitle>$descr</subtitle>\n" . | |
5527 | '<link rel="alternate" type="text/html" href="' . | |
5528 | $alt_url . '" />' . "\n" . | |
5529 | '<link rel="self" type="' . $content_type . '" href="' . | |
5530 | $cgi->self_url() . '" />' . "\n" . | |
5531 | "<id>" . href(-full=>1) . "</id>\n" . | |
5532 | # use project owner for feed author | |
5533 | "<author><name>$owner</name></author>\n"; | |
5534 | if (defined $favicon) { | |
5535 | print "<icon>" . esc_url($favicon) . "</icon>\n"; | |
5536 | } | |
5537 | if (defined $logo_url) { | |
5538 | # not twice as wide as tall: 72 x 27 pixels | |
e1147267 | 5539 | print "<logo>" . esc_url($logo) . "</logo>\n"; |
af6feeb2 JN |
5540 | } |
5541 | if (! %latest_date) { | |
5542 | # dummy date to keep the feed valid until commits trickle in: | |
5543 | print "<updated>1970-01-01T00:00:00Z</updated>\n"; | |
5544 | } else { | |
5545 | print "<updated>$latest_date{'iso-8601'}</updated>\n"; | |
5546 | } | |
5547 | } | |
717b8311 | 5548 | |
af6feeb2 | 5549 | # contents |
b6093a5c RF |
5550 | for (my $i = 0; $i <= $#commitlist; $i++) { |
5551 | my %co = %{$commitlist[$i]}; | |
5552 | my $commit = $co{'id'}; | |
717b8311 | 5553 | # we read 150, we always show 30 and the ones more recent than 48 hours |
91fd2bf3 | 5554 | if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) { |
717b8311 JN |
5555 | last; |
5556 | } | |
91fd2bf3 | 5557 | my %cd = parse_date($co{'author_epoch'}); |
af6feeb2 JN |
5558 | |
5559 | # get list of changed files | |
b6093a5c | 5560 | open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, |
c906b181 JN |
5561 | $co{'parent'} || "--root", |
5562 | $co{'id'}, "--", (defined $file_name ? $file_name : ()) | |
6bcf4b46 | 5563 | or next; |
717b8311 | 5564 | my @difftree = map { chomp; $_ } <$fd>; |
6bcf4b46 JN |
5565 | close $fd |
5566 | or next; | |
af6feeb2 JN |
5567 | |
5568 | # print element (entry, item) | |
5569 | my $co_url = href(-full=>1, action=>"commit", hash=>$commit); | |
5570 | if ($format eq 'rss') { | |
5571 | print "<item>\n" . | |
5572 | "<title>" . esc_html($co{'title'}) . "</title>\n" . | |
5573 | "<author>" . esc_html($co{'author'}) . "</author>\n" . | |
5574 | "<pubDate>$cd{'rfc2822'}</pubDate>\n" . | |
5575 | "<guid isPermaLink=\"true\">$co_url</guid>\n" . | |
5576 | "<link>$co_url</link>\n" . | |
5577 | "<description>" . esc_html($co{'title'}) . "</description>\n" . | |
5578 | "<content:encoded>" . | |
5579 | "<![CDATA[\n"; | |
5580 | } elsif ($format eq 'atom') { | |
5581 | print "<entry>\n" . | |
5582 | "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" . | |
5583 | "<updated>$cd{'iso-8601'}</updated>\n" . | |
ab23c19d JN |
5584 | "<author>\n" . |
5585 | " <name>" . esc_html($co{'author_name'}) . "</name>\n"; | |
5586 | if ($co{'author_email'}) { | |
5587 | print " <email>" . esc_html($co{'author_email'}) . "</email>\n"; | |
5588 | } | |
5589 | print "</author>\n" . | |
af6feeb2 | 5590 | # use committer for contributor |
ab23c19d JN |
5591 | "<contributor>\n" . |
5592 | " <name>" . esc_html($co{'committer_name'}) . "</name>\n"; | |
5593 | if ($co{'committer_email'}) { | |
5594 | print " <email>" . esc_html($co{'committer_email'}) . "</email>\n"; | |
5595 | } | |
5596 | print "</contributor>\n" . | |
af6feeb2 JN |
5597 | "<published>$cd{'iso-8601'}</published>\n" . |
5598 | "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" . | |
5599 | "<id>$co_url</id>\n" . | |
5600 | "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" . | |
5601 | "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n"; | |
5602 | } | |
717b8311 | 5603 | my $comment = $co{'comment'}; |
af6feeb2 | 5604 | print "<pre>\n"; |
717b8311 | 5605 | foreach my $line (@$comment) { |
af6feeb2 JN |
5606 | $line = esc_html($line); |
5607 | print "$line\n"; | |
717b8311 | 5608 | } |
af6feeb2 JN |
5609 | print "</pre><ul>\n"; |
5610 | foreach my $difftree_line (@difftree) { | |
5611 | my %difftree = parse_difftree_raw_line($difftree_line); | |
5612 | next if !$difftree{'from_id'}; | |
5613 | ||
5614 | my $file = $difftree{'file'} || $difftree{'to_file'}; | |
5615 | ||
5616 | print "<li>" . | |
5617 | "[" . | |
5618 | $cgi->a({-href => href(-full=>1, action=>"blobdiff", | |
5619 | hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'}, | |
5620 | hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'}, | |
5621 | file_name=>$file, file_parent=>$difftree{'from_file'}), | |
5622 | -title => "diff"}, 'D'); | |
5623 | if ($have_blame) { | |
5624 | print $cgi->a({-href => href(-full=>1, action=>"blame", | |
5625 | file_name=>$file, hash_base=>$commit), | |
5626 | -title => "blame"}, 'B'); | |
717b8311 | 5627 | } |
af6feeb2 JN |
5628 | # if this is not a feed of a file history |
5629 | if (!defined $file_name || $file_name ne $file) { | |
5630 | print $cgi->a({-href => href(-full=>1, action=>"history", | |
5631 | file_name=>$file, hash=>$commit), | |
5632 | -title => "history"}, 'H'); | |
5633 | } | |
5634 | $file = esc_path($file); | |
5635 | print "] ". | |
5636 | "$file</li>\n"; | |
5637 | } | |
5638 | if ($format eq 'rss') { | |
5639 | print "</ul>]]>\n" . | |
5640 | "</content:encoded>\n" . | |
5641 | "</item>\n"; | |
5642 | } elsif ($format eq 'atom') { | |
5643 | print "</ul>\n</div>\n" . | |
5644 | "</content>\n" . | |
5645 | "</entry>\n"; | |
717b8311 | 5646 | } |
717b8311 | 5647 | } |
af6feeb2 JN |
5648 | |
5649 | # end of feed | |
5650 | if ($format eq 'rss') { | |
5651 | print "</channel>\n</rss>\n"; | |
5652 | } elsif ($format eq 'atom') { | |
5653 | print "</feed>\n"; | |
5654 | } | |
5655 | } | |
5656 | ||
5657 | sub git_rss { | |
5658 | git_feed('rss'); | |
5659 | } | |
5660 | ||
5661 | sub git_atom { | |
5662 | git_feed('atom'); | |
717b8311 JN |
5663 | } |
5664 | ||
5665 | sub git_opml { | |
847e01fb | 5666 | my @list = git_get_projects_list(); |
717b8311 JN |
5667 | |
5668 | print $cgi->header(-type => 'text/xml', -charset => 'utf-8'); | |
59b9f61a JN |
5669 | print <<XML; |
5670 | <?xml version="1.0" encoding="utf-8"?> | |
5671 | <opml version="1.0"> | |
5672 | <head> | |
8be2890c | 5673 | <title>$site_name OPML Export</title> |
59b9f61a JN |
5674 | </head> |
5675 | <body> | |
5676 | <outline text="git RSS feeds"> | |
5677 | XML | |
717b8311 JN |
5678 | |
5679 | foreach my $pr (@list) { | |
5680 | my %proj = %$pr; | |
847e01fb | 5681 | my $head = git_get_head_hash($proj{'path'}); |
717b8311 JN |
5682 | if (!defined $head) { |
5683 | next; | |
5684 | } | |
25691fbe | 5685 | $git_dir = "$projectroot/$proj{'path'}"; |
847e01fb | 5686 | my %co = parse_commit($head); |
717b8311 JN |
5687 | if (!%co) { |
5688 | next; | |
5689 | } | |
5690 | ||
5691 | my $path = esc_html(chop_str($proj{'path'}, 25, 5)); | |
5692 | my $rss = "$my_url?p=$proj{'path'};a=rss"; | |
5693 | my $html = "$my_url?p=$proj{'path'};a=summary"; | |
5694 | print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n"; | |
5695 | } | |
59b9f61a JN |
5696 | print <<XML; |
5697 | </outline> | |
5698 | </body> | |
5699 | </opml> | |
5700 | XML | |
717b8311 | 5701 | } |