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