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