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