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