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