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