]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: gravatar url cache
[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 # Rather than recomputing the url for an email multiple times, we cache it
1517 # after the first hit. This gives a visible benefit in views where the avatar
1518 # for the same email is used repeatedly (e.g. shortlog).
1519 # The cache is shared by all avatar engines (currently gravatar only), which
1520 # are free to use it as preferred. Since only one avatar engine is used for any
1521 # given page, there's no risk for cache conflicts.
1522 our %avatar_cache = ();
1523
1524 # Compute the gravatar url for a given email, if it's not in the cache already.
1525 # Gravatar stores only the part of the URL before the size, since that's the
1526 # one computationally more expensive. This also allows reuse of the cache for
1527 # different sizes (for this particular engine).
1528 sub gravatar_url {
1529 my $email = lc shift;
1530 my $size = shift;
1531 $avatar_cache{$email} ||=
1532 "http://www.gravatar.com/avatar/" .
1533 Digest::MD5::md5_hex($email) . "?s=";
1534 return $avatar_cache{$email} . $size;
1535 }
1536
1537 # Insert an avatar for the given $email at the given $size if the feature
1538 # is enabled.
1539 sub git_get_avatar {
1540 my ($email, %opts) = @_;
1541 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1542 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1543 $opts{-size} ||= 'default';
1544 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1545 my $url = "";
1546 if ($git_avatar eq 'gravatar') {
1547 $url = gravatar_url($email, $size);
1548 }
1549 # Currently only gravatars are supported, but other forms such as
1550 # picons can be added by putting an else up here and defining $url
1551 # as needed. If no variant puts something in $url, we assume avatars
1552 # are completely disabled/unavailable.
1553 if ($url) {
1554 return $pre_white .
1555 "<img width=\"$size\" " .
1556 "class=\"avatar\" " .
1557 "src=\"$url\" " .
1558 "/>" . $post_white;
1559 } else {
1560 return "";
1561 }
1562 }
1563
1564 # format the author name of the given commit with the given tag
1565 # the author name is chopped and escaped according to the other
1566 # optional parameters (see chop_str).
1567 sub format_author_html {
1568 my $tag = shift;
1569 my $co = shift;
1570 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1571 return "<$tag class=\"author\">" .
1572 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1573 $author . "</$tag>";
1574 }
1575
1576 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1577 sub format_git_diff_header_line {
1578 my $line = shift;
1579 my $diffinfo = shift;
1580 my ($from, $to) = @_;
1581
1582 if ($diffinfo->{'nparents'}) {
1583 # combined diff
1584 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1585 if ($to->{'href'}) {
1586 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1587 esc_path($to->{'file'}));
1588 } else { # file was deleted (no href)
1589 $line .= esc_path($to->{'file'});
1590 }
1591 } else {
1592 # "ordinary" diff
1593 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1594 if ($from->{'href'}) {
1595 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1596 'a/' . esc_path($from->{'file'}));
1597 } else { # file was added (no href)
1598 $line .= 'a/' . esc_path($from->{'file'});
1599 }
1600 $line .= ' ';
1601 if ($to->{'href'}) {
1602 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1603 'b/' . esc_path($to->{'file'}));
1604 } else { # file was deleted
1605 $line .= 'b/' . esc_path($to->{'file'});
1606 }
1607 }
1608
1609 return "<div class=\"diff header\">$line</div>\n";
1610 }
1611
1612 # format extended diff header line, before patch itself
1613 sub format_extended_diff_header_line {
1614 my $line = shift;
1615 my $diffinfo = shift;
1616 my ($from, $to) = @_;
1617
1618 # match <path>
1619 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1620 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1621 esc_path($from->{'file'}));
1622 }
1623 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1624 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1625 esc_path($to->{'file'}));
1626 }
1627 # match single <mode>
1628 if ($line =~ m/\s(\d{6})$/) {
1629 $line .= '<span class="info"> (' .
1630 file_type_long($1) .
1631 ')</span>';
1632 }
1633 # match <hash>
1634 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1635 # can match only for combined diff
1636 $line = 'index ';
1637 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1638 if ($from->{'href'}[$i]) {
1639 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1640 -class=>"hash"},
1641 substr($diffinfo->{'from_id'}[$i],0,7));
1642 } else {
1643 $line .= '0' x 7;
1644 }
1645 # separator
1646 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1647 }
1648 $line .= '..';
1649 if ($to->{'href'}) {
1650 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1651 substr($diffinfo->{'to_id'},0,7));
1652 } else {
1653 $line .= '0' x 7;
1654 }
1655
1656 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1657 # can match only for ordinary diff
1658 my ($from_link, $to_link);
1659 if ($from->{'href'}) {
1660 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1661 substr($diffinfo->{'from_id'},0,7));
1662 } else {
1663 $from_link = '0' x 7;
1664 }
1665 if ($to->{'href'}) {
1666 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1667 substr($diffinfo->{'to_id'},0,7));
1668 } else {
1669 $to_link = '0' x 7;
1670 }
1671 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1672 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1673 }
1674
1675 return $line . "<br/>\n";
1676 }
1677
1678 # format from-file/to-file diff header
1679 sub format_diff_from_to_header {
1680 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1681 my $line;
1682 my $result = '';
1683
1684 $line = $from_line;
1685 #assert($line =~ m/^---/) if DEBUG;
1686 # no extra formatting for "^--- /dev/null"
1687 if (! $diffinfo->{'nparents'}) {
1688 # ordinary (single parent) diff
1689 if ($line =~ m!^--- "?a/!) {
1690 if ($from->{'href'}) {
1691 $line = '--- a/' .
1692 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1693 esc_path($from->{'file'}));
1694 } else {
1695 $line = '--- a/' .
1696 esc_path($from->{'file'});
1697 }
1698 }
1699 $result .= qq!<div class="diff from_file">$line</div>\n!;
1700
1701 } else {
1702 # combined diff (merge commit)
1703 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1704 if ($from->{'href'}[$i]) {
1705 $line = '--- ' .
1706 $cgi->a({-href=>href(action=>"blobdiff",
1707 hash_parent=>$diffinfo->{'from_id'}[$i],
1708 hash_parent_base=>$parents[$i],
1709 file_parent=>$from->{'file'}[$i],
1710 hash=>$diffinfo->{'to_id'},
1711 hash_base=>$hash,
1712 file_name=>$to->{'file'}),
1713 -class=>"path",
1714 -title=>"diff" . ($i+1)},
1715 $i+1) .
1716 '/' .
1717 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1718 esc_path($from->{'file'}[$i]));
1719 } else {
1720 $line = '--- /dev/null';
1721 }
1722 $result .= qq!<div class="diff from_file">$line</div>\n!;
1723 }
1724 }
1725
1726 $line = $to_line;
1727 #assert($line =~ m/^\+\+\+/) if DEBUG;
1728 # no extra formatting for "^+++ /dev/null"
1729 if ($line =~ m!^\+\+\+ "?b/!) {
1730 if ($to->{'href'}) {
1731 $line = '+++ b/' .
1732 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1733 esc_path($to->{'file'}));
1734 } else {
1735 $line = '+++ b/' .
1736 esc_path($to->{'file'});
1737 }
1738 }
1739 $result .= qq!<div class="diff to_file">$line</div>\n!;
1740
1741 return $result;
1742 }
1743
1744 # create note for patch simplified by combined diff
1745 sub format_diff_cc_simplified {
1746 my ($diffinfo, @parents) = @_;
1747 my $result = '';
1748
1749 $result .= "<div class=\"diff header\">" .
1750 "diff --cc ";
1751 if (!is_deleted($diffinfo)) {
1752 $result .= $cgi->a({-href => href(action=>"blob",
1753 hash_base=>$hash,
1754 hash=>$diffinfo->{'to_id'},
1755 file_name=>$diffinfo->{'to_file'}),
1756 -class => "path"},
1757 esc_path($diffinfo->{'to_file'}));
1758 } else {
1759 $result .= esc_path($diffinfo->{'to_file'});
1760 }
1761 $result .= "</div>\n" . # class="diff header"
1762 "<div class=\"diff nodifferences\">" .
1763 "Simple merge" .
1764 "</div>\n"; # class="diff nodifferences"
1765
1766 return $result;
1767 }
1768
1769 # format patch (diff) line (not to be used for diff headers)
1770 sub format_diff_line {
1771 my $line = shift;
1772 my ($from, $to) = @_;
1773 my $diff_class = "";
1774
1775 chomp $line;
1776
1777 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1778 # combined diff
1779 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1780 if ($line =~ m/^\@{3}/) {
1781 $diff_class = " chunk_header";
1782 } elsif ($line =~ m/^\\/) {
1783 $diff_class = " incomplete";
1784 } elsif ($prefix =~ tr/+/+/) {
1785 $diff_class = " add";
1786 } elsif ($prefix =~ tr/-/-/) {
1787 $diff_class = " rem";
1788 }
1789 } else {
1790 # assume ordinary diff
1791 my $char = substr($line, 0, 1);
1792 if ($char eq '+') {
1793 $diff_class = " add";
1794 } elsif ($char eq '-') {
1795 $diff_class = " rem";
1796 } elsif ($char eq '@') {
1797 $diff_class = " chunk_header";
1798 } elsif ($char eq "\\") {
1799 $diff_class = " incomplete";
1800 }
1801 }
1802 $line = untabify($line);
1803 if ($from && $to && $line =~ m/^\@{2} /) {
1804 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1805 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1806
1807 $from_lines = 0 unless defined $from_lines;
1808 $to_lines = 0 unless defined $to_lines;
1809
1810 if ($from->{'href'}) {
1811 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1812 -class=>"list"}, $from_text);
1813 }
1814 if ($to->{'href'}) {
1815 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1816 -class=>"list"}, $to_text);
1817 }
1818 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1819 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1820 return "<div class=\"diff$diff_class\">$line</div>\n";
1821 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1822 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1823 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1824
1825 @from_text = split(' ', $ranges);
1826 for (my $i = 0; $i < @from_text; ++$i) {
1827 ($from_start[$i], $from_nlines[$i]) =
1828 (split(',', substr($from_text[$i], 1)), 0);
1829 }
1830
1831 $to_text = pop @from_text;
1832 $to_start = pop @from_start;
1833 $to_nlines = pop @from_nlines;
1834
1835 $line = "<span class=\"chunk_info\">$prefix ";
1836 for (my $i = 0; $i < @from_text; ++$i) {
1837 if ($from->{'href'}[$i]) {
1838 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1839 -class=>"list"}, $from_text[$i]);
1840 } else {
1841 $line .= $from_text[$i];
1842 }
1843 $line .= " ";
1844 }
1845 if ($to->{'href'}) {
1846 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1847 -class=>"list"}, $to_text);
1848 } else {
1849 $line .= $to_text;
1850 }
1851 $line .= " $prefix</span>" .
1852 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1853 return "<div class=\"diff$diff_class\">$line</div>\n";
1854 }
1855 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1856 }
1857
1858 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1859 # linked. Pass the hash of the tree/commit to snapshot.
1860 sub format_snapshot_links {
1861 my ($hash) = @_;
1862 my $num_fmts = @snapshot_fmts;
1863 if ($num_fmts > 1) {
1864 # A parenthesized list of links bearing format names.
1865 # e.g. "snapshot (_tar.gz_ _zip_)"
1866 return "snapshot (" . join(' ', map
1867 $cgi->a({
1868 -href => href(
1869 action=>"snapshot",
1870 hash=>$hash,
1871 snapshot_format=>$_
1872 )
1873 }, $known_snapshot_formats{$_}{'display'})
1874 , @snapshot_fmts) . ")";
1875 } elsif ($num_fmts == 1) {
1876 # A single "snapshot" link whose tooltip bears the format name.
1877 # i.e. "_snapshot_"
1878 my ($fmt) = @snapshot_fmts;
1879 return
1880 $cgi->a({
1881 -href => href(
1882 action=>"snapshot",
1883 hash=>$hash,
1884 snapshot_format=>$fmt
1885 ),
1886 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1887 }, "snapshot");
1888 } else { # $num_fmts == 0
1889 return undef;
1890 }
1891 }
1892
1893 ## ......................................................................
1894 ## functions returning values to be passed, perhaps after some
1895 ## transformation, to other functions; e.g. returning arguments to href()
1896
1897 # returns hash to be passed to href to generate gitweb URL
1898 # in -title key it returns description of link
1899 sub get_feed_info {
1900 my $format = shift || 'Atom';
1901 my %res = (action => lc($format));
1902
1903 # feed links are possible only for project views
1904 return unless (defined $project);
1905 # some views should link to OPML, or to generic project feed,
1906 # or don't have specific feed yet (so they should use generic)
1907 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1908
1909 my $branch;
1910 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1911 # from tag links; this also makes possible to detect branch links
1912 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1913 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1914 $branch = $1;
1915 }
1916 # find log type for feed description (title)
1917 my $type = 'log';
1918 if (defined $file_name) {
1919 $type = "history of $file_name";
1920 $type .= "/" if ($action eq 'tree');
1921 $type .= " on '$branch'" if (defined $branch);
1922 } else {
1923 $type = "log of $branch" if (defined $branch);
1924 }
1925
1926 $res{-title} = $type;
1927 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1928 $res{'file_name'} = $file_name;
1929
1930 return %res;
1931 }
1932
1933 ## ----------------------------------------------------------------------
1934 ## git utility subroutines, invoking git commands
1935
1936 # returns path to the core git executable and the --git-dir parameter as list
1937 sub git_cmd {
1938 return $GIT, '--git-dir='.$git_dir;
1939 }
1940
1941 # quote the given arguments for passing them to the shell
1942 # quote_command("command", "arg 1", "arg with ' and ! characters")
1943 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1944 # Try to avoid using this function wherever possible.
1945 sub quote_command {
1946 return join(' ',
1947 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
1948 }
1949
1950 # get HEAD ref of given project as hash
1951 sub git_get_head_hash {
1952 my $project = shift;
1953 my $o_git_dir = $git_dir;
1954 my $retval = undef;
1955 $git_dir = "$projectroot/$project";
1956 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1957 my $head = <$fd>;
1958 close $fd;
1959 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1960 $retval = $1;
1961 }
1962 }
1963 if (defined $o_git_dir) {
1964 $git_dir = $o_git_dir;
1965 }
1966 return $retval;
1967 }
1968
1969 # get type of given object
1970 sub git_get_type {
1971 my $hash = shift;
1972
1973 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1974 my $type = <$fd>;
1975 close $fd or return;
1976 chomp $type;
1977 return $type;
1978 }
1979
1980 # repository configuration
1981 our $config_file = '';
1982 our %config;
1983
1984 # store multiple values for single key as anonymous array reference
1985 # single values stored directly in the hash, not as [ <value> ]
1986 sub hash_set_multi {
1987 my ($hash, $key, $value) = @_;
1988
1989 if (!exists $hash->{$key}) {
1990 $hash->{$key} = $value;
1991 } elsif (!ref $hash->{$key}) {
1992 $hash->{$key} = [ $hash->{$key}, $value ];
1993 } else {
1994 push @{$hash->{$key}}, $value;
1995 }
1996 }
1997
1998 # return hash of git project configuration
1999 # optionally limited to some section, e.g. 'gitweb'
2000 sub git_parse_project_config {
2001 my $section_regexp = shift;
2002 my %config;
2003
2004 local $/ = "\0";
2005
2006 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2007 or return;
2008
2009 while (my $keyval = <$fh>) {
2010 chomp $keyval;
2011 my ($key, $value) = split(/\n/, $keyval, 2);
2012
2013 hash_set_multi(\%config, $key, $value)
2014 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2015 }
2016 close $fh;
2017
2018 return %config;
2019 }
2020
2021 # convert config value to boolean: 'true' or 'false'
2022 # no value, number > 0, 'true' and 'yes' values are true
2023 # rest of values are treated as false (never as error)
2024 sub config_to_bool {
2025 my $val = shift;
2026
2027 return 1 if !defined $val; # section.key
2028
2029 # strip leading and trailing whitespace
2030 $val =~ s/^\s+//;
2031 $val =~ s/\s+$//;
2032
2033 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2034 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2035 }
2036
2037 # convert config value to simple decimal number
2038 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2039 # to be multiplied by 1024, 1048576, or 1073741824
2040 sub config_to_int {
2041 my $val = shift;
2042
2043 # strip leading and trailing whitespace
2044 $val =~ s/^\s+//;
2045 $val =~ s/\s+$//;
2046
2047 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2048 $unit = lc($unit);
2049 # unknown unit is treated as 1
2050 return $num * ($unit eq 'g' ? 1073741824 :
2051 $unit eq 'm' ? 1048576 :
2052 $unit eq 'k' ? 1024 : 1);
2053 }
2054 return $val;
2055 }
2056
2057 # convert config value to array reference, if needed
2058 sub config_to_multi {
2059 my $val = shift;
2060
2061 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2062 }
2063
2064 sub git_get_project_config {
2065 my ($key, $type) = @_;
2066
2067 # key sanity check
2068 return unless ($key);
2069 $key =~ s/^gitweb\.//;
2070 return if ($key =~ m/\W/);
2071
2072 # type sanity check
2073 if (defined $type) {
2074 $type =~ s/^--//;
2075 $type = undef
2076 unless ($type eq 'bool' || $type eq 'int');
2077 }
2078
2079 # get config
2080 if (!defined $config_file ||
2081 $config_file ne "$git_dir/config") {
2082 %config = git_parse_project_config('gitweb');
2083 $config_file = "$git_dir/config";
2084 }
2085
2086 # check if config variable (key) exists
2087 return unless exists $config{"gitweb.$key"};
2088
2089 # ensure given type
2090 if (!defined $type) {
2091 return $config{"gitweb.$key"};
2092 } elsif ($type eq 'bool') {
2093 # backward compatibility: 'git config --bool' returns true/false
2094 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2095 } elsif ($type eq 'int') {
2096 return config_to_int($config{"gitweb.$key"});
2097 }
2098 return $config{"gitweb.$key"};
2099 }
2100
2101 # get hash of given path at given ref
2102 sub git_get_hash_by_path {
2103 my $base = shift;
2104 my $path = shift || return undef;
2105 my $type = shift;
2106
2107 $path =~ s,/+$,,;
2108
2109 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2110 or die_error(500, "Open git-ls-tree failed");
2111 my $line = <$fd>;
2112 close $fd or return undef;
2113
2114 if (!defined $line) {
2115 # there is no tree or hash given by $path at $base
2116 return undef;
2117 }
2118
2119 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2120 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2121 if (defined $type && $type ne $2) {
2122 # type doesn't match
2123 return undef;
2124 }
2125 return $3;
2126 }
2127
2128 # get path of entry with given hash at given tree-ish (ref)
2129 # used to get 'from' filename for combined diff (merge commit) for renames
2130 sub git_get_path_by_hash {
2131 my $base = shift || return;
2132 my $hash = shift || return;
2133
2134 local $/ = "\0";
2135
2136 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2137 or return undef;
2138 while (my $line = <$fd>) {
2139 chomp $line;
2140
2141 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2142 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2143 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2144 close $fd;
2145 return $1;
2146 }
2147 }
2148 close $fd;
2149 return undef;
2150 }
2151
2152 ## ......................................................................
2153 ## git utility functions, directly accessing git repository
2154
2155 sub git_get_project_description {
2156 my $path = shift;
2157
2158 $git_dir = "$projectroot/$path";
2159 open my $fd, '<', "$git_dir/description"
2160 or return git_get_project_config('description');
2161 my $descr = <$fd>;
2162 close $fd;
2163 if (defined $descr) {
2164 chomp $descr;
2165 }
2166 return $descr;
2167 }
2168
2169 sub git_get_project_ctags {
2170 my $path = shift;
2171 my $ctags = {};
2172
2173 $git_dir = "$projectroot/$path";
2174 opendir my $dh, "$git_dir/ctags"
2175 or return $ctags;
2176 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2177 open my $ct, '<', $_ or next;
2178 my $val = <$ct>;
2179 chomp $val;
2180 close $ct;
2181 my $ctag = $_; $ctag =~ s#.*/##;
2182 $ctags->{$ctag} = $val;
2183 }
2184 closedir $dh;
2185 $ctags;
2186 }
2187
2188 sub git_populate_project_tagcloud {
2189 my $ctags = shift;
2190
2191 # First, merge different-cased tags; tags vote on casing
2192 my %ctags_lc;
2193 foreach (keys %$ctags) {
2194 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2195 if (not $ctags_lc{lc $_}->{topcount}
2196 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2197 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2198 $ctags_lc{lc $_}->{topname} = $_;
2199 }
2200 }
2201
2202 my $cloud;
2203 if (eval { require HTML::TagCloud; 1; }) {
2204 $cloud = HTML::TagCloud->new;
2205 foreach (sort keys %ctags_lc) {
2206 # Pad the title with spaces so that the cloud looks
2207 # less crammed.
2208 my $title = $ctags_lc{$_}->{topname};
2209 $title =~ s/ /&nbsp;/g;
2210 $title =~ s/^/&nbsp;/g;
2211 $title =~ s/$/&nbsp;/g;
2212 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2213 }
2214 } else {
2215 $cloud = \%ctags_lc;
2216 }
2217 $cloud;
2218 }
2219
2220 sub git_show_project_tagcloud {
2221 my ($cloud, $count) = @_;
2222 print STDERR ref($cloud)."..\n";
2223 if (ref $cloud eq 'HTML::TagCloud') {
2224 return $cloud->html_and_css($count);
2225 } else {
2226 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2227 return '<p align="center">' . join (', ', map {
2228 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2229 } splice(@tags, 0, $count)) . '</p>';
2230 }
2231 }
2232
2233 sub git_get_project_url_list {
2234 my $path = shift;
2235
2236 $git_dir = "$projectroot/$path";
2237 open my $fd, '<', "$git_dir/cloneurl"
2238 or return wantarray ?
2239 @{ config_to_multi(git_get_project_config('url')) } :
2240 config_to_multi(git_get_project_config('url'));
2241 my @git_project_url_list = map { chomp; $_ } <$fd>;
2242 close $fd;
2243
2244 return wantarray ? @git_project_url_list : \@git_project_url_list;
2245 }
2246
2247 sub git_get_projects_list {
2248 my ($filter) = @_;
2249 my @list;
2250
2251 $filter ||= '';
2252 $filter =~ s/\.git$//;
2253
2254 my $check_forks = gitweb_check_feature('forks');
2255
2256 if (-d $projects_list) {
2257 # search in directory
2258 my $dir = $projects_list . ($filter ? "/$filter" : '');
2259 # remove the trailing "/"
2260 $dir =~ s!/+$!!;
2261 my $pfxlen = length("$dir");
2262 my $pfxdepth = ($dir =~ tr!/!!);
2263
2264 File::Find::find({
2265 follow_fast => 1, # follow symbolic links
2266 follow_skip => 2, # ignore duplicates
2267 dangling_symlinks => 0, # ignore dangling symlinks, silently
2268 wanted => sub {
2269 # skip project-list toplevel, if we get it.
2270 return if (m!^[/.]$!);
2271 # only directories can be git repositories
2272 return unless (-d $_);
2273 # don't traverse too deep (Find is super slow on os x)
2274 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2275 $File::Find::prune = 1;
2276 return;
2277 }
2278
2279 my $subdir = substr($File::Find::name, $pfxlen + 1);
2280 # we check related file in $projectroot
2281 my $path = ($filter ? "$filter/" : '') . $subdir;
2282 if (check_export_ok("$projectroot/$path")) {
2283 push @list, { path => $path };
2284 $File::Find::prune = 1;
2285 }
2286 },
2287 }, "$dir");
2288
2289 } elsif (-f $projects_list) {
2290 # read from file(url-encoded):
2291 # 'git%2Fgit.git Linus+Torvalds'
2292 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2293 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2294 my %paths;
2295 open my $fd, '<', $projects_list or return;
2296 PROJECT:
2297 while (my $line = <$fd>) {
2298 chomp $line;
2299 my ($path, $owner) = split ' ', $line;
2300 $path = unescape($path);
2301 $owner = unescape($owner);
2302 if (!defined $path) {
2303 next;
2304 }
2305 if ($filter ne '') {
2306 # looking for forks;
2307 my $pfx = substr($path, 0, length($filter));
2308 if ($pfx ne $filter) {
2309 next PROJECT;
2310 }
2311 my $sfx = substr($path, length($filter));
2312 if ($sfx !~ /^\/.*\.git$/) {
2313 next PROJECT;
2314 }
2315 } elsif ($check_forks) {
2316 PATH:
2317 foreach my $filter (keys %paths) {
2318 # looking for forks;
2319 my $pfx = substr($path, 0, length($filter));
2320 if ($pfx ne $filter) {
2321 next PATH;
2322 }
2323 my $sfx = substr($path, length($filter));
2324 if ($sfx !~ /^\/.*\.git$/) {
2325 next PATH;
2326 }
2327 # is a fork, don't include it in
2328 # the list
2329 next PROJECT;
2330 }
2331 }
2332 if (check_export_ok("$projectroot/$path")) {
2333 my $pr = {
2334 path => $path,
2335 owner => to_utf8($owner),
2336 };
2337 push @list, $pr;
2338 (my $forks_path = $path) =~ s/\.git$//;
2339 $paths{$forks_path}++;
2340 }
2341 }
2342 close $fd;
2343 }
2344 return @list;
2345 }
2346
2347 our $gitweb_project_owner = undef;
2348 sub git_get_project_list_from_file {
2349
2350 return if (defined $gitweb_project_owner);
2351
2352 $gitweb_project_owner = {};
2353 # read from file (url-encoded):
2354 # 'git%2Fgit.git Linus+Torvalds'
2355 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2356 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2357 if (-f $projects_list) {
2358 open(my $fd, '<', $projects_list);
2359 while (my $line = <$fd>) {
2360 chomp $line;
2361 my ($pr, $ow) = split ' ', $line;
2362 $pr = unescape($pr);
2363 $ow = unescape($ow);
2364 $gitweb_project_owner->{$pr} = to_utf8($ow);
2365 }
2366 close $fd;
2367 }
2368 }
2369
2370 sub git_get_project_owner {
2371 my $project = shift;
2372 my $owner;
2373
2374 return undef unless $project;
2375 $git_dir = "$projectroot/$project";
2376
2377 if (!defined $gitweb_project_owner) {
2378 git_get_project_list_from_file();
2379 }
2380
2381 if (exists $gitweb_project_owner->{$project}) {
2382 $owner = $gitweb_project_owner->{$project};
2383 }
2384 if (!defined $owner){
2385 $owner = git_get_project_config('owner');
2386 }
2387 if (!defined $owner) {
2388 $owner = get_file_owner("$git_dir");
2389 }
2390
2391 return $owner;
2392 }
2393
2394 sub git_get_last_activity {
2395 my ($path) = @_;
2396 my $fd;
2397
2398 $git_dir = "$projectroot/$path";
2399 open($fd, "-|", git_cmd(), 'for-each-ref',
2400 '--format=%(committer)',
2401 '--sort=-committerdate',
2402 '--count=1',
2403 'refs/heads') or return;
2404 my $most_recent = <$fd>;
2405 close $fd or return;
2406 if (defined $most_recent &&
2407 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2408 my $timestamp = $1;
2409 my $age = time - $timestamp;
2410 return ($age, age_string($age));
2411 }
2412 return (undef, undef);
2413 }
2414
2415 sub git_get_references {
2416 my $type = shift || "";
2417 my %refs;
2418 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2419 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2420 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2421 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2422 or return;
2423
2424 while (my $line = <$fd>) {
2425 chomp $line;
2426 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2427 if (defined $refs{$1}) {
2428 push @{$refs{$1}}, $2;
2429 } else {
2430 $refs{$1} = [ $2 ];
2431 }
2432 }
2433 }
2434 close $fd or return;
2435 return \%refs;
2436 }
2437
2438 sub git_get_rev_name_tags {
2439 my $hash = shift || return undef;
2440
2441 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2442 or return;
2443 my $name_rev = <$fd>;
2444 close $fd;
2445
2446 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2447 return $1;
2448 } else {
2449 # catches also '$hash undefined' output
2450 return undef;
2451 }
2452 }
2453
2454 ## ----------------------------------------------------------------------
2455 ## parse to hash functions
2456
2457 sub parse_date {
2458 my $epoch = shift;
2459 my $tz = shift || "-0000";
2460
2461 my %date;
2462 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2463 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2464 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2465 $date{'hour'} = $hour;
2466 $date{'minute'} = $min;
2467 $date{'mday'} = $mday;
2468 $date{'day'} = $days[$wday];
2469 $date{'month'} = $months[$mon];
2470 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2471 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2472 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2473 $mday, $months[$mon], $hour ,$min;
2474 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2475 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2476
2477 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2478 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2479 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2480 $date{'hour_local'} = $hour;
2481 $date{'minute_local'} = $min;
2482 $date{'tz_local'} = $tz;
2483 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2484 1900+$year, $mon+1, $mday,
2485 $hour, $min, $sec, $tz);
2486 return %date;
2487 }
2488
2489 sub parse_tag {
2490 my $tag_id = shift;
2491 my %tag;
2492 my @comment;
2493
2494 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2495 $tag{'id'} = $tag_id;
2496 while (my $line = <$fd>) {
2497 chomp $line;
2498 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2499 $tag{'object'} = $1;
2500 } elsif ($line =~ m/^type (.+)$/) {
2501 $tag{'type'} = $1;
2502 } elsif ($line =~ m/^tag (.+)$/) {
2503 $tag{'name'} = $1;
2504 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2505 $tag{'author'} = $1;
2506 $tag{'author_epoch'} = $2;
2507 $tag{'author_tz'} = $3;
2508 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2509 $tag{'author_name'} = $1;
2510 $tag{'author_email'} = $2;
2511 } else {
2512 $tag{'author_name'} = $tag{'author'};
2513 }
2514 } elsif ($line =~ m/--BEGIN/) {
2515 push @comment, $line;
2516 last;
2517 } elsif ($line eq "") {
2518 last;
2519 }
2520 }
2521 push @comment, <$fd>;
2522 $tag{'comment'} = \@comment;
2523 close $fd or return;
2524 if (!defined $tag{'name'}) {
2525 return
2526 };
2527 return %tag
2528 }
2529
2530 sub parse_commit_text {
2531 my ($commit_text, $withparents) = @_;
2532 my @commit_lines = split '\n', $commit_text;
2533 my %co;
2534
2535 pop @commit_lines; # Remove '\0'
2536
2537 if (! @commit_lines) {
2538 return;
2539 }
2540
2541 my $header = shift @commit_lines;
2542 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2543 return;
2544 }
2545 ($co{'id'}, my @parents) = split ' ', $header;
2546 while (my $line = shift @commit_lines) {
2547 last if $line eq "\n";
2548 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2549 $co{'tree'} = $1;
2550 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2551 push @parents, $1;
2552 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2553 $co{'author'} = $1;
2554 $co{'author_epoch'} = $2;
2555 $co{'author_tz'} = $3;
2556 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2557 $co{'author_name'} = $1;
2558 $co{'author_email'} = $2;
2559 } else {
2560 $co{'author_name'} = $co{'author'};
2561 }
2562 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2563 $co{'committer'} = $1;
2564 $co{'committer_epoch'} = $2;
2565 $co{'committer_tz'} = $3;
2566 $co{'committer_name'} = $co{'committer'};
2567 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2568 $co{'committer_name'} = $1;
2569 $co{'committer_email'} = $2;
2570 } else {
2571 $co{'committer_name'} = $co{'committer'};
2572 }
2573 }
2574 }
2575 if (!defined $co{'tree'}) {
2576 return;
2577 };
2578 $co{'parents'} = \@parents;
2579 $co{'parent'} = $parents[0];
2580
2581 foreach my $title (@commit_lines) {
2582 $title =~ s/^ //;
2583 if ($title ne "") {
2584 $co{'title'} = chop_str($title, 80, 5);
2585 # remove leading stuff of merges to make the interesting part visible
2586 if (length($title) > 50) {
2587 $title =~ s/^Automatic //;
2588 $title =~ s/^merge (of|with) /Merge ... /i;
2589 if (length($title) > 50) {
2590 $title =~ s/(http|rsync):\/\///;
2591 }
2592 if (length($title) > 50) {
2593 $title =~ s/(master|www|rsync)\.//;
2594 }
2595 if (length($title) > 50) {
2596 $title =~ s/kernel.org:?//;
2597 }
2598 if (length($title) > 50) {
2599 $title =~ s/\/pub\/scm//;
2600 }
2601 }
2602 $co{'title_short'} = chop_str($title, 50, 5);
2603 last;
2604 }
2605 }
2606 if (! defined $co{'title'} || $co{'title'} eq "") {
2607 $co{'title'} = $co{'title_short'} = '(no commit message)';
2608 }
2609 # remove added spaces
2610 foreach my $line (@commit_lines) {
2611 $line =~ s/^ //;
2612 }
2613 $co{'comment'} = \@commit_lines;
2614
2615 my $age = time - $co{'committer_epoch'};
2616 $co{'age'} = $age;
2617 $co{'age_string'} = age_string($age);
2618 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2619 if ($age > 60*60*24*7*2) {
2620 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2621 $co{'age_string_age'} = $co{'age_string'};
2622 } else {
2623 $co{'age_string_date'} = $co{'age_string'};
2624 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2625 }
2626 return %co;
2627 }
2628
2629 sub parse_commit {
2630 my ($commit_id) = @_;
2631 my %co;
2632
2633 local $/ = "\0";
2634
2635 open my $fd, "-|", git_cmd(), "rev-list",
2636 "--parents",
2637 "--header",
2638 "--max-count=1",
2639 $commit_id,
2640 "--",
2641 or die_error(500, "Open git-rev-list failed");
2642 %co = parse_commit_text(<$fd>, 1);
2643 close $fd;
2644
2645 return %co;
2646 }
2647
2648 sub parse_commits {
2649 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2650 my @cos;
2651
2652 $maxcount ||= 1;
2653 $skip ||= 0;
2654
2655 local $/ = "\0";
2656
2657 open my $fd, "-|", git_cmd(), "rev-list",
2658 "--header",
2659 @args,
2660 ("--max-count=" . $maxcount),
2661 ("--skip=" . $skip),
2662 @extra_options,
2663 $commit_id,
2664 "--",
2665 ($filename ? ($filename) : ())
2666 or die_error(500, "Open git-rev-list failed");
2667 while (my $line = <$fd>) {
2668 my %co = parse_commit_text($line);
2669 push @cos, \%co;
2670 }
2671 close $fd;
2672
2673 return wantarray ? @cos : \@cos;
2674 }
2675
2676 # parse line of git-diff-tree "raw" output
2677 sub parse_difftree_raw_line {
2678 my $line = shift;
2679 my %res;
2680
2681 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2682 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2683 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2684 $res{'from_mode'} = $1;
2685 $res{'to_mode'} = $2;
2686 $res{'from_id'} = $3;
2687 $res{'to_id'} = $4;
2688 $res{'status'} = $5;
2689 $res{'similarity'} = $6;
2690 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2691 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2692 } else {
2693 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2694 }
2695 }
2696 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2697 # combined diff (for merge commit)
2698 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2699 $res{'nparents'} = length($1);
2700 $res{'from_mode'} = [ split(' ', $2) ];
2701 $res{'to_mode'} = pop @{$res{'from_mode'}};
2702 $res{'from_id'} = [ split(' ', $3) ];
2703 $res{'to_id'} = pop @{$res{'from_id'}};
2704 $res{'status'} = [ split('', $4) ];
2705 $res{'to_file'} = unquote($5);
2706 }
2707 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2708 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2709 $res{'commit'} = $1;
2710 }
2711
2712 return wantarray ? %res : \%res;
2713 }
2714
2715 # wrapper: return parsed line of git-diff-tree "raw" output
2716 # (the argument might be raw line, or parsed info)
2717 sub parsed_difftree_line {
2718 my $line_or_ref = shift;
2719
2720 if (ref($line_or_ref) eq "HASH") {
2721 # pre-parsed (or generated by hand)
2722 return $line_or_ref;
2723 } else {
2724 return parse_difftree_raw_line($line_or_ref);
2725 }
2726 }
2727
2728 # parse line of git-ls-tree output
2729 sub parse_ls_tree_line {
2730 my $line = shift;
2731 my %opts = @_;
2732 my %res;
2733
2734 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2735 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2736
2737 $res{'mode'} = $1;
2738 $res{'type'} = $2;
2739 $res{'hash'} = $3;
2740 if ($opts{'-z'}) {
2741 $res{'name'} = $4;
2742 } else {
2743 $res{'name'} = unquote($4);
2744 }
2745
2746 return wantarray ? %res : \%res;
2747 }
2748
2749 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2750 sub parse_from_to_diffinfo {
2751 my ($diffinfo, $from, $to, @parents) = @_;
2752
2753 if ($diffinfo->{'nparents'}) {
2754 # combined diff
2755 $from->{'file'} = [];
2756 $from->{'href'} = [];
2757 fill_from_file_info($diffinfo, @parents)
2758 unless exists $diffinfo->{'from_file'};
2759 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2760 $from->{'file'}[$i] =
2761 defined $diffinfo->{'from_file'}[$i] ?
2762 $diffinfo->{'from_file'}[$i] :
2763 $diffinfo->{'to_file'};
2764 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2765 $from->{'href'}[$i] = href(action=>"blob",
2766 hash_base=>$parents[$i],
2767 hash=>$diffinfo->{'from_id'}[$i],
2768 file_name=>$from->{'file'}[$i]);
2769 } else {
2770 $from->{'href'}[$i] = undef;
2771 }
2772 }
2773 } else {
2774 # ordinary (not combined) diff
2775 $from->{'file'} = $diffinfo->{'from_file'};
2776 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2777 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2778 hash=>$diffinfo->{'from_id'},
2779 file_name=>$from->{'file'});
2780 } else {
2781 delete $from->{'href'};
2782 }
2783 }
2784
2785 $to->{'file'} = $diffinfo->{'to_file'};
2786 if (!is_deleted($diffinfo)) { # file exists in result
2787 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2788 hash=>$diffinfo->{'to_id'},
2789 file_name=>$to->{'file'});
2790 } else {
2791 delete $to->{'href'};
2792 }
2793 }
2794
2795 ## ......................................................................
2796 ## parse to array of hashes functions
2797
2798 sub git_get_heads_list {
2799 my $limit = shift;
2800 my @headslist;
2801
2802 open my $fd, '-|', git_cmd(), 'for-each-ref',
2803 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2804 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2805 'refs/heads'
2806 or return;
2807 while (my $line = <$fd>) {
2808 my %ref_item;
2809
2810 chomp $line;
2811 my ($refinfo, $committerinfo) = split(/\0/, $line);
2812 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2813 my ($committer, $epoch, $tz) =
2814 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2815 $ref_item{'fullname'} = $name;
2816 $name =~ s!^refs/heads/!!;
2817
2818 $ref_item{'name'} = $name;
2819 $ref_item{'id'} = $hash;
2820 $ref_item{'title'} = $title || '(no commit message)';
2821 $ref_item{'epoch'} = $epoch;
2822 if ($epoch) {
2823 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2824 } else {
2825 $ref_item{'age'} = "unknown";
2826 }
2827
2828 push @headslist, \%ref_item;
2829 }
2830 close $fd;
2831
2832 return wantarray ? @headslist : \@headslist;
2833 }
2834
2835 sub git_get_tags_list {
2836 my $limit = shift;
2837 my @tagslist;
2838
2839 open my $fd, '-|', git_cmd(), 'for-each-ref',
2840 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2841 '--format=%(objectname) %(objecttype) %(refname) '.
2842 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2843 'refs/tags'
2844 or return;
2845 while (my $line = <$fd>) {
2846 my %ref_item;
2847
2848 chomp $line;
2849 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2850 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2851 my ($creator, $epoch, $tz) =
2852 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2853 $ref_item{'fullname'} = $name;
2854 $name =~ s!^refs/tags/!!;
2855
2856 $ref_item{'type'} = $type;
2857 $ref_item{'id'} = $id;
2858 $ref_item{'name'} = $name;
2859 if ($type eq "tag") {
2860 $ref_item{'subject'} = $title;
2861 $ref_item{'reftype'} = $reftype;
2862 $ref_item{'refid'} = $refid;
2863 } else {
2864 $ref_item{'reftype'} = $type;
2865 $ref_item{'refid'} = $id;
2866 }
2867
2868 if ($type eq "tag" || $type eq "commit") {
2869 $ref_item{'epoch'} = $epoch;
2870 if ($epoch) {
2871 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2872 } else {
2873 $ref_item{'age'} = "unknown";
2874 }
2875 }
2876
2877 push @tagslist, \%ref_item;
2878 }
2879 close $fd;
2880
2881 return wantarray ? @tagslist : \@tagslist;
2882 }
2883
2884 ## ----------------------------------------------------------------------
2885 ## filesystem-related functions
2886
2887 sub get_file_owner {
2888 my $path = shift;
2889
2890 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2891 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2892 if (!defined $gcos) {
2893 return undef;
2894 }
2895 my $owner = $gcos;
2896 $owner =~ s/[,;].*$//;
2897 return to_utf8($owner);
2898 }
2899
2900 # assume that file exists
2901 sub insert_file {
2902 my $filename = shift;
2903
2904 open my $fd, '<', $filename;
2905 print map { to_utf8($_) } <$fd>;
2906 close $fd;
2907 }
2908
2909 ## ......................................................................
2910 ## mimetype related functions
2911
2912 sub mimetype_guess_file {
2913 my $filename = shift;
2914 my $mimemap = shift;
2915 -r $mimemap or return undef;
2916
2917 my %mimemap;
2918 open(my $mh, '<', $mimemap) or return undef;
2919 while (<$mh>) {
2920 next if m/^#/; # skip comments
2921 my ($mimetype, $exts) = split(/\t+/);
2922 if (defined $exts) {
2923 my @exts = split(/\s+/, $exts);
2924 foreach my $ext (@exts) {
2925 $mimemap{$ext} = $mimetype;
2926 }
2927 }
2928 }
2929 close($mh);
2930
2931 $filename =~ /\.([^.]*)$/;
2932 return $mimemap{$1};
2933 }
2934
2935 sub mimetype_guess {
2936 my $filename = shift;
2937 my $mime;
2938 $filename =~ /\./ or return undef;
2939
2940 if ($mimetypes_file) {
2941 my $file = $mimetypes_file;
2942 if ($file !~ m!^/!) { # if it is relative path
2943 # it is relative to project
2944 $file = "$projectroot/$project/$file";
2945 }
2946 $mime = mimetype_guess_file($filename, $file);
2947 }
2948 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2949 return $mime;
2950 }
2951
2952 sub blob_mimetype {
2953 my $fd = shift;
2954 my $filename = shift;
2955
2956 if ($filename) {
2957 my $mime = mimetype_guess($filename);
2958 $mime and return $mime;
2959 }
2960
2961 # just in case
2962 return $default_blob_plain_mimetype unless $fd;
2963
2964 if (-T $fd) {
2965 return 'text/plain';
2966 } elsif (! $filename) {
2967 return 'application/octet-stream';
2968 } elsif ($filename =~ m/\.png$/i) {
2969 return 'image/png';
2970 } elsif ($filename =~ m/\.gif$/i) {
2971 return 'image/gif';
2972 } elsif ($filename =~ m/\.jpe?g$/i) {
2973 return 'image/jpeg';
2974 } else {
2975 return 'application/octet-stream';
2976 }
2977 }
2978
2979 sub blob_contenttype {
2980 my ($fd, $file_name, $type) = @_;
2981
2982 $type ||= blob_mimetype($fd, $file_name);
2983 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2984 $type .= "; charset=$default_text_plain_charset";
2985 }
2986
2987 return $type;
2988 }
2989
2990 ## ======================================================================
2991 ## functions printing HTML: header, footer, error page
2992
2993 sub git_header_html {
2994 my $status = shift || "200 OK";
2995 my $expires = shift;
2996
2997 my $title = "$site_name";
2998 if (defined $project) {
2999 $title .= " - " . to_utf8($project);
3000 if (defined $action) {
3001 $title .= "/$action";
3002 if (defined $file_name) {
3003 $title .= " - " . esc_path($file_name);
3004 if ($action eq "tree" && $file_name !~ m|/$|) {
3005 $title .= "/";
3006 }
3007 }
3008 }
3009 }
3010 my $content_type;
3011 # require explicit support from the UA if we are to send the page as
3012 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3013 # we have to do this because MSIE sometimes globs '*/*', pretending to
3014 # support xhtml+xml but choking when it gets what it asked for.
3015 if (defined $cgi->http('HTTP_ACCEPT') &&
3016 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3017 $cgi->Accept('application/xhtml+xml') != 0) {
3018 $content_type = 'application/xhtml+xml';
3019 } else {
3020 $content_type = 'text/html';
3021 }
3022 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3023 -status=> $status, -expires => $expires);
3024 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3025 print <<EOF;
3026 <?xml version="1.0" encoding="utf-8"?>
3027 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3028 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3029 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3030 <!-- git core binaries version $git_version -->
3031 <head>
3032 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3033 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3034 <meta name="robots" content="index, nofollow"/>
3035 <title>$title</title>
3036 EOF
3037 # the stylesheet, favicon etc urls won't work correctly with path_info
3038 # unless we set the appropriate base URL
3039 if ($ENV{'PATH_INFO'}) {
3040 print "<base href=\"".esc_url($base_url)."\" />\n";
3041 }
3042 # print out each stylesheet that exist, providing backwards capability
3043 # for those people who defined $stylesheet in a config file
3044 if (defined $stylesheet) {
3045 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3046 } else {
3047 foreach my $stylesheet (@stylesheets) {
3048 next unless $stylesheet;
3049 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3050 }
3051 }
3052 if (defined $project) {
3053 my %href_params = get_feed_info();
3054 if (!exists $href_params{'-title'}) {
3055 $href_params{'-title'} = 'log';
3056 }
3057
3058 foreach my $format qw(RSS Atom) {
3059 my $type = lc($format);
3060 my %link_attr = (
3061 '-rel' => 'alternate',
3062 '-title' => "$project - $href_params{'-title'} - $format feed",
3063 '-type' => "application/$type+xml"
3064 );
3065
3066 $href_params{'action'} = $type;
3067 $link_attr{'-href'} = href(%href_params);
3068 print "<link ".
3069 "rel=\"$link_attr{'-rel'}\" ".
3070 "title=\"$link_attr{'-title'}\" ".
3071 "href=\"$link_attr{'-href'}\" ".
3072 "type=\"$link_attr{'-type'}\" ".
3073 "/>\n";
3074
3075 $href_params{'extra_options'} = '--no-merges';
3076 $link_attr{'-href'} = href(%href_params);
3077 $link_attr{'-title'} .= ' (no merges)';
3078 print "<link ".
3079 "rel=\"$link_attr{'-rel'}\" ".
3080 "title=\"$link_attr{'-title'}\" ".
3081 "href=\"$link_attr{'-href'}\" ".
3082 "type=\"$link_attr{'-type'}\" ".
3083 "/>\n";
3084 }
3085
3086 } else {
3087 printf('<link rel="alternate" title="%s projects list" '.
3088 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3089 $site_name, href(project=>undef, action=>"project_index"));
3090 printf('<link rel="alternate" title="%s projects feeds" '.
3091 'href="%s" type="text/x-opml" />'."\n",
3092 $site_name, href(project=>undef, action=>"opml"));
3093 }
3094 if (defined $favicon) {
3095 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3096 }
3097
3098 print "</head>\n" .
3099 "<body>\n";
3100
3101 if (-f $site_header) {
3102 insert_file($site_header);
3103 }
3104
3105 print "<div class=\"page_header\">\n" .
3106 $cgi->a({-href => esc_url($logo_url),
3107 -title => $logo_label},
3108 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3109 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3110 if (defined $project) {
3111 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3112 if (defined $action) {
3113 print " / $action";
3114 }
3115 print "\n";
3116 }
3117 print "</div>\n";
3118
3119 my $have_search = gitweb_check_feature('search');
3120 if (defined $project && $have_search) {
3121 if (!defined $searchtext) {
3122 $searchtext = "";
3123 }
3124 my $search_hash;
3125 if (defined $hash_base) {
3126 $search_hash = $hash_base;
3127 } elsif (defined $hash) {
3128 $search_hash = $hash;
3129 } else {
3130 $search_hash = "HEAD";
3131 }
3132 my $action = $my_uri;
3133 my $use_pathinfo = gitweb_check_feature('pathinfo');
3134 if ($use_pathinfo) {
3135 $action .= "/".esc_url($project);
3136 }
3137 print $cgi->startform(-method => "get", -action => $action) .
3138 "<div class=\"search\">\n" .
3139 (!$use_pathinfo &&
3140 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3141 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3142 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3143 $cgi->popup_menu(-name => 'st', -default => 'commit',
3144 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3145 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3146 " search:\n",
3147 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3148 "<span title=\"Extended regular expression\">" .
3149 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3150 -checked => $search_use_regexp) .
3151 "</span>" .
3152 "</div>" .
3153 $cgi->end_form() . "\n";
3154 }
3155 }
3156
3157 sub git_footer_html {
3158 my $feed_class = 'rss_logo';
3159
3160 print "<div class=\"page_footer\">\n";
3161 if (defined $project) {
3162 my $descr = git_get_project_description($project);
3163 if (defined $descr) {
3164 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3165 }
3166
3167 my %href_params = get_feed_info();
3168 if (!%href_params) {
3169 $feed_class .= ' generic';
3170 }
3171 $href_params{'-title'} ||= 'log';
3172
3173 foreach my $format qw(RSS Atom) {
3174 $href_params{'action'} = lc($format);
3175 print $cgi->a({-href => href(%href_params),
3176 -title => "$href_params{'-title'} $format feed",
3177 -class => $feed_class}, $format)."\n";
3178 }
3179
3180 } else {
3181 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3182 -class => $feed_class}, "OPML") . " ";
3183 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3184 -class => $feed_class}, "TXT") . "\n";
3185 }
3186 print "</div>\n"; # class="page_footer"
3187
3188 if (-f $site_footer) {
3189 insert_file($site_footer);
3190 }
3191
3192 print "</body>\n" .
3193 "</html>";
3194 }
3195
3196 # die_error(<http_status_code>, <error_message>)
3197 # Example: die_error(404, 'Hash not found')
3198 # By convention, use the following status codes (as defined in RFC 2616):
3199 # 400: Invalid or missing CGI parameters, or
3200 # requested object exists but has wrong type.
3201 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3202 # this server or project.
3203 # 404: Requested object/revision/project doesn't exist.
3204 # 500: The server isn't configured properly, or
3205 # an internal error occurred (e.g. failed assertions caused by bugs), or
3206 # an unknown error occurred (e.g. the git binary died unexpectedly).
3207 sub die_error {
3208 my $status = shift || 500;
3209 my $error = shift || "Internal server error";
3210
3211 my %http_responses = (400 => '400 Bad Request',
3212 403 => '403 Forbidden',
3213 404 => '404 Not Found',
3214 500 => '500 Internal Server Error');
3215 git_header_html($http_responses{$status});
3216 print <<EOF;
3217 <div class="page_body">
3218 <br /><br />
3219 $status - $error
3220 <br />
3221 </div>
3222 EOF
3223 git_footer_html();
3224 exit;
3225 }
3226
3227 ## ----------------------------------------------------------------------
3228 ## functions printing or outputting HTML: navigation
3229
3230 sub git_print_page_nav {
3231 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3232 $extra = '' if !defined $extra; # pager or formats
3233
3234 my @navs = qw(summary shortlog log commit commitdiff tree);
3235 if ($suppress) {
3236 @navs = grep { $_ ne $suppress } @navs;
3237 }
3238
3239 my %arg = map { $_ => {action=>$_} } @navs;
3240 if (defined $head) {
3241 for (qw(commit commitdiff)) {
3242 $arg{$_}{'hash'} = $head;
3243 }
3244 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3245 for (qw(shortlog log)) {
3246 $arg{$_}{'hash'} = $head;
3247 }
3248 }
3249 }
3250
3251 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3252 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3253
3254 my @actions = gitweb_get_feature('actions');
3255 my %repl = (
3256 '%' => '%',
3257 'n' => $project, # project name
3258 'f' => $git_dir, # project path within filesystem
3259 'h' => $treehead || '', # current hash ('h' parameter)
3260 'b' => $treebase || '', # hash base ('hb' parameter)
3261 );
3262 while (@actions) {
3263 my ($label, $link, $pos) = splice(@actions,0,3);
3264 # insert
3265 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3266 # munch munch
3267 $link =~ s/%([%nfhb])/$repl{$1}/g;
3268 $arg{$label}{'_href'} = $link;
3269 }
3270
3271 print "<div class=\"page_nav\">\n" .
3272 (join " | ",
3273 map { $_ eq $current ?
3274 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3275 } @navs);
3276 print "<br/>\n$extra<br/>\n" .
3277 "</div>\n";
3278 }
3279
3280 sub format_paging_nav {
3281 my ($action, $hash, $head, $page, $has_next_link) = @_;
3282 my $paging_nav;
3283
3284
3285 if ($hash ne $head || $page) {
3286 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3287 } else {
3288 $paging_nav .= "HEAD";
3289 }
3290
3291 if ($page > 0) {
3292 $paging_nav .= " &sdot; " .
3293 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3294 -accesskey => "p", -title => "Alt-p"}, "prev");
3295 } else {
3296 $paging_nav .= " &sdot; prev";
3297 }
3298
3299 if ($has_next_link) {
3300 $paging_nav .= " &sdot; " .
3301 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3302 -accesskey => "n", -title => "Alt-n"}, "next");
3303 } else {
3304 $paging_nav .= " &sdot; next";
3305 }
3306
3307 return $paging_nav;
3308 }
3309
3310 ## ......................................................................
3311 ## functions printing or outputting HTML: div
3312
3313 sub git_print_header_div {
3314 my ($action, $title, $hash, $hash_base) = @_;
3315 my %args = ();
3316
3317 $args{'action'} = $action;
3318 $args{'hash'} = $hash if $hash;
3319 $args{'hash_base'} = $hash_base if $hash_base;
3320
3321 print "<div class=\"header\">\n" .
3322 $cgi->a({-href => href(%args), -class => "title"},
3323 $title ? $title : $action) .
3324 "\n</div>\n";
3325 }
3326
3327 sub print_local_time {
3328 my %date = @_;
3329 if ($date{'hour_local'} < 6) {
3330 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3331 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3332 } else {
3333 printf(" (%02d:%02d %s)",
3334 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3335 }
3336 }
3337
3338 # Outputs the author name and date in long form
3339 sub git_print_authorship {
3340 my $co = shift;
3341 my %opts = @_;
3342 my $tag = $opts{-tag} || 'div';
3343
3344 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3345 print "<$tag class=\"author_date\">" .
3346 esc_html($co->{'author_name'}) .
3347 " [$ad{'rfc2822'}";
3348 print_local_time(%ad) if ($opts{-localtime});
3349 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3350 . "</$tag>\n";
3351 }
3352
3353 # Outputs table rows containing the full author or committer information,
3354 # in the format expected for 'commit' view (& similia).
3355 # Parameters are a commit hash reference, followed by the list of people
3356 # to output information for. If the list is empty it defalts to both
3357 # author and committer.
3358 sub git_print_authorship_rows {
3359 my $co = shift;
3360 # too bad we can't use @people = @_ || ('author', 'committer')
3361 my @people = @_;
3362 @people = ('author', 'committer') unless @people;
3363 foreach my $who (@people) {
3364 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3365 print "<tr><td>$who</td><td>" . esc_html($co->{$who}) . "</td>" .
3366 "<td rowspan=\"2\">" .
3367 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3368 "</td></tr>\n" .
3369 "<tr>" .
3370 "<td></td><td> $wd{'rfc2822'}";
3371 print_local_time(%wd);
3372 print "</td>" .
3373 "</tr>\n";
3374 }
3375 }
3376
3377 sub git_print_page_path {
3378 my $name = shift;
3379 my $type = shift;
3380 my $hb = shift;
3381
3382
3383 print "<div class=\"page_path\">";
3384 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3385 -title => 'tree root'}, to_utf8("[$project]"));
3386 print " / ";
3387 if (defined $name) {
3388 my @dirname = split '/', $name;
3389 my $basename = pop @dirname;
3390 my $fullname = '';
3391
3392 foreach my $dir (@dirname) {
3393 $fullname .= ($fullname ? '/' : '') . $dir;
3394 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3395 hash_base=>$hb),
3396 -title => $fullname}, esc_path($dir));
3397 print " / ";
3398 }
3399 if (defined $type && $type eq 'blob') {
3400 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3401 hash_base=>$hb),
3402 -title => $name}, esc_path($basename));
3403 } elsif (defined $type && $type eq 'tree') {
3404 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3405 hash_base=>$hb),
3406 -title => $name}, esc_path($basename));
3407 print " / ";
3408 } else {
3409 print esc_path($basename);
3410 }
3411 }
3412 print "<br/></div>\n";
3413 }
3414
3415 sub git_print_log {
3416 my $log = shift;
3417 my %opts = @_;
3418
3419 if ($opts{'-remove_title'}) {
3420 # remove title, i.e. first line of log
3421 shift @$log;
3422 }
3423 # remove leading empty lines
3424 while (defined $log->[0] && $log->[0] eq "") {
3425 shift @$log;
3426 }
3427
3428 # print log
3429 my $signoff = 0;
3430 my $empty = 0;
3431 foreach my $line (@$log) {
3432 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3433 $signoff = 1;
3434 $empty = 0;
3435 if (! $opts{'-remove_signoff'}) {
3436 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3437 next;
3438 } else {
3439 # remove signoff lines
3440 next;
3441 }
3442 } else {
3443 $signoff = 0;
3444 }
3445
3446 # print only one empty line
3447 # do not print empty line after signoff
3448 if ($line eq "") {
3449 next if ($empty || $signoff);
3450 $empty = 1;
3451 } else {
3452 $empty = 0;
3453 }
3454
3455 print format_log_line_html($line) . "<br/>\n";
3456 }
3457
3458 if ($opts{'-final_empty_line'}) {
3459 # end with single empty line
3460 print "<br/>\n" unless $empty;
3461 }
3462 }
3463
3464 # return link target (what link points to)
3465 sub git_get_link_target {
3466 my $hash = shift;
3467 my $link_target;
3468
3469 # read link
3470 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3471 or return;
3472 {
3473 local $/ = undef;
3474 $link_target = <$fd>;
3475 }
3476 close $fd
3477 or return;
3478
3479 return $link_target;
3480 }
3481
3482 # given link target, and the directory (basedir) the link is in,
3483 # return target of link relative to top directory (top tree);
3484 # return undef if it is not possible (including absolute links).
3485 sub normalize_link_target {
3486 my ($link_target, $basedir) = @_;
3487
3488 # absolute symlinks (beginning with '/') cannot be normalized
3489 return if (substr($link_target, 0, 1) eq '/');
3490
3491 # normalize link target to path from top (root) tree (dir)
3492 my $path;
3493 if ($basedir) {
3494 $path = $basedir . '/' . $link_target;
3495 } else {
3496 # we are in top (root) tree (dir)
3497 $path = $link_target;
3498 }
3499
3500 # remove //, /./, and /../
3501 my @path_parts;
3502 foreach my $part (split('/', $path)) {
3503 # discard '.' and ''
3504 next if (!$part || $part eq '.');
3505 # handle '..'
3506 if ($part eq '..') {
3507 if (@path_parts) {
3508 pop @path_parts;
3509 } else {
3510 # link leads outside repository (outside top dir)
3511 return;
3512 }
3513 } else {
3514 push @path_parts, $part;
3515 }
3516 }
3517 $path = join('/', @path_parts);
3518
3519 return $path;
3520 }
3521
3522 # print tree entry (row of git_tree), but without encompassing <tr> element
3523 sub git_print_tree_entry {
3524 my ($t, $basedir, $hash_base, $have_blame) = @_;
3525
3526 my %base_key = ();
3527 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3528
3529 # The format of a table row is: mode list link. Where mode is
3530 # the mode of the entry, list is the name of the entry, an href,
3531 # and link is the action links of the entry.
3532
3533 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3534 if ($t->{'type'} eq "blob") {
3535 print "<td class=\"list\">" .
3536 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3537 file_name=>"$basedir$t->{'name'}", %base_key),
3538 -class => "list"}, esc_path($t->{'name'}));
3539 if (S_ISLNK(oct $t->{'mode'})) {
3540 my $link_target = git_get_link_target($t->{'hash'});
3541 if ($link_target) {
3542 my $norm_target = normalize_link_target($link_target, $basedir);
3543 if (defined $norm_target) {
3544 print " -> " .
3545 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3546 file_name=>$norm_target),
3547 -title => $norm_target}, esc_path($link_target));
3548 } else {
3549 print " -> " . esc_path($link_target);
3550 }
3551 }
3552 }
3553 print "</td>\n";
3554 print "<td class=\"link\">";
3555 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3556 file_name=>"$basedir$t->{'name'}", %base_key)},
3557 "blob");
3558 if ($have_blame) {
3559 print " | " .
3560 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3561 file_name=>"$basedir$t->{'name'}", %base_key)},
3562 "blame");
3563 }
3564 if (defined $hash_base) {
3565 print " | " .
3566 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3567 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3568 "history");
3569 }
3570 print " | " .
3571 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3572 file_name=>"$basedir$t->{'name'}")},
3573 "raw");
3574 print "</td>\n";
3575
3576 } elsif ($t->{'type'} eq "tree") {
3577 print "<td class=\"list\">";
3578 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3579 file_name=>"$basedir$t->{'name'}", %base_key)},
3580 esc_path($t->{'name'}));
3581 print "</td>\n";
3582 print "<td class=\"link\">";
3583 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3584 file_name=>"$basedir$t->{'name'}", %base_key)},
3585 "tree");
3586 if (defined $hash_base) {
3587 print " | " .
3588 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3589 file_name=>"$basedir$t->{'name'}")},
3590 "history");
3591 }
3592 print "</td>\n";
3593 } else {
3594 # unknown object: we can only present history for it
3595 # (this includes 'commit' object, i.e. submodule support)
3596 print "<td class=\"list\">" .
3597 esc_path($t->{'name'}) .
3598 "</td>\n";
3599 print "<td class=\"link\">";
3600 if (defined $hash_base) {
3601 print $cgi->a({-href => href(action=>"history",
3602 hash_base=>$hash_base,
3603 file_name=>"$basedir$t->{'name'}")},
3604 "history");
3605 }
3606 print "</td>\n";
3607 }
3608 }
3609
3610 ## ......................................................................
3611 ## functions printing large fragments of HTML
3612
3613 # get pre-image filenames for merge (combined) diff
3614 sub fill_from_file_info {
3615 my ($diff, @parents) = @_;
3616
3617 $diff->{'from_file'} = [ ];
3618 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3619 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3620 if ($diff->{'status'}[$i] eq 'R' ||
3621 $diff->{'status'}[$i] eq 'C') {
3622 $diff->{'from_file'}[$i] =
3623 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3624 }
3625 }
3626
3627 return $diff;
3628 }
3629
3630 # is current raw difftree line of file deletion
3631 sub is_deleted {
3632 my $diffinfo = shift;
3633
3634 return $diffinfo->{'to_id'} eq ('0' x 40);
3635 }
3636
3637 # does patch correspond to [previous] difftree raw line
3638 # $diffinfo - hashref of parsed raw diff format
3639 # $patchinfo - hashref of parsed patch diff format
3640 # (the same keys as in $diffinfo)
3641 sub is_patch_split {
3642 my ($diffinfo, $patchinfo) = @_;
3643
3644 return defined $diffinfo && defined $patchinfo
3645 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3646 }
3647
3648
3649 sub git_difftree_body {
3650 my ($difftree, $hash, @parents) = @_;
3651 my ($parent) = $parents[0];
3652 my $have_blame = gitweb_check_feature('blame');
3653 print "<div class=\"list_head\">\n";
3654 if ($#{$difftree} > 10) {
3655 print(($#{$difftree} + 1) . " files changed:\n");
3656 }
3657 print "</div>\n";
3658
3659 print "<table class=\"" .
3660 (@parents > 1 ? "combined " : "") .
3661 "diff_tree\">\n";
3662
3663 # header only for combined diff in 'commitdiff' view
3664 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3665 if ($has_header) {
3666 # table header
3667 print "<thead><tr>\n" .
3668 "<th></th><th></th>\n"; # filename, patchN link
3669 for (my $i = 0; $i < @parents; $i++) {
3670 my $par = $parents[$i];
3671 print "<th>" .
3672 $cgi->a({-href => href(action=>"commitdiff",
3673 hash=>$hash, hash_parent=>$par),
3674 -title => 'commitdiff to parent number ' .
3675 ($i+1) . ': ' . substr($par,0,7)},
3676 $i+1) .
3677 "&nbsp;</th>\n";
3678 }
3679 print "</tr></thead>\n<tbody>\n";
3680 }
3681
3682 my $alternate = 1;
3683 my $patchno = 0;
3684 foreach my $line (@{$difftree}) {
3685 my $diff = parsed_difftree_line($line);
3686
3687 if ($alternate) {
3688 print "<tr class=\"dark\">\n";
3689 } else {
3690 print "<tr class=\"light\">\n";
3691 }
3692 $alternate ^= 1;
3693
3694 if (exists $diff->{'nparents'}) { # combined diff
3695
3696 fill_from_file_info($diff, @parents)
3697 unless exists $diff->{'from_file'};
3698
3699 if (!is_deleted($diff)) {
3700 # file exists in the result (child) commit
3701 print "<td>" .
3702 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3703 file_name=>$diff->{'to_file'},
3704 hash_base=>$hash),
3705 -class => "list"}, esc_path($diff->{'to_file'})) .
3706 "</td>\n";
3707 } else {
3708 print "<td>" .
3709 esc_path($diff->{'to_file'}) .
3710 "</td>\n";
3711 }
3712
3713 if ($action eq 'commitdiff') {
3714 # link to patch
3715 $patchno++;
3716 print "<td class=\"link\">" .
3717 $cgi->a({-href => "#patch$patchno"}, "patch") .
3718 " | " .
3719 "</td>\n";
3720 }
3721
3722 my $has_history = 0;
3723 my $not_deleted = 0;
3724 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3725 my $hash_parent = $parents[$i];
3726 my $from_hash = $diff->{'from_id'}[$i];
3727 my $from_path = $diff->{'from_file'}[$i];
3728 my $status = $diff->{'status'}[$i];
3729
3730 $has_history ||= ($status ne 'A');
3731 $not_deleted ||= ($status ne 'D');
3732
3733 if ($status eq 'A') {
3734 print "<td class=\"link\" align=\"right\"> | </td>\n";
3735 } elsif ($status eq 'D') {
3736 print "<td class=\"link\">" .
3737 $cgi->a({-href => href(action=>"blob",
3738 hash_base=>$hash,
3739 hash=>$from_hash,
3740 file_name=>$from_path)},
3741 "blob" . ($i+1)) .
3742 " | </td>\n";
3743 } else {
3744 if ($diff->{'to_id'} eq $from_hash) {
3745 print "<td class=\"link nochange\">";
3746 } else {
3747 print "<td class=\"link\">";
3748 }
3749 print $cgi->a({-href => href(action=>"blobdiff",
3750 hash=>$diff->{'to_id'},
3751 hash_parent=>$from_hash,
3752 hash_base=>$hash,
3753 hash_parent_base=>$hash_parent,
3754 file_name=>$diff->{'to_file'},
3755 file_parent=>$from_path)},
3756 "diff" . ($i+1)) .
3757 " | </td>\n";
3758 }
3759 }
3760
3761 print "<td class=\"link\">";
3762 if ($not_deleted) {
3763 print $cgi->a({-href => href(action=>"blob",
3764 hash=>$diff->{'to_id'},
3765 file_name=>$diff->{'to_file'},
3766 hash_base=>$hash)},
3767 "blob");
3768 print " | " if ($has_history);
3769 }
3770 if ($has_history) {
3771 print $cgi->a({-href => href(action=>"history",
3772 file_name=>$diff->{'to_file'},
3773 hash_base=>$hash)},
3774 "history");
3775 }
3776 print "</td>\n";
3777
3778 print "</tr>\n";
3779 next; # instead of 'else' clause, to avoid extra indent
3780 }
3781 # else ordinary diff
3782
3783 my ($to_mode_oct, $to_mode_str, $to_file_type);
3784 my ($from_mode_oct, $from_mode_str, $from_file_type);
3785 if ($diff->{'to_mode'} ne ('0' x 6)) {
3786 $to_mode_oct = oct $diff->{'to_mode'};
3787 if (S_ISREG($to_mode_oct)) { # only for regular file
3788 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3789 }
3790 $to_file_type = file_type($diff->{'to_mode'});
3791 }
3792 if ($diff->{'from_mode'} ne ('0' x 6)) {
3793 $from_mode_oct = oct $diff->{'from_mode'};
3794 if (S_ISREG($to_mode_oct)) { # only for regular file
3795 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3796 }
3797 $from_file_type = file_type($diff->{'from_mode'});
3798 }
3799
3800 if ($diff->{'status'} eq "A") { # created
3801 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3802 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3803 $mode_chng .= "]</span>";
3804 print "<td>";
3805 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3806 hash_base=>$hash, 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->{'to_id'},
3818 hash_base=>$hash, file_name=>$diff->{'file'})},
3819 "blob");
3820 print "</td>\n";
3821
3822 } elsif ($diff->{'status'} eq "D") { # deleted
3823 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3824 print "<td>";
3825 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3826 hash_base=>$parent, file_name=>$diff->{'file'}),
3827 -class => "list"}, esc_path($diff->{'file'}));
3828 print "</td>\n";
3829 print "<td>$mode_chng</td>\n";
3830 print "<td class=\"link\">";
3831 if ($action eq 'commitdiff') {
3832 # link to patch
3833 $patchno++;
3834 print $cgi->a({-href => "#patch$patchno"}, "patch");
3835 print " | ";
3836 }
3837 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3838 hash_base=>$parent, file_name=>$diff->{'file'})},
3839 "blob") . " | ";
3840 if ($have_blame) {
3841 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3842 file_name=>$diff->{'file'})},
3843 "blame") . " | ";
3844 }
3845 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3846 file_name=>$diff->{'file'})},
3847 "history");
3848 print "</td>\n";
3849
3850 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3851 my $mode_chnge = "";
3852 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3853 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3854 if ($from_file_type ne $to_file_type) {
3855 $mode_chnge .= " from $from_file_type to $to_file_type";
3856 }
3857 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3858 if ($from_mode_str && $to_mode_str) {
3859 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3860 } elsif ($to_mode_str) {
3861 $mode_chnge .= " mode: $to_mode_str";
3862 }
3863 }
3864 $mode_chnge .= "]</span>\n";
3865 }
3866 print "<td>";
3867 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3868 hash_base=>$hash, file_name=>$diff->{'file'}),
3869 -class => "list"}, esc_path($diff->{'file'}));
3870 print "</td>\n";
3871 print "<td>$mode_chnge</td>\n";
3872 print "<td class=\"link\">";
3873 if ($action eq 'commitdiff') {
3874 # link to patch
3875 $patchno++;
3876 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3877 " | ";
3878 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3879 # "commit" view and modified file (not onlu mode changed)
3880 print $cgi->a({-href => href(action=>"blobdiff",
3881 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3882 hash_base=>$hash, hash_parent_base=>$parent,
3883 file_name=>$diff->{'file'})},
3884 "diff") .
3885 " | ";
3886 }
3887 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3888 hash_base=>$hash, file_name=>$diff->{'file'})},
3889 "blob") . " | ";
3890 if ($have_blame) {
3891 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3892 file_name=>$diff->{'file'})},
3893 "blame") . " | ";
3894 }
3895 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3896 file_name=>$diff->{'file'})},
3897 "history");
3898 print "</td>\n";
3899
3900 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3901 my %status_name = ('R' => 'moved', 'C' => 'copied');
3902 my $nstatus = $status_name{$diff->{'status'}};
3903 my $mode_chng = "";
3904 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3905 # mode also for directories, so we cannot use $to_mode_str
3906 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3907 }
3908 print "<td>" .
3909 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3910 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3911 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3912 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3913 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3914 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3915 -class => "list"}, esc_path($diff->{'from_file'})) .
3916 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3917 "<td class=\"link\">";
3918 if ($action eq 'commitdiff') {
3919 # link to patch
3920 $patchno++;
3921 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3922 " | ";
3923 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3924 # "commit" view and modified file (not only pure rename or copy)
3925 print $cgi->a({-href => href(action=>"blobdiff",
3926 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3927 hash_base=>$hash, hash_parent_base=>$parent,
3928 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3929 "diff") .
3930 " | ";
3931 }
3932 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3933 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3934 "blob") . " | ";
3935 if ($have_blame) {
3936 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3937 file_name=>$diff->{'to_file'})},
3938 "blame") . " | ";
3939 }
3940 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3941 file_name=>$diff->{'to_file'})},
3942 "history");
3943 print "</td>\n";
3944
3945 } # we should not encounter Unmerged (U) or Unknown (X) status
3946 print "</tr>\n";
3947 }
3948 print "</tbody>" if $has_header;
3949 print "</table>\n";
3950 }
3951
3952 sub git_patchset_body {
3953 my ($fd, $difftree, $hash, @hash_parents) = @_;
3954 my ($hash_parent) = $hash_parents[0];
3955
3956 my $is_combined = (@hash_parents > 1);
3957 my $patch_idx = 0;
3958 my $patch_number = 0;
3959 my $patch_line;
3960 my $diffinfo;
3961 my $to_name;
3962 my (%from, %to);
3963
3964 print "<div class=\"patchset\">\n";
3965
3966 # skip to first patch
3967 while ($patch_line = <$fd>) {
3968 chomp $patch_line;
3969
3970 last if ($patch_line =~ m/^diff /);
3971 }
3972
3973 PATCH:
3974 while ($patch_line) {
3975
3976 # parse "git diff" header line
3977 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3978 # $1 is from_name, which we do not use
3979 $to_name = unquote($2);
3980 $to_name =~ s!^b/!!;
3981 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3982 # $1 is 'cc' or 'combined', which we do not use
3983 $to_name = unquote($2);
3984 } else {
3985 $to_name = undef;
3986 }
3987
3988 # check if current patch belong to current raw line
3989 # and parse raw git-diff line if needed
3990 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3991 # this is continuation of a split patch
3992 print "<div class=\"patch cont\">\n";
3993 } else {
3994 # advance raw git-diff output if needed
3995 $patch_idx++ if defined $diffinfo;
3996
3997 # read and prepare patch information
3998 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3999
4000 # compact combined diff output can have some patches skipped
4001 # find which patch (using pathname of result) we are at now;
4002 if ($is_combined) {
4003 while ($to_name ne $diffinfo->{'to_file'}) {
4004 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4005 format_diff_cc_simplified($diffinfo, @hash_parents) .
4006 "</div>\n"; # class="patch"
4007
4008 $patch_idx++;
4009 $patch_number++;
4010
4011 last if $patch_idx > $#$difftree;
4012 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4013 }
4014 }
4015
4016 # modifies %from, %to hashes
4017 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4018
4019 # this is first patch for raw difftree line with $patch_idx index
4020 # we index @$difftree array from 0, but number patches from 1
4021 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4022 }
4023
4024 # git diff header
4025 #assert($patch_line =~ m/^diff /) if DEBUG;
4026 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4027 $patch_number++;
4028 # print "git diff" header
4029 print format_git_diff_header_line($patch_line, $diffinfo,
4030 \%from, \%to);
4031
4032 # print extended diff header
4033 print "<div class=\"diff extended_header\">\n";
4034 EXTENDED_HEADER:
4035 while ($patch_line = <$fd>) {
4036 chomp $patch_line;
4037
4038 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4039
4040 print format_extended_diff_header_line($patch_line, $diffinfo,
4041 \%from, \%to);
4042 }
4043 print "</div>\n"; # class="diff extended_header"
4044
4045 # from-file/to-file diff header
4046 if (! $patch_line) {
4047 print "</div>\n"; # class="patch"
4048 last PATCH;
4049 }
4050 next PATCH if ($patch_line =~ m/^diff /);
4051 #assert($patch_line =~ m/^---/) if DEBUG;
4052
4053 my $last_patch_line = $patch_line;
4054 $patch_line = <$fd>;
4055 chomp $patch_line;
4056 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4057
4058 print format_diff_from_to_header($last_patch_line, $patch_line,
4059 $diffinfo, \%from, \%to,
4060 @hash_parents);
4061
4062 # the patch itself
4063 LINE:
4064 while ($patch_line = <$fd>) {
4065 chomp $patch_line;
4066
4067 next PATCH if ($patch_line =~ m/^diff /);
4068
4069 print format_diff_line($patch_line, \%from, \%to);
4070 }
4071
4072 } continue {
4073 print "</div>\n"; # class="patch"
4074 }
4075
4076 # for compact combined (--cc) format, with chunk and patch simpliciaction
4077 # patchset might be empty, but there might be unprocessed raw lines
4078 for (++$patch_idx if $patch_number > 0;
4079 $patch_idx < @$difftree;
4080 ++$patch_idx) {
4081 # read and prepare patch information
4082 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4083
4084 # generate anchor for "patch" links in difftree / whatchanged part
4085 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4086 format_diff_cc_simplified($diffinfo, @hash_parents) .
4087 "</div>\n"; # class="patch"
4088
4089 $patch_number++;
4090 }
4091
4092 if ($patch_number == 0) {
4093 if (@hash_parents > 1) {
4094 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4095 } else {
4096 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4097 }
4098 }
4099
4100 print "</div>\n"; # class="patchset"
4101 }
4102
4103 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4104
4105 # fills project list info (age, description, owner, forks) for each
4106 # project in the list, removing invalid projects from returned list
4107 # NOTE: modifies $projlist, but does not remove entries from it
4108 sub fill_project_list_info {
4109 my ($projlist, $check_forks) = @_;
4110 my @projects;
4111
4112 my $show_ctags = gitweb_check_feature('ctags');
4113 PROJECT:
4114 foreach my $pr (@$projlist) {
4115 my (@activity) = git_get_last_activity($pr->{'path'});
4116 unless (@activity) {
4117 next PROJECT;
4118 }
4119 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4120 if (!defined $pr->{'descr'}) {
4121 my $descr = git_get_project_description($pr->{'path'}) || "";
4122 $descr = to_utf8($descr);
4123 $pr->{'descr_long'} = $descr;
4124 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4125 }
4126 if (!defined $pr->{'owner'}) {
4127 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4128 }
4129 if ($check_forks) {
4130 my $pname = $pr->{'path'};
4131 if (($pname =~ s/\.git$//) &&
4132 ($pname !~ /\/$/) &&
4133 (-d "$projectroot/$pname")) {
4134 $pr->{'forks'} = "-d $projectroot/$pname";
4135 } else {
4136 $pr->{'forks'} = 0;
4137 }
4138 }
4139 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4140 push @projects, $pr;
4141 }
4142
4143 return @projects;
4144 }
4145
4146 # print 'sort by' <th> element, generating 'sort by $name' replay link
4147 # if that order is not selected
4148 sub print_sort_th {
4149 my ($name, $order, $header) = @_;
4150 $header ||= ucfirst($name);
4151
4152 if ($order eq $name) {
4153 print "<th>$header</th>\n";
4154 } else {
4155 print "<th>" .
4156 $cgi->a({-href => href(-replay=>1, order=>$name),
4157 -class => "header"}, $header) .
4158 "</th>\n";
4159 }
4160 }
4161
4162 sub git_project_list_body {
4163 # actually uses global variable $project
4164 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4165
4166 my $check_forks = gitweb_check_feature('forks');
4167 my @projects = fill_project_list_info($projlist, $check_forks);
4168
4169 $order ||= $default_projects_order;
4170 $from = 0 unless defined $from;
4171 $to = $#projects if (!defined $to || $#projects < $to);
4172
4173 my %order_info = (
4174 project => { key => 'path', type => 'str' },
4175 descr => { key => 'descr_long', type => 'str' },
4176 owner => { key => 'owner', type => 'str' },
4177 age => { key => 'age', type => 'num' }
4178 );
4179 my $oi = $order_info{$order};
4180 if ($oi->{'type'} eq 'str') {
4181 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4182 } else {
4183 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4184 }
4185
4186 my $show_ctags = gitweb_check_feature('ctags');
4187 if ($show_ctags) {
4188 my %ctags;
4189 foreach my $p (@projects) {
4190 foreach my $ct (keys %{$p->{'ctags'}}) {
4191 $ctags{$ct} += $p->{'ctags'}->{$ct};
4192 }
4193 }
4194 my $cloud = git_populate_project_tagcloud(\%ctags);
4195 print git_show_project_tagcloud($cloud, 64);
4196 }
4197
4198 print "<table class=\"project_list\">\n";
4199 unless ($no_header) {
4200 print "<tr>\n";
4201 if ($check_forks) {
4202 print "<th></th>\n";
4203 }
4204 print_sort_th('project', $order, 'Project');
4205 print_sort_th('descr', $order, 'Description');
4206 print_sort_th('owner', $order, 'Owner');
4207 print_sort_th('age', $order, 'Last Change');
4208 print "<th></th>\n" . # for links
4209 "</tr>\n";
4210 }
4211 my $alternate = 1;
4212 my $tagfilter = $cgi->param('by_tag');
4213 for (my $i = $from; $i <= $to; $i++) {
4214 my $pr = $projects[$i];
4215
4216 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4217 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4218 and not $pr->{'descr_long'} =~ /$searchtext/;
4219 # Weed out forks or non-matching entries of search
4220 if ($check_forks) {
4221 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4222 $forkbase="^$forkbase" if $forkbase;
4223 next if not $searchtext and not $tagfilter and $show_ctags
4224 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4225 }
4226
4227 if ($alternate) {
4228 print "<tr class=\"dark\">\n";
4229 } else {
4230 print "<tr class=\"light\">\n";
4231 }
4232 $alternate ^= 1;
4233 if ($check_forks) {
4234 print "<td>";
4235 if ($pr->{'forks'}) {
4236 print "<!-- $pr->{'forks'} -->\n";
4237 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4238 }
4239 print "</td>\n";
4240 }
4241 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4242 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4243 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4244 -class => "list", -title => $pr->{'descr_long'}},
4245 esc_html($pr->{'descr'})) . "</td>\n" .
4246 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4247 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4248 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4249 "<td class=\"link\">" .
4250 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4251 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4252 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4253 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4254 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4255 "</td>\n" .
4256 "</tr>\n";
4257 }
4258 if (defined $extra) {
4259 print "<tr>\n";
4260 if ($check_forks) {
4261 print "<td></td>\n";
4262 }
4263 print "<td colspan=\"5\">$extra</td>\n" .
4264 "</tr>\n";
4265 }
4266 print "</table>\n";
4267 }
4268
4269 sub git_shortlog_body {
4270 # uses global variable $project
4271 my ($commitlist, $from, $to, $refs, $extra) = @_;
4272
4273 $from = 0 unless defined $from;
4274 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4275
4276 print "<table class=\"shortlog\">\n";
4277 my $alternate = 1;
4278 for (my $i = $from; $i <= $to; $i++) {
4279 my %co = %{$commitlist->[$i]};
4280 my $commit = $co{'id'};
4281 my $ref = format_ref_marker($refs, $commit);
4282 if ($alternate) {
4283 print "<tr class=\"dark\">\n";
4284 } else {
4285 print "<tr class=\"light\">\n";
4286 }
4287 $alternate ^= 1;
4288 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4289 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4290 format_author_html('td', \%co, 10) . "<td>";
4291 print format_subject_html($co{'title'}, $co{'title_short'},
4292 href(action=>"commit", hash=>$commit), $ref);
4293 print "</td>\n" .
4294 "<td class=\"link\">" .
4295 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4296 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4297 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4298 my $snapshot_links = format_snapshot_links($commit);
4299 if (defined $snapshot_links) {
4300 print " | " . $snapshot_links;
4301 }
4302 print "</td>\n" .
4303 "</tr>\n";
4304 }
4305 if (defined $extra) {
4306 print "<tr>\n" .
4307 "<td colspan=\"4\">$extra</td>\n" .
4308 "</tr>\n";
4309 }
4310 print "</table>\n";
4311 }
4312
4313 sub git_history_body {
4314 # Warning: assumes constant type (blob or tree) during history
4315 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4316
4317 $from = 0 unless defined $from;
4318 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4319
4320 print "<table class=\"history\">\n";
4321 my $alternate = 1;
4322 for (my $i = $from; $i <= $to; $i++) {
4323 my %co = %{$commitlist->[$i]};
4324 if (!%co) {
4325 next;
4326 }
4327 my $commit = $co{'id'};
4328
4329 my $ref = format_ref_marker($refs, $commit);
4330
4331 if ($alternate) {
4332 print "<tr class=\"dark\">\n";
4333 } else {
4334 print "<tr class=\"light\">\n";
4335 }
4336 $alternate ^= 1;
4337 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4338 # shortlog: format_author_html('td', \%co, 10)
4339 format_author_html('td', \%co, 15, 3) . "<td>";
4340 # originally git_history used chop_str($co{'title'}, 50)
4341 print format_subject_html($co{'title'}, $co{'title_short'},
4342 href(action=>"commit", hash=>$commit), $ref);
4343 print "</td>\n" .
4344 "<td class=\"link\">" .
4345 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4346 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4347
4348 if ($ftype eq 'blob') {
4349 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4350 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4351 if (defined $blob_current && defined $blob_parent &&
4352 $blob_current ne $blob_parent) {
4353 print " | " .
4354 $cgi->a({-href => href(action=>"blobdiff",
4355 hash=>$blob_current, hash_parent=>$blob_parent,
4356 hash_base=>$hash_base, hash_parent_base=>$commit,
4357 file_name=>$file_name)},
4358 "diff to current");
4359 }
4360 }
4361 print "</td>\n" .
4362 "</tr>\n";
4363 }
4364 if (defined $extra) {
4365 print "<tr>\n" .
4366 "<td colspan=\"4\">$extra</td>\n" .
4367 "</tr>\n";
4368 }
4369 print "</table>\n";
4370 }
4371
4372 sub git_tags_body {
4373 # uses global variable $project
4374 my ($taglist, $from, $to, $extra) = @_;
4375 $from = 0 unless defined $from;
4376 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4377
4378 print "<table class=\"tags\">\n";
4379 my $alternate = 1;
4380 for (my $i = $from; $i <= $to; $i++) {
4381 my $entry = $taglist->[$i];
4382 my %tag = %$entry;
4383 my $comment = $tag{'subject'};
4384 my $comment_short;
4385 if (defined $comment) {
4386 $comment_short = chop_str($comment, 30, 5);
4387 }
4388 if ($alternate) {
4389 print "<tr class=\"dark\">\n";
4390 } else {
4391 print "<tr class=\"light\">\n";
4392 }
4393 $alternate ^= 1;
4394 if (defined $tag{'age'}) {
4395 print "<td><i>$tag{'age'}</i></td>\n";
4396 } else {
4397 print "<td></td>\n";
4398 }
4399 print "<td>" .
4400 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4401 -class => "list name"}, esc_html($tag{'name'})) .
4402 "</td>\n" .
4403 "<td>";
4404 if (defined $comment) {
4405 print format_subject_html($comment, $comment_short,
4406 href(action=>"tag", hash=>$tag{'id'}));
4407 }
4408 print "</td>\n" .
4409 "<td class=\"selflink\">";
4410 if ($tag{'type'} eq "tag") {
4411 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4412 } else {
4413 print "&nbsp;";
4414 }
4415 print "</td>\n" .
4416 "<td class=\"link\">" . " | " .
4417 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4418 if ($tag{'reftype'} eq "commit") {
4419 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4420 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4421 } elsif ($tag{'reftype'} eq "blob") {
4422 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4423 }
4424 print "</td>\n" .
4425 "</tr>";
4426 }
4427 if (defined $extra) {
4428 print "<tr>\n" .
4429 "<td colspan=\"5\">$extra</td>\n" .
4430 "</tr>\n";
4431 }
4432 print "</table>\n";
4433 }
4434
4435 sub git_heads_body {
4436 # uses global variable $project
4437 my ($headlist, $head, $from, $to, $extra) = @_;
4438 $from = 0 unless defined $from;
4439 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4440
4441 print "<table class=\"heads\">\n";
4442 my $alternate = 1;
4443 for (my $i = $from; $i <= $to; $i++) {
4444 my $entry = $headlist->[$i];
4445 my %ref = %$entry;
4446 my $curr = $ref{'id'} eq $head;
4447 if ($alternate) {
4448 print "<tr class=\"dark\">\n";
4449 } else {
4450 print "<tr class=\"light\">\n";
4451 }
4452 $alternate ^= 1;
4453 print "<td><i>$ref{'age'}</i></td>\n" .
4454 ($curr ? "<td class=\"current_head\">" : "<td>") .
4455 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4456 -class => "list name"},esc_html($ref{'name'})) .
4457 "</td>\n" .
4458 "<td class=\"link\">" .
4459 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4460 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4461 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4462 "</td>\n" .
4463 "</tr>";
4464 }
4465 if (defined $extra) {
4466 print "<tr>\n" .
4467 "<td colspan=\"3\">$extra</td>\n" .
4468 "</tr>\n";
4469 }
4470 print "</table>\n";
4471 }
4472
4473 sub git_search_grep_body {
4474 my ($commitlist, $from, $to, $extra) = @_;
4475 $from = 0 unless defined $from;
4476 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4477
4478 print "<table class=\"commit_search\">\n";
4479 my $alternate = 1;
4480 for (my $i = $from; $i <= $to; $i++) {
4481 my %co = %{$commitlist->[$i]};
4482 if (!%co) {
4483 next;
4484 }
4485 my $commit = $co{'id'};
4486 if ($alternate) {
4487 print "<tr class=\"dark\">\n";
4488 } else {
4489 print "<tr class=\"light\">\n";
4490 }
4491 $alternate ^= 1;
4492 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4493 format_author_html('td', \%co, 15, 5) .
4494 "<td>" .
4495 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4496 -class => "list subject"},
4497 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4498 my $comment = $co{'comment'};
4499 foreach my $line (@$comment) {
4500 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4501 my ($lead, $match, $trail) = ($1, $2, $3);
4502 $match = chop_str($match, 70, 5, 'center');
4503 my $contextlen = int((80 - length($match))/2);
4504 $contextlen = 30 if ($contextlen > 30);
4505 $lead = chop_str($lead, $contextlen, 10, 'left');
4506 $trail = chop_str($trail, $contextlen, 10, 'right');
4507
4508 $lead = esc_html($lead);
4509 $match = esc_html($match);
4510 $trail = esc_html($trail);
4511
4512 print "$lead<span class=\"match\">$match</span>$trail<br />";
4513 }
4514 }
4515 print "</td>\n" .
4516 "<td class=\"link\">" .
4517 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4518 " | " .
4519 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4520 " | " .
4521 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4522 print "</td>\n" .
4523 "</tr>\n";
4524 }
4525 if (defined $extra) {
4526 print "<tr>\n" .
4527 "<td colspan=\"3\">$extra</td>\n" .
4528 "</tr>\n";
4529 }
4530 print "</table>\n";
4531 }
4532
4533 ## ======================================================================
4534 ## ======================================================================
4535 ## actions
4536
4537 sub git_project_list {
4538 my $order = $input_params{'order'};
4539 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4540 die_error(400, "Unknown order parameter");
4541 }
4542
4543 my @list = git_get_projects_list();
4544 if (!@list) {
4545 die_error(404, "No projects found");
4546 }
4547
4548 git_header_html();
4549 if (-f $home_text) {
4550 print "<div class=\"index_include\">\n";
4551 insert_file($home_text);
4552 print "</div>\n";
4553 }
4554 print $cgi->startform(-method => "get") .
4555 "<p class=\"projsearch\">Search:\n" .
4556 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4557 "</p>" .
4558 $cgi->end_form() . "\n";
4559 git_project_list_body(\@list, $order);
4560 git_footer_html();
4561 }
4562
4563 sub git_forks {
4564 my $order = $input_params{'order'};
4565 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4566 die_error(400, "Unknown order parameter");
4567 }
4568
4569 my @list = git_get_projects_list($project);
4570 if (!@list) {
4571 die_error(404, "No forks found");
4572 }
4573
4574 git_header_html();
4575 git_print_page_nav('','');
4576 git_print_header_div('summary', "$project forks");
4577 git_project_list_body(\@list, $order);
4578 git_footer_html();
4579 }
4580
4581 sub git_project_index {
4582 my @projects = git_get_projects_list($project);
4583
4584 print $cgi->header(
4585 -type => 'text/plain',
4586 -charset => 'utf-8',
4587 -content_disposition => 'inline; filename="index.aux"');
4588
4589 foreach my $pr (@projects) {
4590 if (!exists $pr->{'owner'}) {
4591 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4592 }
4593
4594 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4595 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4596 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4597 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4598 $path =~ s/ /\+/g;
4599 $owner =~ s/ /\+/g;
4600
4601 print "$path $owner\n";
4602 }
4603 }
4604
4605 sub git_summary {
4606 my $descr = git_get_project_description($project) || "none";
4607 my %co = parse_commit("HEAD");
4608 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4609 my $head = $co{'id'};
4610
4611 my $owner = git_get_project_owner($project);
4612
4613 my $refs = git_get_references();
4614 # These get_*_list functions return one more to allow us to see if
4615 # there are more ...
4616 my @taglist = git_get_tags_list(16);
4617 my @headlist = git_get_heads_list(16);
4618 my @forklist;
4619 my $check_forks = gitweb_check_feature('forks');
4620
4621 if ($check_forks) {
4622 @forklist = git_get_projects_list($project);
4623 }
4624
4625 git_header_html();
4626 git_print_page_nav('summary','', $head);
4627
4628 print "<div class=\"title\">&nbsp;</div>\n";
4629 print "<table class=\"projects_list\">\n" .
4630 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4631 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4632 if (defined $cd{'rfc2822'}) {
4633 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4634 }
4635
4636 # use per project git URL list in $projectroot/$project/cloneurl
4637 # or make project git URL from git base URL and project name
4638 my $url_tag = "URL";
4639 my @url_list = git_get_project_url_list($project);
4640 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4641 foreach my $git_url (@url_list) {
4642 next unless $git_url;
4643 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4644 $url_tag = "";
4645 }
4646
4647 # Tag cloud
4648 my $show_ctags = gitweb_check_feature('ctags');
4649 if ($show_ctags) {
4650 my $ctags = git_get_project_ctags($project);
4651 my $cloud = git_populate_project_tagcloud($ctags);
4652 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4653 print "</td>\n<td>" unless %$ctags;
4654 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4655 print "</td>\n<td>" if %$ctags;
4656 print git_show_project_tagcloud($cloud, 48);
4657 print "</td></tr>";
4658 }
4659
4660 print "</table>\n";
4661
4662 # If XSS prevention is on, we don't include README.html.
4663 # TODO: Allow a readme in some safe format.
4664 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4665 print "<div class=\"title\">readme</div>\n" .
4666 "<div class=\"readme\">\n";
4667 insert_file("$projectroot/$project/README.html");
4668 print "\n</div>\n"; # class="readme"
4669 }
4670
4671 # we need to request one more than 16 (0..15) to check if
4672 # those 16 are all
4673 my @commitlist = $head ? parse_commits($head, 17) : ();
4674 if (@commitlist) {
4675 git_print_header_div('shortlog');
4676 git_shortlog_body(\@commitlist, 0, 15, $refs,
4677 $#commitlist <= 15 ? undef :
4678 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4679 }
4680
4681 if (@taglist) {
4682 git_print_header_div('tags');
4683 git_tags_body(\@taglist, 0, 15,
4684 $#taglist <= 15 ? undef :
4685 $cgi->a({-href => href(action=>"tags")}, "..."));
4686 }
4687
4688 if (@headlist) {
4689 git_print_header_div('heads');
4690 git_heads_body(\@headlist, $head, 0, 15,
4691 $#headlist <= 15 ? undef :
4692 $cgi->a({-href => href(action=>"heads")}, "..."));
4693 }
4694
4695 if (@forklist) {
4696 git_print_header_div('forks');
4697 git_project_list_body(\@forklist, 'age', 0, 15,
4698 $#forklist <= 15 ? undef :
4699 $cgi->a({-href => href(action=>"forks")}, "..."),
4700 'no_header');
4701 }
4702
4703 git_footer_html();
4704 }
4705
4706 sub git_tag {
4707 my $head = git_get_head_hash($project);
4708 git_header_html();
4709 git_print_page_nav('','', $head,undef,$head);
4710 my %tag = parse_tag($hash);
4711
4712 if (! %tag) {
4713 die_error(404, "Unknown tag object");
4714 }
4715
4716 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4717 print "<div class=\"title_text\">\n" .
4718 "<table class=\"object_header\">\n" .
4719 "<tr>\n" .
4720 "<td>object</td>\n" .
4721 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4722 $tag{'object'}) . "</td>\n" .
4723 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4724 $tag{'type'}) . "</td>\n" .
4725 "</tr>\n";
4726 if (defined($tag{'author'})) {
4727 git_print_authorship_rows(\%tag, 'author');
4728 }
4729 print "</table>\n\n" .
4730 "</div>\n";
4731 print "<div class=\"page_body\">";
4732 my $comment = $tag{'comment'};
4733 foreach my $line (@$comment) {
4734 chomp $line;
4735 print esc_html($line, -nbsp=>1) . "<br/>\n";
4736 }
4737 print "</div>\n";
4738 git_footer_html();
4739 }
4740
4741 sub git_blame {
4742 # permissions
4743 gitweb_check_feature('blame')
4744 or die_error(403, "Blame view not allowed");
4745
4746 # error checking
4747 die_error(400, "No file name given") unless $file_name;
4748 $hash_base ||= git_get_head_hash($project);
4749 die_error(404, "Couldn't find base commit") unless $hash_base;
4750 my %co = parse_commit($hash_base)
4751 or die_error(404, "Commit not found");
4752 my $ftype = "blob";
4753 if (!defined $hash) {
4754 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4755 or die_error(404, "Error looking up file");
4756 } else {
4757 $ftype = git_get_type($hash);
4758 if ($ftype !~ "blob") {
4759 die_error(400, "Object is not a blob");
4760 }
4761 }
4762
4763 # run git-blame --porcelain
4764 open my $fd, "-|", git_cmd(), "blame", '-p',
4765 $hash_base, '--', $file_name
4766 or die_error(500, "Open git-blame failed");
4767
4768 # page header
4769 git_header_html();
4770 my $formats_nav =
4771 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4772 "blob") .
4773 " | " .
4774 $cgi->a({-href => href(action=>"history", -replay=>1)},
4775 "history") .
4776 " | " .
4777 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4778 "HEAD");
4779 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4780 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4781 git_print_page_path($file_name, $ftype, $hash_base);
4782
4783 # page body
4784 my @rev_color = qw(light2 dark2);
4785 my $num_colors = scalar(@rev_color);
4786 my $current_color = 0;
4787 my %metainfo = ();
4788
4789 print <<HTML;
4790 <div class="page_body">
4791 <table class="blame">
4792 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4793 HTML
4794 LINE:
4795 while (my $line = <$fd>) {
4796 chomp $line;
4797 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
4798 # no <lines in group> for subsequent lines in group of lines
4799 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4800 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
4801 if (!exists $metainfo{$full_rev}) {
4802 $metainfo{$full_rev} = {};
4803 }
4804 my $meta = $metainfo{$full_rev};
4805 my $data;
4806 while ($data = <$fd>) {
4807 chomp $data;
4808 last if ($data =~ s/^\t//); # contents of line
4809 if ($data =~ /^(\S+) (.*)$/) {
4810 $meta->{$1} = $2;
4811 }
4812 }
4813 my $short_rev = substr($full_rev, 0, 8);
4814 my $author = $meta->{'author'};
4815 my %date =
4816 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
4817 my $date = $date{'iso-tz'};
4818 if ($group_size) {
4819 $current_color = ($current_color + 1) % $num_colors;
4820 }
4821 print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
4822 if ($group_size) {
4823 print "<td class=\"sha1\"";
4824 print " title=\"". esc_html($author) . ", $date\"";
4825 print " rowspan=\"$group_size\"" if ($group_size > 1);
4826 print ">";
4827 print $cgi->a({-href => href(action=>"commit",
4828 hash=>$full_rev,
4829 file_name=>$file_name)},
4830 esc_html($short_rev));
4831 print "</td>\n";
4832 }
4833 my $parent_commit;
4834 if (!exists $meta->{'parent'}) {
4835 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4836 or die_error(500, "Open git-rev-parse failed");
4837 $parent_commit = <$dd>;
4838 close $dd;
4839 chomp($parent_commit);
4840 $meta->{'parent'} = $parent_commit;
4841 } else {
4842 $parent_commit = $meta->{'parent'};
4843 }
4844 my $blamed = href(action => 'blame',
4845 file_name => $meta->{'filename'},
4846 hash_base => $parent_commit);
4847 print "<td class=\"linenr\">";
4848 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4849 -class => "linenr" },
4850 esc_html($lineno));
4851 print "</td>";
4852 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4853 print "</tr>\n";
4854 }
4855 print "</table>\n";
4856 print "</div>";
4857 close $fd
4858 or print "Reading blob failed\n";
4859
4860 # page footer
4861 git_footer_html();
4862 }
4863
4864 sub git_tags {
4865 my $head = git_get_head_hash($project);
4866 git_header_html();
4867 git_print_page_nav('','', $head,undef,$head);
4868 git_print_header_div('summary', $project);
4869
4870 my @tagslist = git_get_tags_list();
4871 if (@tagslist) {
4872 git_tags_body(\@tagslist);
4873 }
4874 git_footer_html();
4875 }
4876
4877 sub git_heads {
4878 my $head = git_get_head_hash($project);
4879 git_header_html();
4880 git_print_page_nav('','', $head,undef,$head);
4881 git_print_header_div('summary', $project);
4882
4883 my @headslist = git_get_heads_list();
4884 if (@headslist) {
4885 git_heads_body(\@headslist, $head);
4886 }
4887 git_footer_html();
4888 }
4889
4890 sub git_blob_plain {
4891 my $type = shift;
4892 my $expires;
4893
4894 if (!defined $hash) {
4895 if (defined $file_name) {
4896 my $base = $hash_base || git_get_head_hash($project);
4897 $hash = git_get_hash_by_path($base, $file_name, "blob")
4898 or die_error(404, "Cannot find file");
4899 } else {
4900 die_error(400, "No file name defined");
4901 }
4902 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4903 # blobs defined by non-textual hash id's can be cached
4904 $expires = "+1d";
4905 }
4906
4907 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4908 or die_error(500, "Open git-cat-file blob '$hash' failed");
4909
4910 # content-type (can include charset)
4911 $type = blob_contenttype($fd, $file_name, $type);
4912
4913 # "save as" filename, even when no $file_name is given
4914 my $save_as = "$hash";
4915 if (defined $file_name) {
4916 $save_as = $file_name;
4917 } elsif ($type =~ m/^text\//) {
4918 $save_as .= '.txt';
4919 }
4920
4921 # With XSS prevention on, blobs of all types except a few known safe
4922 # ones are served with "Content-Disposition: attachment" to make sure
4923 # they don't run in our security domain. For certain image types,
4924 # blob view writes an <img> tag referring to blob_plain view, and we
4925 # want to be sure not to break that by serving the image as an
4926 # attachment (though Firefox 3 doesn't seem to care).
4927 my $sandbox = $prevent_xss &&
4928 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
4929
4930 print $cgi->header(
4931 -type => $type,
4932 -expires => $expires,
4933 -content_disposition =>
4934 ($sandbox ? 'attachment' : 'inline')
4935 . '; filename="' . $save_as . '"');
4936 local $/ = undef;
4937 binmode STDOUT, ':raw';
4938 print <$fd>;
4939 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4940 close $fd;
4941 }
4942
4943 sub git_blob {
4944 my $expires;
4945
4946 if (!defined $hash) {
4947 if (defined $file_name) {
4948 my $base = $hash_base || git_get_head_hash($project);
4949 $hash = git_get_hash_by_path($base, $file_name, "blob")
4950 or die_error(404, "Cannot find file");
4951 } else {
4952 die_error(400, "No file name defined");
4953 }
4954 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4955 # blobs defined by non-textual hash id's can be cached
4956 $expires = "+1d";
4957 }
4958
4959 my $have_blame = gitweb_check_feature('blame');
4960 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4961 or die_error(500, "Couldn't cat $file_name, $hash");
4962 my $mimetype = blob_mimetype($fd, $file_name);
4963 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4964 close $fd;
4965 return git_blob_plain($mimetype);
4966 }
4967 # we can have blame only for text/* mimetype
4968 $have_blame &&= ($mimetype =~ m!^text/!);
4969
4970 git_header_html(undef, $expires);
4971 my $formats_nav = '';
4972 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4973 if (defined $file_name) {
4974 if ($have_blame) {
4975 $formats_nav .=
4976 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4977 "blame") .
4978 " | ";
4979 }
4980 $formats_nav .=
4981 $cgi->a({-href => href(action=>"history", -replay=>1)},
4982 "history") .
4983 " | " .
4984 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4985 "raw") .
4986 " | " .
4987 $cgi->a({-href => href(action=>"blob",
4988 hash_base=>"HEAD", file_name=>$file_name)},
4989 "HEAD");
4990 } else {
4991 $formats_nav .=
4992 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4993 "raw");
4994 }
4995 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4996 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4997 } else {
4998 print "<div class=\"page_nav\">\n" .
4999 "<br/><br/></div>\n" .
5000 "<div class=\"title\">$hash</div>\n";
5001 }
5002 git_print_page_path($file_name, "blob", $hash_base);
5003 print "<div class=\"page_body\">\n";
5004 if ($mimetype =~ m!^image/!) {
5005 print qq!<img type="$mimetype"!;
5006 if ($file_name) {
5007 print qq! alt="$file_name" title="$file_name"!;
5008 }
5009 print qq! src="! .
5010 href(action=>"blob_plain", hash=>$hash,
5011 hash_base=>$hash_base, file_name=>$file_name) .
5012 qq!" />\n!;
5013 } else {
5014 my $nr;
5015 while (my $line = <$fd>) {
5016 chomp $line;
5017 $nr++;
5018 $line = untabify($line);
5019 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5020 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5021 }
5022 }
5023 close $fd
5024 or print "Reading blob failed.\n";
5025 print "</div>";
5026 git_footer_html();
5027 }
5028
5029 sub git_tree {
5030 if (!defined $hash_base) {
5031 $hash_base = "HEAD";
5032 }
5033 if (!defined $hash) {
5034 if (defined $file_name) {
5035 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5036 } else {
5037 $hash = $hash_base;
5038 }
5039 }
5040 die_error(404, "No such tree") unless defined($hash);
5041
5042 my @entries = ();
5043 {
5044 local $/ = "\0";
5045 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
5046 or die_error(500, "Open git-ls-tree failed");
5047 @entries = map { chomp; $_ } <$fd>;
5048 close $fd
5049 or die_error(404, "Reading tree failed");
5050 }
5051
5052 my $refs = git_get_references();
5053 my $ref = format_ref_marker($refs, $hash_base);
5054 git_header_html();
5055 my $basedir = '';
5056 my $have_blame = gitweb_check_feature('blame');
5057 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5058 my @views_nav = ();
5059 if (defined $file_name) {
5060 push @views_nav,
5061 $cgi->a({-href => href(action=>"history", -replay=>1)},
5062 "history"),
5063 $cgi->a({-href => href(action=>"tree",
5064 hash_base=>"HEAD", file_name=>$file_name)},
5065 "HEAD"),
5066 }
5067 my $snapshot_links = format_snapshot_links($hash);
5068 if (defined $snapshot_links) {
5069 # FIXME: Should be available when we have no hash base as well.
5070 push @views_nav, $snapshot_links;
5071 }
5072 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
5073 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5074 } else {
5075 undef $hash_base;
5076 print "<div class=\"page_nav\">\n";
5077 print "<br/><br/></div>\n";
5078 print "<div class=\"title\">$hash</div>\n";
5079 }
5080 if (defined $file_name) {
5081 $basedir = $file_name;
5082 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5083 $basedir .= '/';
5084 }
5085 git_print_page_path($file_name, 'tree', $hash_base);
5086 }
5087 print "<div class=\"page_body\">\n";
5088 print "<table class=\"tree\">\n";
5089 my $alternate = 1;
5090 # '..' (top directory) link if possible
5091 if (defined $hash_base &&
5092 defined $file_name && $file_name =~ m![^/]+$!) {
5093 if ($alternate) {
5094 print "<tr class=\"dark\">\n";
5095 } else {
5096 print "<tr class=\"light\">\n";
5097 }
5098 $alternate ^= 1;
5099
5100 my $up = $file_name;
5101 $up =~ s!/?[^/]+$!!;
5102 undef $up unless $up;
5103 # based on git_print_tree_entry
5104 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5105 print '<td class="list">';
5106 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
5107 file_name=>$up)},
5108 "..");
5109 print "</td>\n";
5110 print "<td class=\"link\"></td>\n";
5111
5112 print "</tr>\n";
5113 }
5114 foreach my $line (@entries) {
5115 my %t = parse_ls_tree_line($line, -z => 1);
5116
5117 if ($alternate) {
5118 print "<tr class=\"dark\">\n";
5119 } else {
5120 print "<tr class=\"light\">\n";
5121 }
5122 $alternate ^= 1;
5123
5124 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5125
5126 print "</tr>\n";
5127 }
5128 print "</table>\n" .
5129 "</div>";
5130 git_footer_html();
5131 }
5132
5133 sub git_snapshot {
5134 my $format = $input_params{'snapshot_format'};
5135 if (!@snapshot_fmts) {
5136 die_error(403, "Snapshots not allowed");
5137 }
5138 # default to first supported snapshot format
5139 $format ||= $snapshot_fmts[0];
5140 if ($format !~ m/^[a-z0-9]+$/) {
5141 die_error(400, "Invalid snapshot format parameter");
5142 } elsif (!exists($known_snapshot_formats{$format})) {
5143 die_error(400, "Unknown snapshot format");
5144 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5145 die_error(403, "Unsupported snapshot format");
5146 }
5147
5148 if (!defined $hash) {
5149 $hash = git_get_head_hash($project);
5150 }
5151
5152 my $name = $project;
5153 $name =~ s,([^/])/*\.git$,$1,;
5154 $name = basename($name);
5155 my $filename = to_utf8($name);
5156 $name =~ s/\047/\047\\\047\047/g;
5157 my $cmd;
5158 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
5159 $cmd = quote_command(
5160 git_cmd(), 'archive',
5161 "--format=$known_snapshot_formats{$format}{'format'}",
5162 "--prefix=$name/", $hash);
5163 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5164 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5165 }
5166
5167 print $cgi->header(
5168 -type => $known_snapshot_formats{$format}{'type'},
5169 -content_disposition => 'inline; filename="' . "$filename" . '"',
5170 -status => '200 OK');
5171
5172 open my $fd, "-|", $cmd
5173 or die_error(500, "Execute git-archive failed");
5174 binmode STDOUT, ':raw';
5175 print <$fd>;
5176 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5177 close $fd;
5178 }
5179
5180 sub git_log {
5181 my $head = git_get_head_hash($project);
5182 if (!defined $hash) {
5183 $hash = $head;
5184 }
5185 if (!defined $page) {
5186 $page = 0;
5187 }
5188 my $refs = git_get_references();
5189
5190 my @commitlist = parse_commits($hash, 101, (100 * $page));
5191
5192 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
5193
5194 my ($patch_max) = gitweb_get_feature('patches');
5195 if ($patch_max) {
5196 if ($patch_max < 0 || @commitlist <= $patch_max) {
5197 $paging_nav .= " &sdot; " .
5198 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5199 "patches");
5200 }
5201 }
5202
5203 git_header_html();
5204 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
5205
5206 if (!@commitlist) {
5207 my %co = parse_commit($hash);
5208
5209 git_print_header_div('summary', $project);
5210 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
5211 }
5212 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5213 for (my $i = 0; $i <= $to; $i++) {
5214 my %co = %{$commitlist[$i]};
5215 next if !%co;
5216 my $commit = $co{'id'};
5217 my $ref = format_ref_marker($refs, $commit);
5218 my %ad = parse_date($co{'author_epoch'});
5219 git_print_header_div('commit',
5220 "<span class=\"age\">$co{'age_string'}</span>" .
5221 esc_html($co{'title'}) . $ref,
5222 $commit);
5223 print "<div class=\"title_text\">\n" .
5224 "<div class=\"log_link\">\n" .
5225 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5226 " | " .
5227 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5228 " | " .
5229 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5230 "<br/>\n" .
5231 "</div>\n";
5232 git_print_authorship(\%co, -tag => 'span');
5233 print "<br/>\n</div>\n";
5234
5235 print "<div class=\"log_body\">\n";
5236 git_print_log($co{'comment'}, -final_empty_line=> 1);
5237 print "</div>\n";
5238 }
5239 if ($#commitlist >= 100) {
5240 print "<div class=\"page_nav\">\n";
5241 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
5242 -accesskey => "n", -title => "Alt-n"}, "next");
5243 print "</div>\n";
5244 }
5245 git_footer_html();
5246 }
5247
5248 sub git_commit {
5249 $hash ||= $hash_base || "HEAD";
5250 my %co = parse_commit($hash)
5251 or die_error(404, "Unknown commit object");
5252
5253 my $parent = $co{'parent'};
5254 my $parents = $co{'parents'}; # listref
5255
5256 # we need to prepare $formats_nav before any parameter munging
5257 my $formats_nav;
5258 if (!defined $parent) {
5259 # --root commitdiff
5260 $formats_nav .= '(initial)';
5261 } elsif (@$parents == 1) {
5262 # single parent commit
5263 $formats_nav .=
5264 '(parent: ' .
5265 $cgi->a({-href => href(action=>"commit",
5266 hash=>$parent)},
5267 esc_html(substr($parent, 0, 7))) .
5268 ')';
5269 } else {
5270 # merge commit
5271 $formats_nav .=
5272 '(merge: ' .
5273 join(' ', map {
5274 $cgi->a({-href => href(action=>"commit",
5275 hash=>$_)},
5276 esc_html(substr($_, 0, 7)));
5277 } @$parents ) .
5278 ')';
5279 }
5280 if (gitweb_check_feature('patches')) {
5281 $formats_nav .= " | " .
5282 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5283 "patch");
5284 }
5285
5286 if (!defined $parent) {
5287 $parent = "--root";
5288 }
5289 my @difftree;
5290 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5291 @diff_opts,
5292 (@$parents <= 1 ? $parent : '-c'),
5293 $hash, "--"
5294 or die_error(500, "Open git-diff-tree failed");
5295 @difftree = map { chomp; $_ } <$fd>;
5296 close $fd or die_error(404, "Reading git-diff-tree failed");
5297
5298 # non-textual hash id's can be cached
5299 my $expires;
5300 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5301 $expires = "+1d";
5302 }
5303 my $refs = git_get_references();
5304 my $ref = format_ref_marker($refs, $co{'id'});
5305
5306 git_header_html(undef, $expires);
5307 git_print_page_nav('commit', '',
5308 $hash, $co{'tree'}, $hash,
5309 $formats_nav);
5310
5311 if (defined $co{'parent'}) {
5312 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5313 } else {
5314 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5315 }
5316 print "<div class=\"title_text\">\n" .
5317 "<table class=\"object_header\">\n";
5318 git_print_authorship_rows(\%co);
5319 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5320 print "<tr>" .
5321 "<td>tree</td>" .
5322 "<td class=\"sha1\">" .
5323 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5324 class => "list"}, $co{'tree'}) .
5325 "</td>" .
5326 "<td class=\"link\">" .
5327 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5328 "tree");
5329 my $snapshot_links = format_snapshot_links($hash);
5330 if (defined $snapshot_links) {
5331 print " | " . $snapshot_links;
5332 }
5333 print "</td>" .
5334 "</tr>\n";
5335
5336 foreach my $par (@$parents) {
5337 print "<tr>" .
5338 "<td>parent</td>" .
5339 "<td class=\"sha1\">" .
5340 $cgi->a({-href => href(action=>"commit", hash=>$par),
5341 class => "list"}, $par) .
5342 "</td>" .
5343 "<td class=\"link\">" .
5344 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5345 " | " .
5346 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5347 "</td>" .
5348 "</tr>\n";
5349 }
5350 print "</table>".
5351 "</div>\n";
5352
5353 print "<div class=\"page_body\">\n";
5354 git_print_log($co{'comment'});
5355 print "</div>\n";
5356
5357 git_difftree_body(\@difftree, $hash, @$parents);
5358
5359 git_footer_html();
5360 }
5361
5362 sub git_object {
5363 # object is defined by:
5364 # - hash or hash_base alone
5365 # - hash_base and file_name
5366 my $type;
5367
5368 # - hash or hash_base alone
5369 if ($hash || ($hash_base && !defined $file_name)) {
5370 my $object_id = $hash || $hash_base;
5371
5372 open my $fd, "-|", quote_command(
5373 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5374 or die_error(404, "Object does not exist");
5375 $type = <$fd>;
5376 chomp $type;
5377 close $fd
5378 or die_error(404, "Object does not exist");
5379
5380 # - hash_base and file_name
5381 } elsif ($hash_base && defined $file_name) {
5382 $file_name =~ s,/+$,,;
5383
5384 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5385 or die_error(404, "Base object does not exist");
5386
5387 # here errors should not hapen
5388 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5389 or die_error(500, "Open git-ls-tree failed");
5390 my $line = <$fd>;
5391 close $fd;
5392
5393 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5394 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5395 die_error(404, "File or directory for given base does not exist");
5396 }
5397 $type = $2;
5398 $hash = $3;
5399 } else {
5400 die_error(400, "Not enough information to find object");
5401 }
5402
5403 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5404 hash=>$hash, hash_base=>$hash_base,
5405 file_name=>$file_name),
5406 -status => '302 Found');
5407 }
5408
5409 sub git_blobdiff {
5410 my $format = shift || 'html';
5411
5412 my $fd;
5413 my @difftree;
5414 my %diffinfo;
5415 my $expires;
5416
5417 # preparing $fd and %diffinfo for git_patchset_body
5418 # new style URI
5419 if (defined $hash_base && defined $hash_parent_base) {
5420 if (defined $file_name) {
5421 # read raw output
5422 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5423 $hash_parent_base, $hash_base,
5424 "--", (defined $file_parent ? $file_parent : ()), $file_name
5425 or die_error(500, "Open git-diff-tree failed");
5426 @difftree = map { chomp; $_ } <$fd>;
5427 close $fd
5428 or die_error(404, "Reading git-diff-tree failed");
5429 @difftree
5430 or die_error(404, "Blob diff not found");
5431
5432 } elsif (defined $hash &&
5433 $hash =~ /[0-9a-fA-F]{40}/) {
5434 # try to find filename from $hash
5435
5436 # read filtered raw output
5437 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5438 $hash_parent_base, $hash_base, "--"
5439 or die_error(500, "Open git-diff-tree failed");
5440 @difftree =
5441 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5442 # $hash == to_id
5443 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5444 map { chomp; $_ } <$fd>;
5445 close $fd
5446 or die_error(404, "Reading git-diff-tree failed");
5447 @difftree
5448 or die_error(404, "Blob diff not found");
5449
5450 } else {
5451 die_error(400, "Missing one of the blob diff parameters");
5452 }
5453
5454 if (@difftree > 1) {
5455 die_error(400, "Ambiguous blob diff specification");
5456 }
5457
5458 %diffinfo = parse_difftree_raw_line($difftree[0]);
5459 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5460 $file_name ||= $diffinfo{'to_file'};
5461
5462 $hash_parent ||= $diffinfo{'from_id'};
5463 $hash ||= $diffinfo{'to_id'};
5464
5465 # non-textual hash id's can be cached
5466 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5467 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5468 $expires = '+1d';
5469 }
5470
5471 # open patch output
5472 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5473 '-p', ($format eq 'html' ? "--full-index" : ()),
5474 $hash_parent_base, $hash_base,
5475 "--", (defined $file_parent ? $file_parent : ()), $file_name
5476 or die_error(500, "Open git-diff-tree failed");
5477 }
5478
5479 # old/legacy style URI -- not generated anymore since 1.4.3.
5480 if (!%diffinfo) {
5481 die_error('404 Not Found', "Missing one of the blob diff parameters")
5482 }
5483
5484 # header
5485 if ($format eq 'html') {
5486 my $formats_nav =
5487 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5488 "raw");
5489 git_header_html(undef, $expires);
5490 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5491 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5492 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5493 } else {
5494 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5495 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5496 }
5497 if (defined $file_name) {
5498 git_print_page_path($file_name, "blob", $hash_base);
5499 } else {
5500 print "<div class=\"page_path\"></div>\n";
5501 }
5502
5503 } elsif ($format eq 'plain') {
5504 print $cgi->header(
5505 -type => 'text/plain',
5506 -charset => 'utf-8',
5507 -expires => $expires,
5508 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5509
5510 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5511
5512 } else {
5513 die_error(400, "Unknown blobdiff format");
5514 }
5515
5516 # patch
5517 if ($format eq 'html') {
5518 print "<div class=\"page_body\">\n";
5519
5520 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5521 close $fd;
5522
5523 print "</div>\n"; # class="page_body"
5524 git_footer_html();
5525
5526 } else {
5527 while (my $line = <$fd>) {
5528 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5529 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5530
5531 print $line;
5532
5533 last if $line =~ m!^\+\+\+!;
5534 }
5535 local $/ = undef;
5536 print <$fd>;
5537 close $fd;
5538 }
5539 }
5540
5541 sub git_blobdiff_plain {
5542 git_blobdiff('plain');
5543 }
5544
5545 sub git_commitdiff {
5546 my %params = @_;
5547 my $format = $params{-format} || 'html';
5548
5549 my ($patch_max) = gitweb_get_feature('patches');
5550 if ($format eq 'patch') {
5551 die_error(403, "Patch view not allowed") unless $patch_max;
5552 }
5553
5554 $hash ||= $hash_base || "HEAD";
5555 my %co = parse_commit($hash)
5556 or die_error(404, "Unknown commit object");
5557
5558 # choose format for commitdiff for merge
5559 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5560 $hash_parent = '--cc';
5561 }
5562 # we need to prepare $formats_nav before almost any parameter munging
5563 my $formats_nav;
5564 if ($format eq 'html') {
5565 $formats_nav =
5566 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5567 "raw");
5568 if ($patch_max) {
5569 $formats_nav .= " | " .
5570 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5571 "patch");
5572 }
5573
5574 if (defined $hash_parent &&
5575 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5576 # commitdiff with two commits given
5577 my $hash_parent_short = $hash_parent;
5578 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5579 $hash_parent_short = substr($hash_parent, 0, 7);
5580 }
5581 $formats_nav .=
5582 ' (from';
5583 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5584 if ($co{'parents'}[$i] eq $hash_parent) {
5585 $formats_nav .= ' parent ' . ($i+1);
5586 last;
5587 }
5588 }
5589 $formats_nav .= ': ' .
5590 $cgi->a({-href => href(action=>"commitdiff",
5591 hash=>$hash_parent)},
5592 esc_html($hash_parent_short)) .
5593 ')';
5594 } elsif (!$co{'parent'}) {
5595 # --root commitdiff
5596 $formats_nav .= ' (initial)';
5597 } elsif (scalar @{$co{'parents'}} == 1) {
5598 # single parent commit
5599 $formats_nav .=
5600 ' (parent: ' .
5601 $cgi->a({-href => href(action=>"commitdiff",
5602 hash=>$co{'parent'})},
5603 esc_html(substr($co{'parent'}, 0, 7))) .
5604 ')';
5605 } else {
5606 # merge commit
5607 if ($hash_parent eq '--cc') {
5608 $formats_nav .= ' | ' .
5609 $cgi->a({-href => href(action=>"commitdiff",
5610 hash=>$hash, hash_parent=>'-c')},
5611 'combined');
5612 } else { # $hash_parent eq '-c'
5613 $formats_nav .= ' | ' .
5614 $cgi->a({-href => href(action=>"commitdiff",
5615 hash=>$hash, hash_parent=>'--cc')},
5616 'compact');
5617 }
5618 $formats_nav .=
5619 ' (merge: ' .
5620 join(' ', map {
5621 $cgi->a({-href => href(action=>"commitdiff",
5622 hash=>$_)},
5623 esc_html(substr($_, 0, 7)));
5624 } @{$co{'parents'}} ) .
5625 ')';
5626 }
5627 }
5628
5629 my $hash_parent_param = $hash_parent;
5630 if (!defined $hash_parent_param) {
5631 # --cc for multiple parents, --root for parentless
5632 $hash_parent_param =
5633 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5634 }
5635
5636 # read commitdiff
5637 my $fd;
5638 my @difftree;
5639 if ($format eq 'html') {
5640 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5641 "--no-commit-id", "--patch-with-raw", "--full-index",
5642 $hash_parent_param, $hash, "--"
5643 or die_error(500, "Open git-diff-tree failed");
5644
5645 while (my $line = <$fd>) {
5646 chomp $line;
5647 # empty line ends raw part of diff-tree output
5648 last unless $line;
5649 push @difftree, scalar parse_difftree_raw_line($line);
5650 }
5651
5652 } elsif ($format eq 'plain') {
5653 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5654 '-p', $hash_parent_param, $hash, "--"
5655 or die_error(500, "Open git-diff-tree failed");
5656 } elsif ($format eq 'patch') {
5657 # For commit ranges, we limit the output to the number of
5658 # patches specified in the 'patches' feature.
5659 # For single commits, we limit the output to a single patch,
5660 # diverging from the git-format-patch default.
5661 my @commit_spec = ();
5662 if ($hash_parent) {
5663 if ($patch_max > 0) {
5664 push @commit_spec, "-$patch_max";
5665 }
5666 push @commit_spec, '-n', "$hash_parent..$hash";
5667 } else {
5668 if ($params{-single}) {
5669 push @commit_spec, '-1';
5670 } else {
5671 if ($patch_max > 0) {
5672 push @commit_spec, "-$patch_max";
5673 }
5674 push @commit_spec, "-n";
5675 }
5676 push @commit_spec, '--root', $hash;
5677 }
5678 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
5679 '--stdout', @commit_spec
5680 or die_error(500, "Open git-format-patch failed");
5681 } else {
5682 die_error(400, "Unknown commitdiff format");
5683 }
5684
5685 # non-textual hash id's can be cached
5686 my $expires;
5687 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5688 $expires = "+1d";
5689 }
5690
5691 # write commit message
5692 if ($format eq 'html') {
5693 my $refs = git_get_references();
5694 my $ref = format_ref_marker($refs, $co{'id'});
5695
5696 git_header_html(undef, $expires);
5697 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5698 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5699 print "<div class=\"title_text\">\n" .
5700 "<table class=\"object_header\">\n";
5701 git_print_authorship_rows(\%co);
5702 print "</table>".
5703 "</div>\n";
5704 print "<div class=\"page_body\">\n";
5705 if (@{$co{'comment'}} > 1) {
5706 print "<div class=\"log\">\n";
5707 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5708 print "</div>\n"; # class="log"
5709 }
5710
5711 } elsif ($format eq 'plain') {
5712 my $refs = git_get_references("tags");
5713 my $tagname = git_get_rev_name_tags($hash);
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 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5722 print "From: " . to_utf8($co{'author'}) . "\n";
5723 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5724 print "Subject: " . to_utf8($co{'title'}) . "\n";
5725
5726 print "X-Git-Tag: $tagname\n" if $tagname;
5727 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5728
5729 foreach my $line (@{$co{'comment'}}) {
5730 print to_utf8($line) . "\n";
5731 }
5732 print "---\n\n";
5733 } elsif ($format eq 'patch') {
5734 my $filename = basename($project) . "-$hash.patch";
5735
5736 print $cgi->header(
5737 -type => 'text/plain',
5738 -charset => 'utf-8',
5739 -expires => $expires,
5740 -content_disposition => 'inline; filename="' . "$filename" . '"');
5741 }
5742
5743 # write patch
5744 if ($format eq 'html') {
5745 my $use_parents = !defined $hash_parent ||
5746 $hash_parent eq '-c' || $hash_parent eq '--cc';
5747 git_difftree_body(\@difftree, $hash,
5748 $use_parents ? @{$co{'parents'}} : $hash_parent);
5749 print "<br/>\n";
5750
5751 git_patchset_body($fd, \@difftree, $hash,
5752 $use_parents ? @{$co{'parents'}} : $hash_parent);
5753 close $fd;
5754 print "</div>\n"; # class="page_body"
5755 git_footer_html();
5756
5757 } elsif ($format eq 'plain') {
5758 local $/ = undef;
5759 print <$fd>;
5760 close $fd
5761 or print "Reading git-diff-tree failed\n";
5762 } elsif ($format eq 'patch') {
5763 local $/ = undef;
5764 print <$fd>;
5765 close $fd
5766 or print "Reading git-format-patch failed\n";
5767 }
5768 }
5769
5770 sub git_commitdiff_plain {
5771 git_commitdiff(-format => 'plain');
5772 }
5773
5774 # format-patch-style patches
5775 sub git_patch {
5776 git_commitdiff(-format => 'patch', -single=> 1);
5777 }
5778
5779 sub git_patches {
5780 git_commitdiff(-format => 'patch');
5781 }
5782
5783 sub git_history {
5784 if (!defined $hash_base) {
5785 $hash_base = git_get_head_hash($project);
5786 }
5787 if (!defined $page) {
5788 $page = 0;
5789 }
5790 my $ftype;
5791 my %co = parse_commit($hash_base)
5792 or die_error(404, "Unknown commit object");
5793
5794 my $refs = git_get_references();
5795 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5796
5797 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5798 $file_name, "--full-history")
5799 or die_error(404, "No such file or directory on given branch");
5800
5801 if (!defined $hash && defined $file_name) {
5802 # some commits could have deleted file in question,
5803 # and not have it in tree, but one of them has to have it
5804 for (my $i = 0; $i <= @commitlist; $i++) {
5805 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5806 last if defined $hash;
5807 }
5808 }
5809 if (defined $hash) {
5810 $ftype = git_get_type($hash);
5811 }
5812 if (!defined $ftype) {
5813 die_error(500, "Unknown type of object");
5814 }
5815
5816 my $paging_nav = '';
5817 if ($page > 0) {
5818 $paging_nav .=
5819 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5820 file_name=>$file_name)},
5821 "first");
5822 $paging_nav .= " &sdot; " .
5823 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5824 -accesskey => "p", -title => "Alt-p"}, "prev");
5825 } else {
5826 $paging_nav .= "first";
5827 $paging_nav .= " &sdot; prev";
5828 }
5829 my $next_link = '';
5830 if ($#commitlist >= 100) {
5831 $next_link =
5832 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5833 -accesskey => "n", -title => "Alt-n"}, "next");
5834 $paging_nav .= " &sdot; $next_link";
5835 } else {
5836 $paging_nav .= " &sdot; next";
5837 }
5838
5839 git_header_html();
5840 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5841 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5842 git_print_page_path($file_name, $ftype, $hash_base);
5843
5844 git_history_body(\@commitlist, 0, 99,
5845 $refs, $hash_base, $ftype, $next_link);
5846
5847 git_footer_html();
5848 }
5849
5850 sub git_search {
5851 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5852 if (!defined $searchtext) {
5853 die_error(400, "Text field is empty");
5854 }
5855 if (!defined $hash) {
5856 $hash = git_get_head_hash($project);
5857 }
5858 my %co = parse_commit($hash);
5859 if (!%co) {
5860 die_error(404, "Unknown commit object");
5861 }
5862 if (!defined $page) {
5863 $page = 0;
5864 }
5865
5866 $searchtype ||= 'commit';
5867 if ($searchtype eq 'pickaxe') {
5868 # pickaxe may take all resources of your box and run for several minutes
5869 # with every query - so decide by yourself how public you make this feature
5870 gitweb_check_feature('pickaxe')
5871 or die_error(403, "Pickaxe is disabled");
5872 }
5873 if ($searchtype eq 'grep') {
5874 gitweb_check_feature('grep')[0]
5875 or die_error(403, "Grep is disabled");
5876 }
5877
5878 git_header_html();
5879
5880 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5881 my $greptype;
5882 if ($searchtype eq 'commit') {
5883 $greptype = "--grep=";
5884 } elsif ($searchtype eq 'author') {
5885 $greptype = "--author=";
5886 } elsif ($searchtype eq 'committer') {
5887 $greptype = "--committer=";
5888 }
5889 $greptype .= $searchtext;
5890 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5891 $greptype, '--regexp-ignore-case',
5892 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5893
5894 my $paging_nav = '';
5895 if ($page > 0) {
5896 $paging_nav .=
5897 $cgi->a({-href => href(action=>"search", hash=>$hash,
5898 searchtext=>$searchtext,
5899 searchtype=>$searchtype)},
5900 "first");
5901 $paging_nav .= " &sdot; " .
5902 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5903 -accesskey => "p", -title => "Alt-p"}, "prev");
5904 } else {
5905 $paging_nav .= "first";
5906 $paging_nav .= " &sdot; prev";
5907 }
5908 my $next_link = '';
5909 if ($#commitlist >= 100) {
5910 $next_link =
5911 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5912 -accesskey => "n", -title => "Alt-n"}, "next");
5913 $paging_nav .= " &sdot; $next_link";
5914 } else {
5915 $paging_nav .= " &sdot; next";
5916 }
5917
5918 if ($#commitlist >= 100) {
5919 }
5920
5921 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5922 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5923 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5924 }
5925
5926 if ($searchtype eq 'pickaxe') {
5927 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5928 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5929
5930 print "<table class=\"pickaxe search\">\n";
5931 my $alternate = 1;
5932 local $/ = "\n";
5933 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5934 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5935 ($search_use_regexp ? '--pickaxe-regex' : ());
5936 undef %co;
5937 my @files;
5938 while (my $line = <$fd>) {
5939 chomp $line;
5940 next unless $line;
5941
5942 my %set = parse_difftree_raw_line($line);
5943 if (defined $set{'commit'}) {
5944 # finish previous commit
5945 if (%co) {
5946 print "</td>\n" .
5947 "<td class=\"link\">" .
5948 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5949 " | " .
5950 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5951 print "</td>\n" .
5952 "</tr>\n";
5953 }
5954
5955 if ($alternate) {
5956 print "<tr class=\"dark\">\n";
5957 } else {
5958 print "<tr class=\"light\">\n";
5959 }
5960 $alternate ^= 1;
5961 %co = parse_commit($set{'commit'});
5962 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5963 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5964 "<td><i>$author</i></td>\n" .
5965 "<td>" .
5966 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5967 -class => "list subject"},
5968 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5969 } elsif (defined $set{'to_id'}) {
5970 next if ($set{'to_id'} =~ m/^0{40}$/);
5971
5972 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5973 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5974 -class => "list"},
5975 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5976 "<br/>\n";
5977 }
5978 }
5979 close $fd;
5980
5981 # finish last commit (warning: repetition!)
5982 if (%co) {
5983 print "</td>\n" .
5984 "<td class=\"link\">" .
5985 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5986 " | " .
5987 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5988 print "</td>\n" .
5989 "</tr>\n";
5990 }
5991
5992 print "</table>\n";
5993 }
5994
5995 if ($searchtype eq 'grep') {
5996 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5997 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5998
5999 print "<table class=\"grep_search\">\n";
6000 my $alternate = 1;
6001 my $matches = 0;
6002 local $/ = "\n";
6003 open my $fd, "-|", git_cmd(), 'grep', '-n',
6004 $search_use_regexp ? ('-E', '-i') : '-F',
6005 $searchtext, $co{'tree'};
6006 my $lastfile = '';
6007 while (my $line = <$fd>) {
6008 chomp $line;
6009 my ($file, $lno, $ltext, $binary);
6010 last if ($matches++ > 1000);
6011 if ($line =~ /^Binary file (.+) matches$/) {
6012 $file = $1;
6013 $binary = 1;
6014 } else {
6015 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6016 }
6017 if ($file ne $lastfile) {
6018 $lastfile and print "</td></tr>\n";
6019 if ($alternate++) {
6020 print "<tr class=\"dark\">\n";
6021 } else {
6022 print "<tr class=\"light\">\n";
6023 }
6024 print "<td class=\"list\">".
6025 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6026 file_name=>"$file"),
6027 -class => "list"}, esc_path($file));
6028 print "</td><td>\n";
6029 $lastfile = $file;
6030 }
6031 if ($binary) {
6032 print "<div class=\"binary\">Binary file</div>\n";
6033 } else {
6034 $ltext = untabify($ltext);
6035 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6036 $ltext = esc_html($1, -nbsp=>1);
6037 $ltext .= '<span class="match">';
6038 $ltext .= esc_html($2, -nbsp=>1);
6039 $ltext .= '</span>';
6040 $ltext .= esc_html($3, -nbsp=>1);
6041 } else {
6042 $ltext = esc_html($ltext, -nbsp=>1);
6043 }
6044 print "<div class=\"pre\">" .
6045 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6046 file_name=>"$file").'#l'.$lno,
6047 -class => "linenr"}, sprintf('%4i', $lno))
6048 . ' ' . $ltext . "</div>\n";
6049 }
6050 }
6051 if ($lastfile) {
6052 print "</td></tr>\n";
6053 if ($matches > 1000) {
6054 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6055 }
6056 } else {
6057 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6058 }
6059 close $fd;
6060
6061 print "</table>\n";
6062 }
6063 git_footer_html();
6064 }
6065
6066 sub git_search_help {
6067 git_header_html();
6068 git_print_page_nav('','', $hash,$hash,$hash);
6069 print <<EOT;
6070 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6071 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6072 the pattern entered is recognized as the POSIX extended
6073 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6074 insensitive).</p>
6075 <dl>
6076 <dt><b>commit</b></dt>
6077 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6078 EOT
6079 my $have_grep = gitweb_check_feature('grep');
6080 if ($have_grep) {
6081 print <<EOT;
6082 <dt><b>grep</b></dt>
6083 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6084 a different one) are searched for the given pattern. On large trees, this search can take
6085 a while and put some strain on the server, so please use it with some consideration. Note that
6086 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6087 case-sensitive.</dd>
6088 EOT
6089 }
6090 print <<EOT;
6091 <dt><b>author</b></dt>
6092 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6093 <dt><b>committer</b></dt>
6094 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6095 EOT
6096 my $have_pickaxe = gitweb_check_feature('pickaxe');
6097 if ($have_pickaxe) {
6098 print <<EOT;
6099 <dt><b>pickaxe</b></dt>
6100 <dd>All commits that caused the string to appear or disappear from any file (changes that
6101 added, removed or "modified" the string) will be listed. This search can take a while and
6102 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6103 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6104 EOT
6105 }
6106 print "</dl>\n";
6107 git_footer_html();
6108 }
6109
6110 sub git_shortlog {
6111 my $head = git_get_head_hash($project);
6112 if (!defined $hash) {
6113 $hash = $head;
6114 }
6115 if (!defined $page) {
6116 $page = 0;
6117 }
6118 my $refs = git_get_references();
6119
6120 my $commit_hash = $hash;
6121 if (defined $hash_parent) {
6122 $commit_hash = "$hash_parent..$hash";
6123 }
6124 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
6125
6126 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
6127 my $next_link = '';
6128 if ($#commitlist >= 100) {
6129 $next_link =
6130 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6131 -accesskey => "n", -title => "Alt-n"}, "next");
6132 }
6133 my $patch_max = gitweb_check_feature('patches');
6134 if ($patch_max) {
6135 if ($patch_max < 0 || @commitlist <= $patch_max) {
6136 $paging_nav .= " &sdot; " .
6137 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6138 "patches");
6139 }
6140 }
6141
6142 git_header_html();
6143 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
6144 git_print_header_div('summary', $project);
6145
6146 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
6147
6148 git_footer_html();
6149 }
6150
6151 ## ......................................................................
6152 ## feeds (RSS, Atom; OPML)
6153
6154 sub git_feed {
6155 my $format = shift || 'atom';
6156 my $have_blame = gitweb_check_feature('blame');
6157
6158 # Atom: http://www.atomenabled.org/developers/syndication/
6159 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6160 if ($format ne 'rss' && $format ne 'atom') {
6161 die_error(400, "Unknown web feed format");
6162 }
6163
6164 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6165 my $head = $hash || 'HEAD';
6166 my @commitlist = parse_commits($head, 150, 0, $file_name);
6167
6168 my %latest_commit;
6169 my %latest_date;
6170 my $content_type = "application/$format+xml";
6171 if (defined $cgi->http('HTTP_ACCEPT') &&
6172 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6173 # browser (feed reader) prefers text/xml
6174 $content_type = 'text/xml';
6175 }
6176 if (defined($commitlist[0])) {
6177 %latest_commit = %{$commitlist[0]};
6178 my $latest_epoch = $latest_commit{'committer_epoch'};
6179 %latest_date = parse_date($latest_epoch);
6180 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6181 if (defined $if_modified) {
6182 my $since;
6183 if (eval { require HTTP::Date; 1; }) {
6184 $since = HTTP::Date::str2time($if_modified);
6185 } elsif (eval { require Time::ParseDate; 1; }) {
6186 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6187 }
6188 if (defined $since && $latest_epoch <= $since) {
6189 print $cgi->header(
6190 -type => $content_type,
6191 -charset => 'utf-8',
6192 -last_modified => $latest_date{'rfc2822'},
6193 -status => '304 Not Modified');
6194 return;
6195 }
6196 }
6197 print $cgi->header(
6198 -type => $content_type,
6199 -charset => 'utf-8',
6200 -last_modified => $latest_date{'rfc2822'});
6201 } else {
6202 print $cgi->header(
6203 -type => $content_type,
6204 -charset => 'utf-8');
6205 }
6206
6207 # Optimization: skip generating the body if client asks only
6208 # for Last-Modified date.
6209 return if ($cgi->request_method() eq 'HEAD');
6210
6211 # header variables
6212 my $title = "$site_name - $project/$action";
6213 my $feed_type = 'log';
6214 if (defined $hash) {
6215 $title .= " - '$hash'";
6216 $feed_type = 'branch log';
6217 if (defined $file_name) {
6218 $title .= " :: $file_name";
6219 $feed_type = 'history';
6220 }
6221 } elsif (defined $file_name) {
6222 $title .= " - $file_name";
6223 $feed_type = 'history';
6224 }
6225 $title .= " $feed_type";
6226 my $descr = git_get_project_description($project);
6227 if (defined $descr) {
6228 $descr = esc_html($descr);
6229 } else {
6230 $descr = "$project " .
6231 ($format eq 'rss' ? 'RSS' : 'Atom') .
6232 " feed";
6233 }
6234 my $owner = git_get_project_owner($project);
6235 $owner = esc_html($owner);
6236
6237 #header
6238 my $alt_url;
6239 if (defined $file_name) {
6240 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6241 } elsif (defined $hash) {
6242 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6243 } else {
6244 $alt_url = href(-full=>1, action=>"summary");
6245 }
6246 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6247 if ($format eq 'rss') {
6248 print <<XML;
6249 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6250 <channel>
6251 XML
6252 print "<title>$title</title>\n" .
6253 "<link>$alt_url</link>\n" .
6254 "<description>$descr</description>\n" .
6255 "<language>en</language>\n" .
6256 # project owner is responsible for 'editorial' content
6257 "<managingEditor>$owner</managingEditor>\n";
6258 if (defined $logo || defined $favicon) {
6259 # prefer the logo to the favicon, since RSS
6260 # doesn't allow both
6261 my $img = esc_url($logo || $favicon);
6262 print "<image>\n" .
6263 "<url>$img</url>\n" .
6264 "<title>$title</title>\n" .
6265 "<link>$alt_url</link>\n" .
6266 "</image>\n";
6267 }
6268 if (%latest_date) {
6269 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6270 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6271 }
6272 print "<generator>gitweb v.$version/$git_version</generator>\n";
6273 } elsif ($format eq 'atom') {
6274 print <<XML;
6275 <feed xmlns="http://www.w3.org/2005/Atom">
6276 XML
6277 print "<title>$title</title>\n" .
6278 "<subtitle>$descr</subtitle>\n" .
6279 '<link rel="alternate" type="text/html" href="' .
6280 $alt_url . '" />' . "\n" .
6281 '<link rel="self" type="' . $content_type . '" href="' .
6282 $cgi->self_url() . '" />' . "\n" .
6283 "<id>" . href(-full=>1) . "</id>\n" .
6284 # use project owner for feed author
6285 "<author><name>$owner</name></author>\n";
6286 if (defined $favicon) {
6287 print "<icon>" . esc_url($favicon) . "</icon>\n";
6288 }
6289 if (defined $logo_url) {
6290 # not twice as wide as tall: 72 x 27 pixels
6291 print "<logo>" . esc_url($logo) . "</logo>\n";
6292 }
6293 if (! %latest_date) {
6294 # dummy date to keep the feed valid until commits trickle in:
6295 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6296 } else {
6297 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6298 }
6299 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6300 }
6301
6302 # contents
6303 for (my $i = 0; $i <= $#commitlist; $i++) {
6304 my %co = %{$commitlist[$i]};
6305 my $commit = $co{'id'};
6306 # we read 150, we always show 30 and the ones more recent than 48 hours
6307 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6308 last;
6309 }
6310 my %cd = parse_date($co{'author_epoch'});
6311
6312 # get list of changed files
6313 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6314 $co{'parent'} || "--root",
6315 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6316 or next;
6317 my @difftree = map { chomp; $_ } <$fd>;
6318 close $fd
6319 or next;
6320
6321 # print element (entry, item)
6322 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6323 if ($format eq 'rss') {
6324 print "<item>\n" .
6325 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6326 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6327 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6328 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6329 "<link>$co_url</link>\n" .
6330 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6331 "<content:encoded>" .
6332 "<![CDATA[\n";
6333 } elsif ($format eq 'atom') {
6334 print "<entry>\n" .
6335 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6336 "<updated>$cd{'iso-8601'}</updated>\n" .
6337 "<author>\n" .
6338 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6339 if ($co{'author_email'}) {
6340 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6341 }
6342 print "</author>\n" .
6343 # use committer for contributor
6344 "<contributor>\n" .
6345 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6346 if ($co{'committer_email'}) {
6347 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6348 }
6349 print "</contributor>\n" .
6350 "<published>$cd{'iso-8601'}</published>\n" .
6351 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6352 "<id>$co_url</id>\n" .
6353 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6354 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6355 }
6356 my $comment = $co{'comment'};
6357 print "<pre>\n";
6358 foreach my $line (@$comment) {
6359 $line = esc_html($line);
6360 print "$line\n";
6361 }
6362 print "</pre><ul>\n";
6363 foreach my $difftree_line (@difftree) {
6364 my %difftree = parse_difftree_raw_line($difftree_line);
6365 next if !$difftree{'from_id'};
6366
6367 my $file = $difftree{'file'} || $difftree{'to_file'};
6368
6369 print "<li>" .
6370 "[" .
6371 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6372 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6373 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6374 file_name=>$file, file_parent=>$difftree{'from_file'}),
6375 -title => "diff"}, 'D');
6376 if ($have_blame) {
6377 print $cgi->a({-href => href(-full=>1, action=>"blame",
6378 file_name=>$file, hash_base=>$commit),
6379 -title => "blame"}, 'B');
6380 }
6381 # if this is not a feed of a file history
6382 if (!defined $file_name || $file_name ne $file) {
6383 print $cgi->a({-href => href(-full=>1, action=>"history",
6384 file_name=>$file, hash=>$commit),
6385 -title => "history"}, 'H');
6386 }
6387 $file = esc_path($file);
6388 print "] ".
6389 "$file</li>\n";
6390 }
6391 if ($format eq 'rss') {
6392 print "</ul>]]>\n" .
6393 "</content:encoded>\n" .
6394 "</item>\n";
6395 } elsif ($format eq 'atom') {
6396 print "</ul>\n</div>\n" .
6397 "</content>\n" .
6398 "</entry>\n";
6399 }
6400 }
6401
6402 # end of feed
6403 if ($format eq 'rss') {
6404 print "</channel>\n</rss>\n";
6405 } elsif ($format eq 'atom') {
6406 print "</feed>\n";
6407 }
6408 }
6409
6410 sub git_rss {
6411 git_feed('rss');
6412 }
6413
6414 sub git_atom {
6415 git_feed('atom');
6416 }
6417
6418 sub git_opml {
6419 my @list = git_get_projects_list();
6420
6421 print $cgi->header(
6422 -type => 'text/xml',
6423 -charset => 'utf-8',
6424 -content_disposition => 'inline; filename="opml.xml"');
6425
6426 print <<XML;
6427 <?xml version="1.0" encoding="utf-8"?>
6428 <opml version="1.0">
6429 <head>
6430 <title>$site_name OPML Export</title>
6431 </head>
6432 <body>
6433 <outline text="git RSS feeds">
6434 XML
6435
6436 foreach my $pr (@list) {
6437 my %proj = %$pr;
6438 my $head = git_get_head_hash($proj{'path'});
6439 if (!defined $head) {
6440 next;
6441 }
6442 $git_dir = "$projectroot/$proj{'path'}";
6443 my %co = parse_commit($head);
6444 if (!%co) {
6445 next;
6446 }
6447
6448 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6449 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6450 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6451 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6452 }
6453 print <<XML;
6454 </outline>
6455 </body>
6456 </opml>
6457 XML
6458 }
This page took 5.981012 seconds and 5 git commands to generate.