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