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