]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Paginate commit/author/committer search output
[Gitweb] / gitweb.perl
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
20
21 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
25
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
29
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
33
34 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
36
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
39
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++"
43 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
44
45 # filename of html text to include at top of each page
46 our $site_header = "++GITWEB_SITE_HEADER++";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
49 # filename of html text to include at bottom of each page
50 our $site_footer = "++GITWEB_SITE_FOOTER++";
51
52 # URI of stylesheets
53 our @stylesheets = ("++GITWEB_CSS++");
54 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
55 our $stylesheet = undef;
56
57 # URI of GIT logo (72x27 size)
58 our $logo = "++GITWEB_LOGO++";
59 # URI of GIT favicon, assumed to be image/png type
60 our $favicon = "++GITWEB_FAVICON++";
61
62 # URI and label (title) of GIT logo link
63 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
64 #our $logo_label = "git documentation";
65 our $logo_url = "http://git.or.cz/";
66 our $logo_label = "git homepage";
67
68 # source of projects list
69 our $projects_list = "++GITWEB_LIST++";
70
71 # show repository only if this file exists
72 # (only effective if this variable evaluates to true)
73 our $export_ok = "++GITWEB_EXPORT_OK++";
74
75 # only allow viewing of repositories also shown on the overview page
76 our $strict_export = "++GITWEB_STRICT_EXPORT++";
77
78 # list of git base URLs used for URL to where fetch project from,
79 # i.e. full URL is "$git_base_url/$project"
80 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
81
82 # default blob_plain mimetype and default charset for text/plain blob
83 our $default_blob_plain_mimetype = 'text/plain';
84 our $default_text_plain_charset = undef;
85
86 # file to use for guessing MIME types before trying /etc/mime.types
87 # (relative to the current git repository)
88 our $mimetypes_file = undef;
89
90 # You define site-wide feature defaults here; override them with
91 # $GITWEB_CONFIG as necessary.
92 our %feature = (
93 # feature => {
94 # 'sub' => feature-sub (subroutine),
95 # 'override' => allow-override (boolean),
96 # 'default' => [ default options...] (array reference)}
97 #
98 # if feature is overridable (it means that allow-override has true value,
99 # then feature-sub will be called with default options as parameters;
100 # return value of feature-sub indicates if to enable specified feature
101 #
102 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
103
104 # Enable the 'blame' blob view, showing the last commit that modified
105 # each line in the file. This can be very CPU-intensive.
106
107 # To enable system wide have in $GITWEB_CONFIG
108 # $feature{'blame'}{'default'} = [1];
109 # To have project specific config enable override in $GITWEB_CONFIG
110 # $feature{'blame'}{'override'} = 1;
111 # and in project config gitweb.blame = 0|1;
112 'blame' => {
113 'sub' => \&feature_blame,
114 'override' => 0,
115 'default' => [0]},
116
117 # Enable the 'snapshot' link, providing a compressed tarball of any
118 # tree. This can potentially generate high traffic if you have large
119 # project.
120
121 # To disable system wide have in $GITWEB_CONFIG
122 # $feature{'snapshot'}{'default'} = [undef];
123 # To have project specific config enable override in $GITWEB_CONFIG
124 # $feature{'snapshot'}{'override'} = 1;
125 # and in project config gitweb.snapshot = none|gzip|bzip2;
126 'snapshot' => {
127 'sub' => \&feature_snapshot,
128 'override' => 0,
129 # => [content-encoding, suffix, program]
130 'default' => ['x-gzip', 'gz', 'gzip']},
131
132 # Enable text search, which will list the commits which match author,
133 # committer or commit text to a given string. Enabled by default.
134 'search' => {
135 'override' => 0,
136 'default' => [1]},
137
138 # Enable the pickaxe search, which will list the commits that modified
139 # a given string in a file. This can be practical and quite faster
140 # alternative to 'blame', but still potentially CPU-intensive.
141
142 # To enable system wide have in $GITWEB_CONFIG
143 # $feature{'pickaxe'}{'default'} = [1];
144 # To have project specific config enable override in $GITWEB_CONFIG
145 # $feature{'pickaxe'}{'override'} = 1;
146 # and in project config gitweb.pickaxe = 0|1;
147 'pickaxe' => {
148 'sub' => \&feature_pickaxe,
149 'override' => 0,
150 'default' => [1]},
151
152 # Make gitweb use an alternative format of the URLs which can be
153 # more readable and natural-looking: project name is embedded
154 # directly in the path and the query string contains other
155 # auxiliary information. All gitweb installations recognize
156 # URL in either format; this configures in which formats gitweb
157 # generates links.
158
159 # To enable system wide have in $GITWEB_CONFIG
160 # $feature{'pathinfo'}{'default'} = [1];
161 # Project specific override is not supported.
162
163 # Note that you will need to change the default location of CSS,
164 # favicon, logo and possibly other files to an absolute URL. Also,
165 # if gitweb.cgi serves as your indexfile, you will need to force
166 # $my_uri to contain the script name in your $GITWEB_CONFIG.
167 'pathinfo' => {
168 'override' => 0,
169 'default' => [0]},
170
171 # Make gitweb consider projects in project root subdirectories
172 # to be forks of existing projects. Given project $projname.git,
173 # projects matching $projname/*.git will not be shown in the main
174 # projects list, instead a '+' mark will be added to $projname
175 # there and a 'forks' view will be enabled for the project, listing
176 # all the forks. This feature is supported only if project list
177 # is taken from a directory, not file.
178
179 # To enable system wide have in $GITWEB_CONFIG
180 # $feature{'forks'}{'default'} = [1];
181 # Project specific override is not supported.
182 'forks' => {
183 'override' => 0,
184 'default' => [0]},
185 );
186
187 sub gitweb_check_feature {
188 my ($name) = @_;
189 return unless exists $feature{$name};
190 my ($sub, $override, @defaults) = (
191 $feature{$name}{'sub'},
192 $feature{$name}{'override'},
193 @{$feature{$name}{'default'}});
194 if (!$override) { return @defaults; }
195 if (!defined $sub) {
196 warn "feature $name is not overrideable";
197 return @defaults;
198 }
199 return $sub->(@defaults);
200 }
201
202 sub feature_blame {
203 my ($val) = git_get_project_config('blame', '--bool');
204
205 if ($val eq 'true') {
206 return 1;
207 } elsif ($val eq 'false') {
208 return 0;
209 }
210
211 return $_[0];
212 }
213
214 sub feature_snapshot {
215 my ($ctype, $suffix, $command) = @_;
216
217 my ($val) = git_get_project_config('snapshot');
218
219 if ($val eq 'gzip') {
220 return ('x-gzip', 'gz', 'gzip');
221 } elsif ($val eq 'bzip2') {
222 return ('x-bzip2', 'bz2', 'bzip2');
223 } elsif ($val eq 'none') {
224 return ();
225 }
226
227 return ($ctype, $suffix, $command);
228 }
229
230 sub gitweb_have_snapshot {
231 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
232 my $have_snapshot = (defined $ctype && defined $suffix);
233
234 return $have_snapshot;
235 }
236
237 sub feature_pickaxe {
238 my ($val) = git_get_project_config('pickaxe', '--bool');
239
240 if ($val eq 'true') {
241 return (1);
242 } elsif ($val eq 'false') {
243 return (0);
244 }
245
246 return ($_[0]);
247 }
248
249 # checking HEAD file with -e is fragile if the repository was
250 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
251 # and then pruned.
252 sub check_head_link {
253 my ($dir) = @_;
254 my $headfile = "$dir/HEAD";
255 return ((-e $headfile) ||
256 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
257 }
258
259 sub check_export_ok {
260 my ($dir) = @_;
261 return (check_head_link($dir) &&
262 (!$export_ok || -e "$dir/$export_ok"));
263 }
264
265 # rename detection options for git-diff and git-diff-tree
266 # - default is '-M', with the cost proportional to
267 # (number of removed files) * (number of new files).
268 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
269 # (number of changed files + number of removed files) * (number of new files)
270 # - even more costly is '-C', '--find-copies-harder' with cost
271 # (number of files in the original tree) * (number of new files)
272 # - one might want to include '-B' option, e.g. '-B', '-M'
273 our @diff_opts = ('-M'); # taken from git_commit
274
275 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
276 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
277
278 # version of the core git binary
279 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
280
281 $projects_list ||= $projectroot;
282
283 # ======================================================================
284 # input validation and dispatch
285 our $action = $cgi->param('a');
286 if (defined $action) {
287 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
288 die_error(undef, "Invalid action parameter");
289 }
290 }
291
292 # parameters which are pathnames
293 our $project = $cgi->param('p');
294 if (defined $project) {
295 if (!validate_pathname($project) ||
296 !(-d "$projectroot/$project") ||
297 !check_head_link("$projectroot/$project") ||
298 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
299 ($strict_export && !project_in_list($project))) {
300 undef $project;
301 die_error(undef, "No such project");
302 }
303 }
304
305 our $file_name = $cgi->param('f');
306 if (defined $file_name) {
307 if (!validate_pathname($file_name)) {
308 die_error(undef, "Invalid file parameter");
309 }
310 }
311
312 our $file_parent = $cgi->param('fp');
313 if (defined $file_parent) {
314 if (!validate_pathname($file_parent)) {
315 die_error(undef, "Invalid file parent parameter");
316 }
317 }
318
319 # parameters which are refnames
320 our $hash = $cgi->param('h');
321 if (defined $hash) {
322 if (!validate_refname($hash)) {
323 die_error(undef, "Invalid hash parameter");
324 }
325 }
326
327 our $hash_parent = $cgi->param('hp');
328 if (defined $hash_parent) {
329 if (!validate_refname($hash_parent)) {
330 die_error(undef, "Invalid hash parent parameter");
331 }
332 }
333
334 our $hash_base = $cgi->param('hb');
335 if (defined $hash_base) {
336 if (!validate_refname($hash_base)) {
337 die_error(undef, "Invalid hash base parameter");
338 }
339 }
340
341 our $hash_parent_base = $cgi->param('hpb');
342 if (defined $hash_parent_base) {
343 if (!validate_refname($hash_parent_base)) {
344 die_error(undef, "Invalid hash parent base parameter");
345 }
346 }
347
348 # other parameters
349 our $page = $cgi->param('pg');
350 if (defined $page) {
351 if ($page =~ m/[^0-9]/) {
352 die_error(undef, "Invalid page parameter");
353 }
354 }
355
356 our $searchtext = $cgi->param('s');
357 if (defined $searchtext) {
358 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
359 die_error(undef, "Invalid search parameter");
360 }
361 if (length($searchtext) < 2) {
362 die_error(undef, "At least two characters are required for search parameter");
363 }
364 $searchtext = quotemeta $searchtext;
365 }
366
367 our $searchtype = $cgi->param('st');
368 if (defined $searchtype) {
369 if ($searchtype =~ m/[^a-z]/) {
370 die_error(undef, "Invalid searchtype parameter");
371 }
372 }
373
374 # now read PATH_INFO and use it as alternative to parameters
375 sub evaluate_path_info {
376 return if defined $project;
377 my $path_info = $ENV{"PATH_INFO"};
378 return if !$path_info;
379 $path_info =~ s,^/+,,;
380 return if !$path_info;
381 # find which part of PATH_INFO is project
382 $project = $path_info;
383 $project =~ s,/+$,,;
384 while ($project && !check_head_link("$projectroot/$project")) {
385 $project =~ s,/*[^/]*$,,;
386 }
387 # validate project
388 $project = validate_pathname($project);
389 if (!$project ||
390 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
391 ($strict_export && !project_in_list($project))) {
392 undef $project;
393 return;
394 }
395 # do not change any parameters if an action is given using the query string
396 return if $action;
397 $path_info =~ s,^$project/*,,;
398 my ($refname, $pathname) = split(/:/, $path_info, 2);
399 if (defined $pathname) {
400 # we got "project.git/branch:filename" or "project.git/branch:dir/"
401 # we could use git_get_type(branch:pathname), but it needs $git_dir
402 $pathname =~ s,^/+,,;
403 if (!$pathname || substr($pathname, -1) eq "/") {
404 $action ||= "tree";
405 $pathname =~ s,/$,,;
406 } else {
407 $action ||= "blob_plain";
408 }
409 $hash_base ||= validate_refname($refname);
410 $file_name ||= validate_pathname($pathname);
411 } elsif (defined $refname) {
412 # we got "project.git/branch"
413 $action ||= "shortlog";
414 $hash ||= validate_refname($refname);
415 }
416 }
417 evaluate_path_info();
418
419 # path to the current git repository
420 our $git_dir;
421 $git_dir = "$projectroot/$project" if $project;
422
423 # dispatch
424 my %actions = (
425 "blame" => \&git_blame2,
426 "blobdiff" => \&git_blobdiff,
427 "blobdiff_plain" => \&git_blobdiff_plain,
428 "blob" => \&git_blob,
429 "blob_plain" => \&git_blob_plain,
430 "commitdiff" => \&git_commitdiff,
431 "commitdiff_plain" => \&git_commitdiff_plain,
432 "commit" => \&git_commit,
433 "forks" => \&git_forks,
434 "heads" => \&git_heads,
435 "history" => \&git_history,
436 "log" => \&git_log,
437 "rss" => \&git_rss,
438 "atom" => \&git_atom,
439 "search" => \&git_search,
440 "search_help" => \&git_search_help,
441 "shortlog" => \&git_shortlog,
442 "summary" => \&git_summary,
443 "tag" => \&git_tag,
444 "tags" => \&git_tags,
445 "tree" => \&git_tree,
446 "snapshot" => \&git_snapshot,
447 "object" => \&git_object,
448 # those below don't need $project
449 "opml" => \&git_opml,
450 "project_list" => \&git_project_list,
451 "project_index" => \&git_project_index,
452 );
453
454 if (defined $project) {
455 $action ||= 'summary';
456 } else {
457 $action ||= 'project_list';
458 }
459 if (!defined($actions{$action})) {
460 die_error(undef, "Unknown action");
461 }
462 if ($action !~ m/^(opml|project_list|project_index)$/ &&
463 !$project) {
464 die_error(undef, "Project needed");
465 }
466 $actions{$action}->();
467 exit;
468
469 ## ======================================================================
470 ## action links
471
472 sub href(%) {
473 my %params = @_;
474 # default is to use -absolute url() i.e. $my_uri
475 my $href = $params{-full} ? $my_url : $my_uri;
476
477 # XXX: Warning: If you touch this, check the search form for updating,
478 # too.
479
480 my @mapping = (
481 project => "p",
482 action => "a",
483 file_name => "f",
484 file_parent => "fp",
485 hash => "h",
486 hash_parent => "hp",
487 hash_base => "hb",
488 hash_parent_base => "hpb",
489 page => "pg",
490 order => "o",
491 searchtext => "s",
492 searchtype => "st",
493 );
494 my %mapping = @mapping;
495
496 $params{'project'} = $project unless exists $params{'project'};
497
498 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
499 if ($use_pathinfo) {
500 # use PATH_INFO for project name
501 $href .= "/$params{'project'}" if defined $params{'project'};
502 delete $params{'project'};
503
504 # Summary just uses the project path URL
505 if (defined $params{'action'} && $params{'action'} eq 'summary') {
506 delete $params{'action'};
507 }
508 }
509
510 # now encode the parameters explicitly
511 my @result = ();
512 for (my $i = 0; $i < @mapping; $i += 2) {
513 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
514 if (defined $params{$name}) {
515 push @result, $symbol . "=" . esc_param($params{$name});
516 }
517 }
518 $href .= "?" . join(';', @result) if scalar @result;
519
520 return $href;
521 }
522
523
524 ## ======================================================================
525 ## validation, quoting/unquoting and escaping
526
527 sub validate_pathname {
528 my $input = shift || return undef;
529
530 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
531 # at the beginning, at the end, and between slashes.
532 # also this catches doubled slashes
533 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
534 return undef;
535 }
536 # no null characters
537 if ($input =~ m!\0!) {
538 return undef;
539 }
540 return $input;
541 }
542
543 sub validate_refname {
544 my $input = shift || return undef;
545
546 # textual hashes are O.K.
547 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
548 return $input;
549 }
550 # it must be correct pathname
551 $input = validate_pathname($input)
552 or return undef;
553 # restrictions on ref name according to git-check-ref-format
554 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
555 return undef;
556 }
557 return $input;
558 }
559
560 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
561 sub to_utf8 {
562 my $str = shift;
563 return decode("utf8", $str, Encode::FB_DEFAULT);
564 }
565
566 # quote unsafe chars, but keep the slash, even when it's not
567 # correct, but quoted slashes look too horrible in bookmarks
568 sub esc_param {
569 my $str = shift;
570 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
571 $str =~ s/\+/%2B/g;
572 $str =~ s/ /\+/g;
573 return $str;
574 }
575
576 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
577 sub esc_url {
578 my $str = shift;
579 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
580 $str =~ s/\+/%2B/g;
581 $str =~ s/ /\+/g;
582 return $str;
583 }
584
585 # replace invalid utf8 character with SUBSTITUTION sequence
586 sub esc_html ($;%) {
587 my $str = shift;
588 my %opts = @_;
589
590 $str = to_utf8($str);
591 $str = escapeHTML($str);
592 if ($opts{'-nbsp'}) {
593 $str =~ s/ /&nbsp;/g;
594 }
595 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
596 return $str;
597 }
598
599 # quote control characters and escape filename to HTML
600 sub esc_path {
601 my $str = shift;
602 my %opts = @_;
603
604 $str = to_utf8($str);
605 $str = escapeHTML($str);
606 if ($opts{'-nbsp'}) {
607 $str =~ s/ /&nbsp;/g;
608 }
609 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
610 return $str;
611 }
612
613 # Make control characters "printable", using character escape codes (CEC)
614 sub quot_cec {
615 my $cntrl = shift;
616 my %es = ( # character escape codes, aka escape sequences
617 "\t" => '\t', # tab (HT)
618 "\n" => '\n', # line feed (LF)
619 "\r" => '\r', # carrige return (CR)
620 "\f" => '\f', # form feed (FF)
621 "\b" => '\b', # backspace (BS)
622 "\a" => '\a', # alarm (bell) (BEL)
623 "\e" => '\e', # escape (ESC)
624 "\013" => '\v', # vertical tab (VT)
625 "\000" => '\0', # nul character (NUL)
626 );
627 my $chr = ( (exists $es{$cntrl})
628 ? $es{$cntrl}
629 : sprintf('\%03o', ord($cntrl)) );
630 return "<span class=\"cntrl\">$chr</span>";
631 }
632
633 # Alternatively use unicode control pictures codepoints,
634 # Unicode "printable representation" (PR)
635 sub quot_upr {
636 my $cntrl = shift;
637 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
638 return "<span class=\"cntrl\">$chr</span>";
639 }
640
641 # git may return quoted and escaped filenames
642 sub unquote {
643 my $str = shift;
644
645 sub unq {
646 my $seq = shift;
647 my %es = ( # character escape codes, aka escape sequences
648 't' => "\t", # tab (HT, TAB)
649 'n' => "\n", # newline (NL)
650 'r' => "\r", # return (CR)
651 'f' => "\f", # form feed (FF)
652 'b' => "\b", # backspace (BS)
653 'a' => "\a", # alarm (bell) (BEL)
654 'e' => "\e", # escape (ESC)
655 'v' => "\013", # vertical tab (VT)
656 );
657
658 if ($seq =~ m/^[0-7]{1,3}$/) {
659 # octal char sequence
660 return chr(oct($seq));
661 } elsif (exists $es{$seq}) {
662 # C escape sequence, aka character escape code
663 return $es{$seq}
664 }
665 # quoted ordinary character
666 return $seq;
667 }
668
669 if ($str =~ m/^"(.*)"$/) {
670 # needs unquoting
671 $str = $1;
672 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
673 }
674 return $str;
675 }
676
677 # escape tabs (convert tabs to spaces)
678 sub untabify {
679 my $line = shift;
680
681 while ((my $pos = index($line, "\t")) != -1) {
682 if (my $count = (8 - ($pos % 8))) {
683 my $spaces = ' ' x $count;
684 $line =~ s/\t/$spaces/;
685 }
686 }
687
688 return $line;
689 }
690
691 sub project_in_list {
692 my $project = shift;
693 my @list = git_get_projects_list();
694 return @list && scalar(grep { $_->{'path'} eq $project } @list);
695 }
696
697 ## ----------------------------------------------------------------------
698 ## HTML aware string manipulation
699
700 sub chop_str {
701 my $str = shift;
702 my $len = shift;
703 my $add_len = shift || 10;
704
705 # allow only $len chars, but don't cut a word if it would fit in $add_len
706 # if it doesn't fit, cut it if it's still longer than the dots we would add
707 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
708 my $body = $1;
709 my $tail = $2;
710 if (length($tail) > 4) {
711 $tail = " ...";
712 $body =~ s/&[^;]*$//; # remove chopped character entities
713 }
714 return "$body$tail";
715 }
716
717 ## ----------------------------------------------------------------------
718 ## functions returning short strings
719
720 # CSS class for given age value (in seconds)
721 sub age_class {
722 my $age = shift;
723
724 if ($age < 60*60*2) {
725 return "age0";
726 } elsif ($age < 60*60*24*2) {
727 return "age1";
728 } else {
729 return "age2";
730 }
731 }
732
733 # convert age in seconds to "nn units ago" string
734 sub age_string {
735 my $age = shift;
736 my $age_str;
737
738 if ($age > 60*60*24*365*2) {
739 $age_str = (int $age/60/60/24/365);
740 $age_str .= " years ago";
741 } elsif ($age > 60*60*24*(365/12)*2) {
742 $age_str = int $age/60/60/24/(365/12);
743 $age_str .= " months ago";
744 } elsif ($age > 60*60*24*7*2) {
745 $age_str = int $age/60/60/24/7;
746 $age_str .= " weeks ago";
747 } elsif ($age > 60*60*24*2) {
748 $age_str = int $age/60/60/24;
749 $age_str .= " days ago";
750 } elsif ($age > 60*60*2) {
751 $age_str = int $age/60/60;
752 $age_str .= " hours ago";
753 } elsif ($age > 60*2) {
754 $age_str = int $age/60;
755 $age_str .= " min ago";
756 } elsif ($age > 2) {
757 $age_str = int $age;
758 $age_str .= " sec ago";
759 } else {
760 $age_str .= " right now";
761 }
762 return $age_str;
763 }
764
765 # convert file mode in octal to symbolic file mode string
766 sub mode_str {
767 my $mode = oct shift;
768
769 if (S_ISDIR($mode & S_IFMT)) {
770 return 'drwxr-xr-x';
771 } elsif (S_ISLNK($mode)) {
772 return 'lrwxrwxrwx';
773 } elsif (S_ISREG($mode)) {
774 # git cares only about the executable bit
775 if ($mode & S_IXUSR) {
776 return '-rwxr-xr-x';
777 } else {
778 return '-rw-r--r--';
779 };
780 } else {
781 return '----------';
782 }
783 }
784
785 # convert file mode in octal to file type string
786 sub file_type {
787 my $mode = shift;
788
789 if ($mode !~ m/^[0-7]+$/) {
790 return $mode;
791 } else {
792 $mode = oct $mode;
793 }
794
795 if (S_ISDIR($mode & S_IFMT)) {
796 return "directory";
797 } elsif (S_ISLNK($mode)) {
798 return "symlink";
799 } elsif (S_ISREG($mode)) {
800 return "file";
801 } else {
802 return "unknown";
803 }
804 }
805
806 # convert file mode in octal to file type description string
807 sub file_type_long {
808 my $mode = shift;
809
810 if ($mode !~ m/^[0-7]+$/) {
811 return $mode;
812 } else {
813 $mode = oct $mode;
814 }
815
816 if (S_ISDIR($mode & S_IFMT)) {
817 return "directory";
818 } elsif (S_ISLNK($mode)) {
819 return "symlink";
820 } elsif (S_ISREG($mode)) {
821 if ($mode & S_IXUSR) {
822 return "executable";
823 } else {
824 return "file";
825 };
826 } else {
827 return "unknown";
828 }
829 }
830
831
832 ## ----------------------------------------------------------------------
833 ## functions returning short HTML fragments, or transforming HTML fragments
834 ## which don't beling to other sections
835
836 # format line of commit message.
837 sub format_log_line_html {
838 my $line = shift;
839
840 $line = esc_html($line, -nbsp=>1);
841 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
842 my $hash_text = $1;
843 my $link =
844 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
845 -class => "text"}, $hash_text);
846 $line =~ s/$hash_text/$link/;
847 }
848 return $line;
849 }
850
851 # format marker of refs pointing to given object
852 sub format_ref_marker {
853 my ($refs, $id) = @_;
854 my $markers = '';
855
856 if (defined $refs->{$id}) {
857 foreach my $ref (@{$refs->{$id}}) {
858 my ($type, $name) = qw();
859 # e.g. tags/v2.6.11 or heads/next
860 if ($ref =~ m!^(.*?)s?/(.*)$!) {
861 $type = $1;
862 $name = $2;
863 } else {
864 $type = "ref";
865 $name = $ref;
866 }
867
868 $markers .= " <span class=\"$type\" title=\"$ref\">" .
869 esc_html($name) . "</span>";
870 }
871 }
872
873 if ($markers) {
874 return ' <span class="refs">'. $markers . '</span>';
875 } else {
876 return "";
877 }
878 }
879
880 # format, perhaps shortened and with markers, title line
881 sub format_subject_html {
882 my ($long, $short, $href, $extra) = @_;
883 $extra = '' unless defined($extra);
884
885 if (length($short) < length($long)) {
886 return $cgi->a({-href => $href, -class => "list subject",
887 -title => to_utf8($long)},
888 esc_html($short) . $extra);
889 } else {
890 return $cgi->a({-href => $href, -class => "list subject"},
891 esc_html($long) . $extra);
892 }
893 }
894
895 # format patch (diff) line (rather not to be used for diff headers)
896 sub format_diff_line {
897 my $line = shift;
898 my ($from, $to) = @_;
899 my $char = substr($line, 0, 1);
900 my $diff_class = "";
901
902 chomp $line;
903
904 if ($char eq '+') {
905 $diff_class = " add";
906 } elsif ($char eq "-") {
907 $diff_class = " rem";
908 } elsif ($char eq "@") {
909 $diff_class = " chunk_header";
910 } elsif ($char eq "\\") {
911 $diff_class = " incomplete";
912 }
913 $line = untabify($line);
914 if ($from && $to && $line =~ m/^\@{2} /) {
915 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
916 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
917
918 $from_lines = 0 unless defined $from_lines;
919 $to_lines = 0 unless defined $to_lines;
920
921 if ($from->{'href'}) {
922 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
923 -class=>"list"}, $from_text);
924 }
925 if ($to->{'href'}) {
926 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
927 -class=>"list"}, $to_text);
928 }
929 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
930 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
931 return "<div class=\"diff$diff_class\">$line</div>\n";
932 }
933 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
934 }
935
936 ## ----------------------------------------------------------------------
937 ## git utility subroutines, invoking git commands
938
939 # returns path to the core git executable and the --git-dir parameter as list
940 sub git_cmd {
941 return $GIT, '--git-dir='.$git_dir;
942 }
943
944 # returns path to the core git executable and the --git-dir parameter as string
945 sub git_cmd_str {
946 return join(' ', git_cmd());
947 }
948
949 # get HEAD ref of given project as hash
950 sub git_get_head_hash {
951 my $project = shift;
952 my $o_git_dir = $git_dir;
953 my $retval = undef;
954 $git_dir = "$projectroot/$project";
955 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
956 my $head = <$fd>;
957 close $fd;
958 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
959 $retval = $1;
960 }
961 }
962 if (defined $o_git_dir) {
963 $git_dir = $o_git_dir;
964 }
965 return $retval;
966 }
967
968 # get type of given object
969 sub git_get_type {
970 my $hash = shift;
971
972 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
973 my $type = <$fd>;
974 close $fd or return;
975 chomp $type;
976 return $type;
977 }
978
979 sub git_get_project_config {
980 my ($key, $type) = @_;
981
982 return unless ($key);
983 $key =~ s/^gitweb\.//;
984 return if ($key =~ m/\W/);
985
986 my @x = (git_cmd(), 'repo-config');
987 if (defined $type) { push @x, $type; }
988 push @x, "--get";
989 push @x, "gitweb.$key";
990 my $val = qx(@x);
991 chomp $val;
992 return ($val);
993 }
994
995 # get hash of given path at given ref
996 sub git_get_hash_by_path {
997 my $base = shift;
998 my $path = shift || return undef;
999 my $type = shift;
1000
1001 $path =~ s,/+$,,;
1002
1003 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1004 or die_error(undef, "Open git-ls-tree failed");
1005 my $line = <$fd>;
1006 close $fd or return undef;
1007
1008 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1009 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1010 if (defined $type && $type ne $2) {
1011 # type doesn't match
1012 return undef;
1013 }
1014 return $3;
1015 }
1016
1017 ## ......................................................................
1018 ## git utility functions, directly accessing git repository
1019
1020 sub git_get_project_description {
1021 my $path = shift;
1022
1023 open my $fd, "$projectroot/$path/description" or return undef;
1024 my $descr = <$fd>;
1025 close $fd;
1026 chomp $descr;
1027 return $descr;
1028 }
1029
1030 sub git_get_project_url_list {
1031 my $path = shift;
1032
1033 open my $fd, "$projectroot/$path/cloneurl" or return;
1034 my @git_project_url_list = map { chomp; $_ } <$fd>;
1035 close $fd;
1036
1037 return wantarray ? @git_project_url_list : \@git_project_url_list;
1038 }
1039
1040 sub git_get_projects_list {
1041 my ($filter) = @_;
1042 my @list;
1043
1044 $filter ||= '';
1045 $filter =~ s/\.git$//;
1046
1047 if (-d $projects_list) {
1048 # search in directory
1049 my $dir = $projects_list . ($filter ? "/$filter" : '');
1050 # remove the trailing "/"
1051 $dir =~ s!/+$!!;
1052 my $pfxlen = length("$dir");
1053
1054 my ($check_forks) = gitweb_check_feature('forks');
1055
1056 File::Find::find({
1057 follow_fast => 1, # follow symbolic links
1058 dangling_symlinks => 0, # ignore dangling symlinks, silently
1059 wanted => sub {
1060 # skip project-list toplevel, if we get it.
1061 return if (m!^[/.]$!);
1062 # only directories can be git repositories
1063 return unless (-d $_);
1064
1065 my $subdir = substr($File::Find::name, $pfxlen + 1);
1066 # we check related file in $projectroot
1067 if ($check_forks and $subdir =~ m#/.#) {
1068 $File::Find::prune = 1;
1069 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1070 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1071 $File::Find::prune = 1;
1072 }
1073 },
1074 }, "$dir");
1075
1076 } elsif (-f $projects_list) {
1077 # read from file(url-encoded):
1078 # 'git%2Fgit.git Linus+Torvalds'
1079 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1080 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1081 open my ($fd), $projects_list or return;
1082 while (my $line = <$fd>) {
1083 chomp $line;
1084 my ($path, $owner) = split ' ', $line;
1085 $path = unescape($path);
1086 $owner = unescape($owner);
1087 if (!defined $path) {
1088 next;
1089 }
1090 if ($filter ne '') {
1091 # looking for forks;
1092 my $pfx = substr($path, 0, length($filter));
1093 if ($pfx ne $filter) {
1094 next;
1095 }
1096 my $sfx = substr($path, length($filter));
1097 if ($sfx !~ /^\/.*\.git$/) {
1098 next;
1099 }
1100 }
1101 if (check_export_ok("$projectroot/$path")) {
1102 my $pr = {
1103 path => $path,
1104 owner => to_utf8($owner),
1105 };
1106 push @list, $pr
1107 }
1108 }
1109 close $fd;
1110 }
1111 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
1112 return @list;
1113 }
1114
1115 sub git_get_project_owner {
1116 my $project = shift;
1117 my $owner;
1118
1119 return undef unless $project;
1120
1121 # read from file (url-encoded):
1122 # 'git%2Fgit.git Linus+Torvalds'
1123 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1124 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1125 if (-f $projects_list) {
1126 open (my $fd , $projects_list);
1127 while (my $line = <$fd>) {
1128 chomp $line;
1129 my ($pr, $ow) = split ' ', $line;
1130 $pr = unescape($pr);
1131 $ow = unescape($ow);
1132 if ($pr eq $project) {
1133 $owner = to_utf8($ow);
1134 last;
1135 }
1136 }
1137 close $fd;
1138 }
1139 if (!defined $owner) {
1140 $owner = get_file_owner("$projectroot/$project");
1141 }
1142
1143 return $owner;
1144 }
1145
1146 sub git_get_last_activity {
1147 my ($path) = @_;
1148 my $fd;
1149
1150 $git_dir = "$projectroot/$path";
1151 open($fd, "-|", git_cmd(), 'for-each-ref',
1152 '--format=%(committer)',
1153 '--sort=-committerdate',
1154 '--count=1',
1155 'refs/heads') or return;
1156 my $most_recent = <$fd>;
1157 close $fd or return;
1158 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1159 my $timestamp = $1;
1160 my $age = time - $timestamp;
1161 return ($age, age_string($age));
1162 }
1163 }
1164
1165 sub git_get_references {
1166 my $type = shift || "";
1167 my %refs;
1168 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1169 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1170 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1171 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1172 or return;
1173
1174 while (my $line = <$fd>) {
1175 chomp $line;
1176 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1177 if (defined $refs{$1}) {
1178 push @{$refs{$1}}, $2;
1179 } else {
1180 $refs{$1} = [ $2 ];
1181 }
1182 }
1183 }
1184 close $fd or return;
1185 return \%refs;
1186 }
1187
1188 sub git_get_rev_name_tags {
1189 my $hash = shift || return undef;
1190
1191 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1192 or return;
1193 my $name_rev = <$fd>;
1194 close $fd;
1195
1196 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1197 return $1;
1198 } else {
1199 # catches also '$hash undefined' output
1200 return undef;
1201 }
1202 }
1203
1204 ## ----------------------------------------------------------------------
1205 ## parse to hash functions
1206
1207 sub parse_date {
1208 my $epoch = shift;
1209 my $tz = shift || "-0000";
1210
1211 my %date;
1212 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1213 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1214 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1215 $date{'hour'} = $hour;
1216 $date{'minute'} = $min;
1217 $date{'mday'} = $mday;
1218 $date{'day'} = $days[$wday];
1219 $date{'month'} = $months[$mon];
1220 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1221 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1222 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1223 $mday, $months[$mon], $hour ,$min;
1224 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1225 1900+$year, $mon, $mday, $hour ,$min, $sec;
1226
1227 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1228 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1229 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1230 $date{'hour_local'} = $hour;
1231 $date{'minute_local'} = $min;
1232 $date{'tz_local'} = $tz;
1233 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1234 1900+$year, $mon+1, $mday,
1235 $hour, $min, $sec, $tz);
1236 return %date;
1237 }
1238
1239 sub parse_tag {
1240 my $tag_id = shift;
1241 my %tag;
1242 my @comment;
1243
1244 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1245 $tag{'id'} = $tag_id;
1246 while (my $line = <$fd>) {
1247 chomp $line;
1248 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1249 $tag{'object'} = $1;
1250 } elsif ($line =~ m/^type (.+)$/) {
1251 $tag{'type'} = $1;
1252 } elsif ($line =~ m/^tag (.+)$/) {
1253 $tag{'name'} = $1;
1254 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1255 $tag{'author'} = $1;
1256 $tag{'epoch'} = $2;
1257 $tag{'tz'} = $3;
1258 } elsif ($line =~ m/--BEGIN/) {
1259 push @comment, $line;
1260 last;
1261 } elsif ($line eq "") {
1262 last;
1263 }
1264 }
1265 push @comment, <$fd>;
1266 $tag{'comment'} = \@comment;
1267 close $fd or return;
1268 if (!defined $tag{'name'}) {
1269 return
1270 };
1271 return %tag
1272 }
1273
1274 sub parse_commit {
1275 my $commit_id = shift;
1276 my $commit_text = shift;
1277
1278 my @commit_lines;
1279 my %co;
1280
1281 if (defined $commit_text) {
1282 @commit_lines = @$commit_text;
1283 } else {
1284 local $/ = "\0";
1285 open my $fd, "-|", git_cmd(), "rev-list",
1286 "--header", "--parents", "--max-count=1",
1287 $commit_id, "--"
1288 or return;
1289 @commit_lines = split '\n', <$fd>;
1290 close $fd or return;
1291 pop @commit_lines;
1292 }
1293 my $header = shift @commit_lines;
1294 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1295 return;
1296 }
1297 ($co{'id'}, my @parents) = split ' ', $header;
1298 $co{'parents'} = \@parents;
1299 $co{'parent'} = $parents[0];
1300 while (my $line = shift @commit_lines) {
1301 last if $line eq "\n";
1302 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1303 $co{'tree'} = $1;
1304 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1305 $co{'author'} = $1;
1306 $co{'author_epoch'} = $2;
1307 $co{'author_tz'} = $3;
1308 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1309 $co{'author_name'} = $1;
1310 $co{'author_email'} = $2;
1311 } else {
1312 $co{'author_name'} = $co{'author'};
1313 }
1314 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1315 $co{'committer'} = $1;
1316 $co{'committer_epoch'} = $2;
1317 $co{'committer_tz'} = $3;
1318 $co{'committer_name'} = $co{'committer'};
1319 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1320 $co{'committer_name'} = $1;
1321 $co{'committer_email'} = $2;
1322 } else {
1323 $co{'committer_name'} = $co{'committer'};
1324 }
1325 }
1326 }
1327 if (!defined $co{'tree'}) {
1328 return;
1329 };
1330
1331 foreach my $title (@commit_lines) {
1332 $title =~ s/^ //;
1333 if ($title ne "") {
1334 $co{'title'} = chop_str($title, 80, 5);
1335 # remove leading stuff of merges to make the interesting part visible
1336 if (length($title) > 50) {
1337 $title =~ s/^Automatic //;
1338 $title =~ s/^merge (of|with) /Merge ... /i;
1339 if (length($title) > 50) {
1340 $title =~ s/(http|rsync):\/\///;
1341 }
1342 if (length($title) > 50) {
1343 $title =~ s/(master|www|rsync)\.//;
1344 }
1345 if (length($title) > 50) {
1346 $title =~ s/kernel.org:?//;
1347 }
1348 if (length($title) > 50) {
1349 $title =~ s/\/pub\/scm//;
1350 }
1351 }
1352 $co{'title_short'} = chop_str($title, 50, 5);
1353 last;
1354 }
1355 }
1356 if ($co{'title'} eq "") {
1357 $co{'title'} = $co{'title_short'} = '(no commit message)';
1358 }
1359 # remove added spaces
1360 foreach my $line (@commit_lines) {
1361 $line =~ s/^ //;
1362 }
1363 $co{'comment'} = \@commit_lines;
1364
1365 my $age = time - $co{'committer_epoch'};
1366 $co{'age'} = $age;
1367 $co{'age_string'} = age_string($age);
1368 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1369 if ($age > 60*60*24*7*2) {
1370 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1371 $co{'age_string_age'} = $co{'age_string'};
1372 } else {
1373 $co{'age_string_date'} = $co{'age_string'};
1374 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1375 }
1376 return %co;
1377 }
1378
1379 # parse ref from ref_file, given by ref_id, with given type
1380 sub parse_ref {
1381 my $ref_file = shift;
1382 my $ref_id = shift;
1383 my $type = shift || git_get_type($ref_id);
1384 my %ref_item;
1385
1386 $ref_item{'type'} = $type;
1387 $ref_item{'id'} = $ref_id;
1388 $ref_item{'epoch'} = 0;
1389 $ref_item{'age'} = "unknown";
1390 if ($type eq "tag") {
1391 my %tag = parse_tag($ref_id);
1392 $ref_item{'comment'} = $tag{'comment'};
1393 if ($tag{'type'} eq "commit") {
1394 my %co = parse_commit($tag{'object'});
1395 $ref_item{'epoch'} = $co{'committer_epoch'};
1396 $ref_item{'age'} = $co{'age_string'};
1397 } elsif (defined($tag{'epoch'})) {
1398 my $age = time - $tag{'epoch'};
1399 $ref_item{'epoch'} = $tag{'epoch'};
1400 $ref_item{'age'} = age_string($age);
1401 }
1402 $ref_item{'reftype'} = $tag{'type'};
1403 $ref_item{'name'} = $tag{'name'};
1404 $ref_item{'refid'} = $tag{'object'};
1405 } elsif ($type eq "commit"){
1406 my %co = parse_commit($ref_id);
1407 $ref_item{'reftype'} = "commit";
1408 $ref_item{'name'} = $ref_file;
1409 $ref_item{'title'} = $co{'title'};
1410 $ref_item{'refid'} = $ref_id;
1411 $ref_item{'epoch'} = $co{'committer_epoch'};
1412 $ref_item{'age'} = $co{'age_string'};
1413 } else {
1414 $ref_item{'reftype'} = $type;
1415 $ref_item{'name'} = $ref_file;
1416 $ref_item{'refid'} = $ref_id;
1417 }
1418
1419 return %ref_item;
1420 }
1421
1422 # parse line of git-diff-tree "raw" output
1423 sub parse_difftree_raw_line {
1424 my $line = shift;
1425 my %res;
1426
1427 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1428 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1429 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1430 $res{'from_mode'} = $1;
1431 $res{'to_mode'} = $2;
1432 $res{'from_id'} = $3;
1433 $res{'to_id'} = $4;
1434 $res{'status'} = $5;
1435 $res{'similarity'} = $6;
1436 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1437 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1438 } else {
1439 $res{'file'} = unquote($7);
1440 }
1441 }
1442 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1443 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1444 $res{'commit'} = $1;
1445 }
1446
1447 return wantarray ? %res : \%res;
1448 }
1449
1450 # parse line of git-ls-tree output
1451 sub parse_ls_tree_line ($;%) {
1452 my $line = shift;
1453 my %opts = @_;
1454 my %res;
1455
1456 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1457 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1458
1459 $res{'mode'} = $1;
1460 $res{'type'} = $2;
1461 $res{'hash'} = $3;
1462 if ($opts{'-z'}) {
1463 $res{'name'} = $4;
1464 } else {
1465 $res{'name'} = unquote($4);
1466 }
1467
1468 return wantarray ? %res : \%res;
1469 }
1470
1471 ## ......................................................................
1472 ## parse to array of hashes functions
1473
1474 sub git_get_heads_list {
1475 my $limit = shift;
1476 my @headslist;
1477
1478 open my $fd, '-|', git_cmd(), 'for-each-ref',
1479 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1480 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1481 'refs/heads'
1482 or return;
1483 while (my $line = <$fd>) {
1484 my %ref_item;
1485
1486 chomp $line;
1487 my ($refinfo, $committerinfo) = split(/\0/, $line);
1488 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1489 my ($committer, $epoch, $tz) =
1490 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1491 $name =~ s!^refs/heads/!!;
1492
1493 $ref_item{'name'} = $name;
1494 $ref_item{'id'} = $hash;
1495 $ref_item{'title'} = $title || '(no commit message)';
1496 $ref_item{'epoch'} = $epoch;
1497 if ($epoch) {
1498 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1499 } else {
1500 $ref_item{'age'} = "unknown";
1501 }
1502
1503 push @headslist, \%ref_item;
1504 }
1505 close $fd;
1506
1507 return wantarray ? @headslist : \@headslist;
1508 }
1509
1510 sub git_get_tags_list {
1511 my $limit = shift;
1512 my @tagslist;
1513
1514 open my $fd, '-|', git_cmd(), 'for-each-ref',
1515 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1516 '--format=%(objectname) %(objecttype) %(refname) '.
1517 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1518 'refs/tags'
1519 or return;
1520 while (my $line = <$fd>) {
1521 my %ref_item;
1522
1523 chomp $line;
1524 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1525 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1526 my ($creator, $epoch, $tz) =
1527 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1528 $name =~ s!^refs/tags/!!;
1529
1530 $ref_item{'type'} = $type;
1531 $ref_item{'id'} = $id;
1532 $ref_item{'name'} = $name;
1533 if ($type eq "tag") {
1534 $ref_item{'subject'} = $title;
1535 $ref_item{'reftype'} = $reftype;
1536 $ref_item{'refid'} = $refid;
1537 } else {
1538 $ref_item{'reftype'} = $type;
1539 $ref_item{'refid'} = $id;
1540 }
1541
1542 if ($type eq "tag" || $type eq "commit") {
1543 $ref_item{'epoch'} = $epoch;
1544 if ($epoch) {
1545 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1546 } else {
1547 $ref_item{'age'} = "unknown";
1548 }
1549 }
1550
1551 push @tagslist, \%ref_item;
1552 }
1553 close $fd;
1554
1555 return wantarray ? @tagslist : \@tagslist;
1556 }
1557
1558 ## ----------------------------------------------------------------------
1559 ## filesystem-related functions
1560
1561 sub get_file_owner {
1562 my $path = shift;
1563
1564 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1565 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1566 if (!defined $gcos) {
1567 return undef;
1568 }
1569 my $owner = $gcos;
1570 $owner =~ s/[,;].*$//;
1571 return to_utf8($owner);
1572 }
1573
1574 ## ......................................................................
1575 ## mimetype related functions
1576
1577 sub mimetype_guess_file {
1578 my $filename = shift;
1579 my $mimemap = shift;
1580 -r $mimemap or return undef;
1581
1582 my %mimemap;
1583 open(MIME, $mimemap) or return undef;
1584 while (<MIME>) {
1585 next if m/^#/; # skip comments
1586 my ($mime, $exts) = split(/\t+/);
1587 if (defined $exts) {
1588 my @exts = split(/\s+/, $exts);
1589 foreach my $ext (@exts) {
1590 $mimemap{$ext} = $mime;
1591 }
1592 }
1593 }
1594 close(MIME);
1595
1596 $filename =~ /\.([^.]*)$/;
1597 return $mimemap{$1};
1598 }
1599
1600 sub mimetype_guess {
1601 my $filename = shift;
1602 my $mime;
1603 $filename =~ /\./ or return undef;
1604
1605 if ($mimetypes_file) {
1606 my $file = $mimetypes_file;
1607 if ($file !~ m!^/!) { # if it is relative path
1608 # it is relative to project
1609 $file = "$projectroot/$project/$file";
1610 }
1611 $mime = mimetype_guess_file($filename, $file);
1612 }
1613 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1614 return $mime;
1615 }
1616
1617 sub blob_mimetype {
1618 my $fd = shift;
1619 my $filename = shift;
1620
1621 if ($filename) {
1622 my $mime = mimetype_guess($filename);
1623 $mime and return $mime;
1624 }
1625
1626 # just in case
1627 return $default_blob_plain_mimetype unless $fd;
1628
1629 if (-T $fd) {
1630 return 'text/plain' .
1631 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1632 } elsif (! $filename) {
1633 return 'application/octet-stream';
1634 } elsif ($filename =~ m/\.png$/i) {
1635 return 'image/png';
1636 } elsif ($filename =~ m/\.gif$/i) {
1637 return 'image/gif';
1638 } elsif ($filename =~ m/\.jpe?g$/i) {
1639 return 'image/jpeg';
1640 } else {
1641 return 'application/octet-stream';
1642 }
1643 }
1644
1645 ## ======================================================================
1646 ## functions printing HTML: header, footer, error page
1647
1648 sub git_header_html {
1649 my $status = shift || "200 OK";
1650 my $expires = shift;
1651
1652 my $title = "$site_name";
1653 if (defined $project) {
1654 $title .= " - $project";
1655 if (defined $action) {
1656 $title .= "/$action";
1657 if (defined $file_name) {
1658 $title .= " - " . esc_path($file_name);
1659 if ($action eq "tree" && $file_name !~ m|/$|) {
1660 $title .= "/";
1661 }
1662 }
1663 }
1664 }
1665 my $content_type;
1666 # require explicit support from the UA if we are to send the page as
1667 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1668 # we have to do this because MSIE sometimes globs '*/*', pretending to
1669 # support xhtml+xml but choking when it gets what it asked for.
1670 if (defined $cgi->http('HTTP_ACCEPT') &&
1671 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1672 $cgi->Accept('application/xhtml+xml') != 0) {
1673 $content_type = 'application/xhtml+xml';
1674 } else {
1675 $content_type = 'text/html';
1676 }
1677 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1678 -status=> $status, -expires => $expires);
1679 print <<EOF;
1680 <?xml version="1.0" encoding="utf-8"?>
1681 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1682 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1683 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1684 <!-- git core binaries version $git_version -->
1685 <head>
1686 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1687 <meta name="generator" content="gitweb/$version git/$git_version"/>
1688 <meta name="robots" content="index, nofollow"/>
1689 <title>$title</title>
1690 EOF
1691 # print out each stylesheet that exist
1692 if (defined $stylesheet) {
1693 #provides backwards capability for those people who define style sheet in a config file
1694 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1695 } else {
1696 foreach my $stylesheet (@stylesheets) {
1697 next unless $stylesheet;
1698 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1699 }
1700 }
1701 if (defined $project) {
1702 printf('<link rel="alternate" title="%s log RSS feed" '.
1703 'href="%s" type="application/rss+xml" />'."\n",
1704 esc_param($project), href(action=>"rss"));
1705 printf('<link rel="alternate" title="%s log Atom feed" '.
1706 'href="%s" type="application/atom+xml" />'."\n",
1707 esc_param($project), href(action=>"atom"));
1708 } else {
1709 printf('<link rel="alternate" title="%s projects list" '.
1710 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1711 $site_name, href(project=>undef, action=>"project_index"));
1712 printf('<link rel="alternate" title="%s projects feeds" '.
1713 'href="%s" type="text/x-opml"/>'."\n",
1714 $site_name, href(project=>undef, action=>"opml"));
1715 }
1716 if (defined $favicon) {
1717 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1718 }
1719
1720 print "</head>\n" .
1721 "<body>\n";
1722
1723 if (-f $site_header) {
1724 open (my $fd, $site_header);
1725 print <$fd>;
1726 close $fd;
1727 }
1728
1729 print "<div class=\"page_header\">\n" .
1730 $cgi->a({-href => esc_url($logo_url),
1731 -title => $logo_label},
1732 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1733 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1734 if (defined $project) {
1735 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1736 if (defined $action) {
1737 print " / $action";
1738 }
1739 print "\n";
1740 }
1741 my ($have_search) = gitweb_check_feature('search');
1742 if ((defined $project) && ($have_search)) {
1743 if (!defined $searchtext) {
1744 $searchtext = "";
1745 }
1746 my $search_hash;
1747 if (defined $hash_base) {
1748 $search_hash = $hash_base;
1749 } elsif (defined $hash) {
1750 $search_hash = $hash;
1751 } else {
1752 $search_hash = "HEAD";
1753 }
1754 $cgi->param("a", "search");
1755 $cgi->param("h", $search_hash);
1756 $cgi->param("p", $project);
1757 print $cgi->startform(-method => "get", -action => $my_uri) .
1758 "<div class=\"search\">\n" .
1759 $cgi->hidden(-name => "p") . "\n" .
1760 $cgi->hidden(-name => "a") . "\n" .
1761 $cgi->hidden(-name => "h") . "\n" .
1762 $cgi->popup_menu(-name => 'st', -default => 'commit',
1763 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1764 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1765 " search:\n",
1766 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1767 "</div>" .
1768 $cgi->end_form() . "\n";
1769 }
1770 print "</div>\n";
1771 }
1772
1773 sub git_footer_html {
1774 print "<div class=\"page_footer\">\n";
1775 if (defined $project) {
1776 my $descr = git_get_project_description($project);
1777 if (defined $descr) {
1778 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1779 }
1780 print $cgi->a({-href => href(action=>"rss"),
1781 -class => "rss_logo"}, "RSS") . " ";
1782 print $cgi->a({-href => href(action=>"atom"),
1783 -class => "rss_logo"}, "Atom") . "\n";
1784 } else {
1785 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1786 -class => "rss_logo"}, "OPML") . " ";
1787 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1788 -class => "rss_logo"}, "TXT") . "\n";
1789 }
1790 print "</div>\n" ;
1791
1792 if (-f $site_footer) {
1793 open (my $fd, $site_footer);
1794 print <$fd>;
1795 close $fd;
1796 }
1797
1798 print "</body>\n" .
1799 "</html>";
1800 }
1801
1802 sub die_error {
1803 my $status = shift || "403 Forbidden";
1804 my $error = shift || "Malformed query, file missing or permission denied";
1805
1806 git_header_html($status);
1807 print <<EOF;
1808 <div class="page_body">
1809 <br /><br />
1810 $status - $error
1811 <br />
1812 </div>
1813 EOF
1814 git_footer_html();
1815 exit;
1816 }
1817
1818 ## ----------------------------------------------------------------------
1819 ## functions printing or outputting HTML: navigation
1820
1821 sub git_print_page_nav {
1822 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1823 $extra = '' if !defined $extra; # pager or formats
1824
1825 my @navs = qw(summary shortlog log commit commitdiff tree);
1826 if ($suppress) {
1827 @navs = grep { $_ ne $suppress } @navs;
1828 }
1829
1830 my %arg = map { $_ => {action=>$_} } @navs;
1831 if (defined $head) {
1832 for (qw(commit commitdiff)) {
1833 $arg{$_}{hash} = $head;
1834 }
1835 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1836 for (qw(shortlog log)) {
1837 $arg{$_}{hash} = $head;
1838 }
1839 }
1840 }
1841 $arg{tree}{hash} = $treehead if defined $treehead;
1842 $arg{tree}{hash_base} = $treebase if defined $treebase;
1843
1844 print "<div class=\"page_nav\">\n" .
1845 (join " | ",
1846 map { $_ eq $current ?
1847 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1848 } @navs);
1849 print "<br/>\n$extra<br/>\n" .
1850 "</div>\n";
1851 }
1852
1853 sub format_paging_nav {
1854 my ($action, $hash, $head, $page, $nrevs) = @_;
1855 my $paging_nav;
1856
1857
1858 if ($hash ne $head || $page) {
1859 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1860 } else {
1861 $paging_nav .= "HEAD";
1862 }
1863
1864 if ($page > 0) {
1865 $paging_nav .= " &sdot; " .
1866 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1867 -accesskey => "p", -title => "Alt-p"}, "prev");
1868 } else {
1869 $paging_nav .= " &sdot; prev";
1870 }
1871
1872 if ($nrevs >= (100 * ($page+1)-1)) {
1873 $paging_nav .= " &sdot; " .
1874 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1875 -accesskey => "n", -title => "Alt-n"}, "next");
1876 } else {
1877 $paging_nav .= " &sdot; next";
1878 }
1879
1880 return $paging_nav;
1881 }
1882
1883 ## ......................................................................
1884 ## functions printing or outputting HTML: div
1885
1886 sub git_print_header_div {
1887 my ($action, $title, $hash, $hash_base) = @_;
1888 my %args = ();
1889
1890 $args{action} = $action;
1891 $args{hash} = $hash if $hash;
1892 $args{hash_base} = $hash_base if $hash_base;
1893
1894 print "<div class=\"header\">\n" .
1895 $cgi->a({-href => href(%args), -class => "title"},
1896 $title ? $title : $action) .
1897 "\n</div>\n";
1898 }
1899
1900 #sub git_print_authorship (\%) {
1901 sub git_print_authorship {
1902 my $co = shift;
1903
1904 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1905 print "<div class=\"author_date\">" .
1906 esc_html($co->{'author_name'}) .
1907 " [$ad{'rfc2822'}";
1908 if ($ad{'hour_local'} < 6) {
1909 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1910 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1911 } else {
1912 printf(" (%02d:%02d %s)",
1913 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1914 }
1915 print "]</div>\n";
1916 }
1917
1918 sub git_print_page_path {
1919 my $name = shift;
1920 my $type = shift;
1921 my $hb = shift;
1922
1923
1924 print "<div class=\"page_path\">";
1925 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1926 -title => 'tree root'}, "[$project]");
1927 print " / ";
1928 if (defined $name) {
1929 my @dirname = split '/', $name;
1930 my $basename = pop @dirname;
1931 my $fullname = '';
1932
1933 foreach my $dir (@dirname) {
1934 $fullname .= ($fullname ? '/' : '') . $dir;
1935 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1936 hash_base=>$hb),
1937 -title => esc_html($fullname)}, esc_path($dir));
1938 print " / ";
1939 }
1940 if (defined $type && $type eq 'blob') {
1941 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1942 hash_base=>$hb),
1943 -title => esc_html($name)}, esc_path($basename));
1944 } elsif (defined $type && $type eq 'tree') {
1945 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1946 hash_base=>$hb),
1947 -title => esc_html($name)}, esc_path($basename));
1948 print " / ";
1949 } else {
1950 print esc_path($basename);
1951 }
1952 }
1953 print "<br/></div>\n";
1954 }
1955
1956 # sub git_print_log (\@;%) {
1957 sub git_print_log ($;%) {
1958 my $log = shift;
1959 my %opts = @_;
1960
1961 if ($opts{'-remove_title'}) {
1962 # remove title, i.e. first line of log
1963 shift @$log;
1964 }
1965 # remove leading empty lines
1966 while (defined $log->[0] && $log->[0] eq "") {
1967 shift @$log;
1968 }
1969
1970 # print log
1971 my $signoff = 0;
1972 my $empty = 0;
1973 foreach my $line (@$log) {
1974 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1975 $signoff = 1;
1976 $empty = 0;
1977 if (! $opts{'-remove_signoff'}) {
1978 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1979 next;
1980 } else {
1981 # remove signoff lines
1982 next;
1983 }
1984 } else {
1985 $signoff = 0;
1986 }
1987
1988 # print only one empty line
1989 # do not print empty line after signoff
1990 if ($line eq "") {
1991 next if ($empty || $signoff);
1992 $empty = 1;
1993 } else {
1994 $empty = 0;
1995 }
1996
1997 print format_log_line_html($line) . "<br/>\n";
1998 }
1999
2000 if ($opts{'-final_empty_line'}) {
2001 # end with single empty line
2002 print "<br/>\n" unless $empty;
2003 }
2004 }
2005
2006 # return link target (what link points to)
2007 sub git_get_link_target {
2008 my $hash = shift;
2009 my $link_target;
2010
2011 # read link
2012 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2013 or return;
2014 {
2015 local $/;
2016 $link_target = <$fd>;
2017 }
2018 close $fd
2019 or return;
2020
2021 return $link_target;
2022 }
2023
2024 # given link target, and the directory (basedir) the link is in,
2025 # return target of link relative to top directory (top tree);
2026 # return undef if it is not possible (including absolute links).
2027 sub normalize_link_target {
2028 my ($link_target, $basedir, $hash_base) = @_;
2029
2030 # we can normalize symlink target only if $hash_base is provided
2031 return unless $hash_base;
2032
2033 # absolute symlinks (beginning with '/') cannot be normalized
2034 return if (substr($link_target, 0, 1) eq '/');
2035
2036 # normalize link target to path from top (root) tree (dir)
2037 my $path;
2038 if ($basedir) {
2039 $path = $basedir . '/' . $link_target;
2040 } else {
2041 # we are in top (root) tree (dir)
2042 $path = $link_target;
2043 }
2044
2045 # remove //, /./, and /../
2046 my @path_parts;
2047 foreach my $part (split('/', $path)) {
2048 # discard '.' and ''
2049 next if (!$part || $part eq '.');
2050 # handle '..'
2051 if ($part eq '..') {
2052 if (@path_parts) {
2053 pop @path_parts;
2054 } else {
2055 # link leads outside repository (outside top dir)
2056 return;
2057 }
2058 } else {
2059 push @path_parts, $part;
2060 }
2061 }
2062 $path = join('/', @path_parts);
2063
2064 return $path;
2065 }
2066
2067 # print tree entry (row of git_tree), but without encompassing <tr> element
2068 sub git_print_tree_entry {
2069 my ($t, $basedir, $hash_base, $have_blame) = @_;
2070
2071 my %base_key = ();
2072 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2073
2074 # The format of a table row is: mode list link. Where mode is
2075 # the mode of the entry, list is the name of the entry, an href,
2076 # and link is the action links of the entry.
2077
2078 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2079 if ($t->{'type'} eq "blob") {
2080 print "<td class=\"list\">" .
2081 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2082 file_name=>"$basedir$t->{'name'}", %base_key),
2083 -class => "list"}, esc_path($t->{'name'}));
2084 if (S_ISLNK(oct $t->{'mode'})) {
2085 my $link_target = git_get_link_target($t->{'hash'});
2086 if ($link_target) {
2087 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2088 if (defined $norm_target) {
2089 print " -> " .
2090 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2091 file_name=>$norm_target),
2092 -title => $norm_target}, esc_path($link_target));
2093 } else {
2094 print " -> " . esc_path($link_target);
2095 }
2096 }
2097 }
2098 print "</td>\n";
2099 print "<td class=\"link\">";
2100 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2101 file_name=>"$basedir$t->{'name'}", %base_key)},
2102 "blob");
2103 if ($have_blame) {
2104 print " | " .
2105 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2106 file_name=>"$basedir$t->{'name'}", %base_key)},
2107 "blame");
2108 }
2109 if (defined $hash_base) {
2110 print " | " .
2111 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2112 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2113 "history");
2114 }
2115 print " | " .
2116 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2117 file_name=>"$basedir$t->{'name'}")},
2118 "raw");
2119 print "</td>\n";
2120
2121 } elsif ($t->{'type'} eq "tree") {
2122 print "<td class=\"list\">";
2123 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2124 file_name=>"$basedir$t->{'name'}", %base_key)},
2125 esc_path($t->{'name'}));
2126 print "</td>\n";
2127 print "<td class=\"link\">";
2128 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2129 file_name=>"$basedir$t->{'name'}", %base_key)},
2130 "tree");
2131 if (defined $hash_base) {
2132 print " | " .
2133 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2134 file_name=>"$basedir$t->{'name'}")},
2135 "history");
2136 }
2137 print "</td>\n";
2138 }
2139 }
2140
2141 ## ......................................................................
2142 ## functions printing large fragments of HTML
2143
2144 sub git_difftree_body {
2145 my ($difftree, $hash, $parent) = @_;
2146 my ($have_blame) = gitweb_check_feature('blame');
2147 print "<div class=\"list_head\">\n";
2148 if ($#{$difftree} > 10) {
2149 print(($#{$difftree} + 1) . " files changed:\n");
2150 }
2151 print "</div>\n";
2152
2153 print "<table class=\"diff_tree\">\n";
2154 my $alternate = 1;
2155 my $patchno = 0;
2156 foreach my $line (@{$difftree}) {
2157 my %diff = parse_difftree_raw_line($line);
2158
2159 if ($alternate) {
2160 print "<tr class=\"dark\">\n";
2161 } else {
2162 print "<tr class=\"light\">\n";
2163 }
2164 $alternate ^= 1;
2165
2166 my ($to_mode_oct, $to_mode_str, $to_file_type);
2167 my ($from_mode_oct, $from_mode_str, $from_file_type);
2168 if ($diff{'to_mode'} ne ('0' x 6)) {
2169 $to_mode_oct = oct $diff{'to_mode'};
2170 if (S_ISREG($to_mode_oct)) { # only for regular file
2171 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2172 }
2173 $to_file_type = file_type($diff{'to_mode'});
2174 }
2175 if ($diff{'from_mode'} ne ('0' x 6)) {
2176 $from_mode_oct = oct $diff{'from_mode'};
2177 if (S_ISREG($to_mode_oct)) { # only for regular file
2178 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2179 }
2180 $from_file_type = file_type($diff{'from_mode'});
2181 }
2182
2183 if ($diff{'status'} eq "A") { # created
2184 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2185 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2186 $mode_chng .= "]</span>";
2187 print "<td>";
2188 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2189 hash_base=>$hash, file_name=>$diff{'file'}),
2190 -class => "list"}, esc_path($diff{'file'}));
2191 print "</td>\n";
2192 print "<td>$mode_chng</td>\n";
2193 print "<td class=\"link\">";
2194 if ($action eq 'commitdiff') {
2195 # link to patch
2196 $patchno++;
2197 print $cgi->a({-href => "#patch$patchno"}, "patch");
2198 print " | ";
2199 }
2200 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2201 hash_base=>$hash, file_name=>$diff{'file'})},
2202 "blob") . " | ";
2203 print "</td>\n";
2204
2205 } elsif ($diff{'status'} eq "D") { # deleted
2206 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2207 print "<td>";
2208 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2209 hash_base=>$parent, file_name=>$diff{'file'}),
2210 -class => "list"}, esc_path($diff{'file'}));
2211 print "</td>\n";
2212 print "<td>$mode_chng</td>\n";
2213 print "<td class=\"link\">";
2214 if ($action eq 'commitdiff') {
2215 # link to patch
2216 $patchno++;
2217 print $cgi->a({-href => "#patch$patchno"}, "patch");
2218 print " | ";
2219 }
2220 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2221 hash_base=>$parent, file_name=>$diff{'file'})},
2222 "blob") . " | ";
2223 if ($have_blame) {
2224 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2225 file_name=>$diff{'file'})},
2226 "blame") . " | ";
2227 }
2228 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2229 file_name=>$diff{'file'})},
2230 "history");
2231 print "</td>\n";
2232
2233 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
2234 my $mode_chnge = "";
2235 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2236 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2237 if ($from_file_type != $to_file_type) {
2238 $mode_chnge .= " from $from_file_type to $to_file_type";
2239 }
2240 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2241 if ($from_mode_str && $to_mode_str) {
2242 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2243 } elsif ($to_mode_str) {
2244 $mode_chnge .= " mode: $to_mode_str";
2245 }
2246 }
2247 $mode_chnge .= "]</span>\n";
2248 }
2249 print "<td>";
2250 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2251 hash_base=>$hash, file_name=>$diff{'file'}),
2252 -class => "list"}, esc_path($diff{'file'}));
2253 print "</td>\n";
2254 print "<td>$mode_chnge</td>\n";
2255 print "<td class=\"link\">";
2256 if ($action eq 'commitdiff') {
2257 # link to patch
2258 $patchno++;
2259 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2260 " | ";
2261 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2262 # "commit" view and modified file (not onlu mode changed)
2263 print $cgi->a({-href => href(action=>"blobdiff",
2264 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2265 hash_base=>$hash, hash_parent_base=>$parent,
2266 file_name=>$diff{'file'})},
2267 "diff") .
2268 " | ";
2269 }
2270 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2271 hash_base=>$hash, file_name=>$diff{'file'})},
2272 "blob") . " | ";
2273 if ($have_blame) {
2274 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2275 file_name=>$diff{'file'})},
2276 "blame") . " | ";
2277 }
2278 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2279 file_name=>$diff{'file'})},
2280 "history");
2281 print "</td>\n";
2282
2283 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
2284 my %status_name = ('R' => 'moved', 'C' => 'copied');
2285 my $nstatus = $status_name{$diff{'status'}};
2286 my $mode_chng = "";
2287 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2288 # mode also for directories, so we cannot use $to_mode_str
2289 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2290 }
2291 print "<td>" .
2292 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2293 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
2294 -class => "list"}, esc_path($diff{'to_file'})) . "</td>\n" .
2295 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2296 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2297 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2298 -class => "list"}, esc_path($diff{'from_file'})) .
2299 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2300 "<td class=\"link\">";
2301 if ($action eq 'commitdiff') {
2302 # link to patch
2303 $patchno++;
2304 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2305 " | ";
2306 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2307 # "commit" view and modified file (not only pure rename or copy)
2308 print $cgi->a({-href => href(action=>"blobdiff",
2309 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2310 hash_base=>$hash, hash_parent_base=>$parent,
2311 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2312 "diff") .
2313 " | ";
2314 }
2315 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2316 hash_base=>$parent, file_name=>$diff{'to_file'})},
2317 "blob") . " | ";
2318 if ($have_blame) {
2319 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2320 file_name=>$diff{'to_file'})},
2321 "blame") . " | ";
2322 }
2323 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2324 file_name=>$diff{'to_file'})},
2325 "history");
2326 print "</td>\n";
2327
2328 } # we should not encounter Unmerged (U) or Unknown (X) status
2329 print "</tr>\n";
2330 }
2331 print "</table>\n";
2332 }
2333
2334 sub git_patchset_body {
2335 my ($fd, $difftree, $hash, $hash_parent) = @_;
2336
2337 my $patch_idx = 0;
2338 my $patch_line;
2339 my $diffinfo;
2340 my (%from, %to);
2341 my ($from_id, $to_id);
2342
2343 print "<div class=\"patchset\">\n";
2344
2345 # skip to first patch
2346 while ($patch_line = <$fd>) {
2347 chomp $patch_line;
2348
2349 last if ($patch_line =~ m/^diff /);
2350 }
2351
2352 PATCH:
2353 while ($patch_line) {
2354 my @diff_header;
2355
2356 # git diff header
2357 #assert($patch_line =~ m/^diff /) if DEBUG;
2358 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2359 push @diff_header, $patch_line;
2360
2361 # extended diff header
2362 EXTENDED_HEADER:
2363 while ($patch_line = <$fd>) {
2364 chomp $patch_line;
2365
2366 last EXTENDED_HEADER if ($patch_line =~ m/^--- /);
2367
2368 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2369 $from_id = $1;
2370 $to_id = $2;
2371 }
2372
2373 push @diff_header, $patch_line;
2374 }
2375 #last PATCH unless $patch_line;
2376 my $last_patch_line = $patch_line;
2377
2378 # check if current patch belong to current raw line
2379 # and parse raw git-diff line if needed
2380 if (defined $diffinfo &&
2381 $diffinfo->{'from_id'} eq $from_id &&
2382 $diffinfo->{'to_id'} eq $to_id) {
2383 # this is split patch
2384 print "<div class=\"patch cont\">\n";
2385 } else {
2386 # advance raw git-diff output if needed
2387 $patch_idx++ if defined $diffinfo;
2388
2389 # read and prepare patch information
2390 if (ref($difftree->[$patch_idx]) eq "HASH") {
2391 # pre-parsed (or generated by hand)
2392 $diffinfo = $difftree->[$patch_idx];
2393 } else {
2394 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2395 }
2396 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2397 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2398 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2399 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2400 hash=>$diffinfo->{'from_id'},
2401 file_name=>$from{'file'});
2402 }
2403 if ($diffinfo->{'status'} ne "D") { # not deleted file
2404 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2405 hash=>$diffinfo->{'to_id'},
2406 file_name=>$to{'file'});
2407 }
2408 # this is first patch for raw difftree line with $patch_idx index
2409 # we index @$difftree array from 0, but number patches from 1
2410 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2411 }
2412
2413 # print "git diff" header
2414 $patch_line = shift @diff_header;
2415 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2416 if ($from{'href'}) {
2417 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2418 'a/' . esc_path($from{'file'}));
2419 } else { # file was added
2420 $patch_line .= 'a/' . esc_path($from{'file'});
2421 }
2422 $patch_line .= ' ';
2423 if ($to{'href'}) {
2424 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2425 'b/' . esc_path($to{'file'}));
2426 } else { # file was deleted
2427 $patch_line .= 'b/' . esc_path($to{'file'});
2428 }
2429 print "<div class=\"diff header\">$patch_line</div>\n";
2430
2431 # print extended diff header
2432 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2433 EXTENDED_HEADER:
2434 foreach $patch_line (@diff_header) {
2435 # match <path>
2436 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2437 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2438 esc_path($from{'file'}));
2439 }
2440 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2441 $patch_line = $cgi->a({-href=>$to{'href'}, -class=>"path"},
2442 esc_path($to{'file'}));
2443 }
2444 # match <mode>
2445 if ($patch_line =~ m/\s(\d{6})$/) {
2446 $patch_line .= '<span class="info"> (' .
2447 file_type_long($1) .
2448 ')</span>';
2449 }
2450 # match <hash>
2451 if ($patch_line =~ m/^index/) {
2452 my ($from_link, $to_link);
2453 if ($from{'href'}) {
2454 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2455 substr($diffinfo->{'from_id'},0,7));
2456 } else {
2457 $from_link = '0' x 7;
2458 }
2459 if ($to{'href'}) {
2460 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2461 substr($diffinfo->{'to_id'},0,7));
2462 } else {
2463 $to_link = '0' x 7;
2464 }
2465 #affirm {
2466 # my ($from_hash, $to_hash) =
2467 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2468 # my ($from_id, $to_id) =
2469 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2470 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2471 #} if DEBUG;
2472 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2473 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2474 }
2475 print $patch_line . "<br/>\n";
2476 }
2477 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2478
2479 # from-file/to-file diff header
2480 $patch_line = $last_patch_line;
2481 #assert($patch_line =~ m/^---/) if DEBUG;
2482 if ($from{'href'}) {
2483 $patch_line = '--- a/' .
2484 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2485 esc_path($from{'file'}));
2486 }
2487 print "<div class=\"diff from_file\">$patch_line</div>\n";
2488
2489 $patch_line = <$fd>;
2490 #last PATCH unless $patch_line;
2491 chomp $patch_line;
2492
2493 #assert($patch_line =~ m/^+++/) if DEBUG;
2494 if ($to{'href'}) {
2495 $patch_line = '+++ b/' .
2496 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2497 esc_path($to{'file'}));
2498 }
2499 print "<div class=\"diff to_file\">$patch_line</div>\n";
2500
2501 # the patch itself
2502 LINE:
2503 while ($patch_line = <$fd>) {
2504 chomp $patch_line;
2505
2506 next PATCH if ($patch_line =~ m/^diff /);
2507
2508 print format_diff_line($patch_line, \%from, \%to);
2509 }
2510
2511 } continue {
2512 print "</div>\n"; # class="patch"
2513 }
2514
2515 print "</div>\n"; # class="patchset"
2516 }
2517
2518 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2519
2520 sub git_project_list_body {
2521 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2522
2523 my ($check_forks) = gitweb_check_feature('forks');
2524
2525 my @projects;
2526 foreach my $pr (@$projlist) {
2527 my (@aa) = git_get_last_activity($pr->{'path'});
2528 unless (@aa) {
2529 next;
2530 }
2531 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2532 if (!defined $pr->{'descr'}) {
2533 my $descr = git_get_project_description($pr->{'path'}) || "";
2534 $pr->{'descr_long'} = to_utf8($descr);
2535 $pr->{'descr'} = chop_str($descr, 25, 5);
2536 }
2537 if (!defined $pr->{'owner'}) {
2538 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2539 }
2540 if ($check_forks) {
2541 my $pname = $pr->{'path'};
2542 if (($pname =~ s/\.git$//) &&
2543 ($pname !~ /\/$/) &&
2544 (-d "$projectroot/$pname")) {
2545 $pr->{'forks'} = "-d $projectroot/$pname";
2546 }
2547 else {
2548 $pr->{'forks'} = 0;
2549 }
2550 }
2551 push @projects, $pr;
2552 }
2553
2554 $order ||= "project";
2555 $from = 0 unless defined $from;
2556 $to = $#projects if (!defined $to || $#projects < $to);
2557
2558 print "<table class=\"project_list\">\n";
2559 unless ($no_header) {
2560 print "<tr>\n";
2561 if ($check_forks) {
2562 print "<th></th>\n";
2563 }
2564 if ($order eq "project") {
2565 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2566 print "<th>Project</th>\n";
2567 } else {
2568 print "<th>" .
2569 $cgi->a({-href => href(project=>undef, order=>'project'),
2570 -class => "header"}, "Project") .
2571 "</th>\n";
2572 }
2573 if ($order eq "descr") {
2574 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2575 print "<th>Description</th>\n";
2576 } else {
2577 print "<th>" .
2578 $cgi->a({-href => href(project=>undef, order=>'descr'),
2579 -class => "header"}, "Description") .
2580 "</th>\n";
2581 }
2582 if ($order eq "owner") {
2583 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2584 print "<th>Owner</th>\n";
2585 } else {
2586 print "<th>" .
2587 $cgi->a({-href => href(project=>undef, order=>'owner'),
2588 -class => "header"}, "Owner") .
2589 "</th>\n";
2590 }
2591 if ($order eq "age") {
2592 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2593 print "<th>Last Change</th>\n";
2594 } else {
2595 print "<th>" .
2596 $cgi->a({-href => href(project=>undef, order=>'age'),
2597 -class => "header"}, "Last Change") .
2598 "</th>\n";
2599 }
2600 print "<th></th>\n" .
2601 "</tr>\n";
2602 }
2603 my $alternate = 1;
2604 for (my $i = $from; $i <= $to; $i++) {
2605 my $pr = $projects[$i];
2606 if ($alternate) {
2607 print "<tr class=\"dark\">\n";
2608 } else {
2609 print "<tr class=\"light\">\n";
2610 }
2611 $alternate ^= 1;
2612 if ($check_forks) {
2613 print "<td>";
2614 if ($pr->{'forks'}) {
2615 print "<!-- $pr->{'forks'} -->\n";
2616 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2617 }
2618 print "</td>\n";
2619 }
2620 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2621 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2622 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2623 -class => "list", -title => $pr->{'descr_long'}},
2624 esc_html($pr->{'descr'})) . "</td>\n" .
2625 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2626 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2627 $pr->{'age_string'} . "</td>\n" .
2628 "<td class=\"link\">" .
2629 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2630 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2631 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2632 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2633 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2634 "</td>\n" .
2635 "</tr>\n";
2636 }
2637 if (defined $extra) {
2638 print "<tr>\n";
2639 if ($check_forks) {
2640 print "<td></td>\n";
2641 }
2642 print "<td colspan=\"5\">$extra</td>\n" .
2643 "</tr>\n";
2644 }
2645 print "</table>\n";
2646 }
2647
2648 sub git_shortlog_body {
2649 # uses global variable $project
2650 my ($revlist, $from, $to, $refs, $extra) = @_;
2651
2652 my $have_snapshot = gitweb_have_snapshot();
2653
2654 $from = 0 unless defined $from;
2655 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2656
2657 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2658 my $alternate = 1;
2659 for (my $i = $from; $i <= $to; $i++) {
2660 my $commit = $revlist->[$i];
2661 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2662 my $ref = format_ref_marker($refs, $commit);
2663 my %co = parse_commit($commit);
2664 if ($alternate) {
2665 print "<tr class=\"dark\">\n";
2666 } else {
2667 print "<tr class=\"light\">\n";
2668 }
2669 $alternate ^= 1;
2670 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2671 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2672 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2673 "<td>";
2674 print format_subject_html($co{'title'}, $co{'title_short'},
2675 href(action=>"commit", hash=>$commit), $ref);
2676 print "</td>\n" .
2677 "<td class=\"link\">" .
2678 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2679 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2680 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2681 if ($have_snapshot) {
2682 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2683 }
2684 print "</td>\n" .
2685 "</tr>\n";
2686 }
2687 if (defined $extra) {
2688 print "<tr>\n" .
2689 "<td colspan=\"4\">$extra</td>\n" .
2690 "</tr>\n";
2691 }
2692 print "</table>\n";
2693 }
2694
2695 sub git_history_body {
2696 # Warning: assumes constant type (blob or tree) during history
2697 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2698
2699 $from = 0 unless defined $from;
2700 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2701
2702 print "<table class=\"history\" cellspacing=\"0\">\n";
2703 my $alternate = 1;
2704 for (my $i = $from; $i <= $to; $i++) {
2705 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2706 next;
2707 }
2708
2709 my $commit = $1;
2710 my %co = parse_commit($commit);
2711 if (!%co) {
2712 next;
2713 }
2714
2715 my $ref = format_ref_marker($refs, $commit);
2716
2717 if ($alternate) {
2718 print "<tr class=\"dark\">\n";
2719 } else {
2720 print "<tr class=\"light\">\n";
2721 }
2722 $alternate ^= 1;
2723 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2724 # shortlog uses chop_str($co{'author_name'}, 10)
2725 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2726 "<td>";
2727 # originally git_history used chop_str($co{'title'}, 50)
2728 print format_subject_html($co{'title'}, $co{'title_short'},
2729 href(action=>"commit", hash=>$commit), $ref);
2730 print "</td>\n" .
2731 "<td class=\"link\">" .
2732 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2733 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2734
2735 if ($ftype eq 'blob') {
2736 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2737 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2738 if (defined $blob_current && defined $blob_parent &&
2739 $blob_current ne $blob_parent) {
2740 print " | " .
2741 $cgi->a({-href => href(action=>"blobdiff",
2742 hash=>$blob_current, hash_parent=>$blob_parent,
2743 hash_base=>$hash_base, hash_parent_base=>$commit,
2744 file_name=>$file_name)},
2745 "diff to current");
2746 }
2747 }
2748 print "</td>\n" .
2749 "</tr>\n";
2750 }
2751 if (defined $extra) {
2752 print "<tr>\n" .
2753 "<td colspan=\"4\">$extra</td>\n" .
2754 "</tr>\n";
2755 }
2756 print "</table>\n";
2757 }
2758
2759 sub git_tags_body {
2760 # uses global variable $project
2761 my ($taglist, $from, $to, $extra) = @_;
2762 $from = 0 unless defined $from;
2763 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2764
2765 print "<table class=\"tags\" cellspacing=\"0\">\n";
2766 my $alternate = 1;
2767 for (my $i = $from; $i <= $to; $i++) {
2768 my $entry = $taglist->[$i];
2769 my %tag = %$entry;
2770 my $comment = $tag{'subject'};
2771 my $comment_short;
2772 if (defined $comment) {
2773 $comment_short = chop_str($comment, 30, 5);
2774 }
2775 if ($alternate) {
2776 print "<tr class=\"dark\">\n";
2777 } else {
2778 print "<tr class=\"light\">\n";
2779 }
2780 $alternate ^= 1;
2781 print "<td><i>$tag{'age'}</i></td>\n" .
2782 "<td>" .
2783 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2784 -class => "list name"}, esc_html($tag{'name'})) .
2785 "</td>\n" .
2786 "<td>";
2787 if (defined $comment) {
2788 print format_subject_html($comment, $comment_short,
2789 href(action=>"tag", hash=>$tag{'id'}));
2790 }
2791 print "</td>\n" .
2792 "<td class=\"selflink\">";
2793 if ($tag{'type'} eq "tag") {
2794 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2795 } else {
2796 print "&nbsp;";
2797 }
2798 print "</td>\n" .
2799 "<td class=\"link\">" . " | " .
2800 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2801 if ($tag{'reftype'} eq "commit") {
2802 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2803 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2804 } elsif ($tag{'reftype'} eq "blob") {
2805 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2806 }
2807 print "</td>\n" .
2808 "</tr>";
2809 }
2810 if (defined $extra) {
2811 print "<tr>\n" .
2812 "<td colspan=\"5\">$extra</td>\n" .
2813 "</tr>\n";
2814 }
2815 print "</table>\n";
2816 }
2817
2818 sub git_heads_body {
2819 # uses global variable $project
2820 my ($headlist, $head, $from, $to, $extra) = @_;
2821 $from = 0 unless defined $from;
2822 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2823
2824 print "<table class=\"heads\" cellspacing=\"0\">\n";
2825 my $alternate = 1;
2826 for (my $i = $from; $i <= $to; $i++) {
2827 my $entry = $headlist->[$i];
2828 my %ref = %$entry;
2829 my $curr = $ref{'id'} eq $head;
2830 if ($alternate) {
2831 print "<tr class=\"dark\">\n";
2832 } else {
2833 print "<tr class=\"light\">\n";
2834 }
2835 $alternate ^= 1;
2836 print "<td><i>$ref{'age'}</i></td>\n" .
2837 ($curr ? "<td class=\"current_head\">" : "<td>") .
2838 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2839 -class => "list name"},esc_html($ref{'name'})) .
2840 "</td>\n" .
2841 "<td class=\"link\">" .
2842 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2843 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2844 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2845 "</td>\n" .
2846 "</tr>";
2847 }
2848 if (defined $extra) {
2849 print "<tr>\n" .
2850 "<td colspan=\"3\">$extra</td>\n" .
2851 "</tr>\n";
2852 }
2853 print "</table>\n";
2854 }
2855
2856 sub git_search_grep_body {
2857 my ($greplist, $from, $to, $extra) = @_;
2858 $from = 0 unless defined $from;
2859 $to = $#{$greplist} if (!defined $to || $#{$greplist} < $to);
2860
2861 print "<table class=\"grep\" cellspacing=\"0\">\n";
2862 my $alternate = 1;
2863 for (my $i = $from; $i <= $to; $i++) {
2864 my $commit = $greplist->[$i];
2865 my %co = parse_commit($commit);
2866 if (!%co) {
2867 next;
2868 }
2869 if ($alternate) {
2870 print "<tr class=\"dark\">\n";
2871 } else {
2872 print "<tr class=\"light\">\n";
2873 }
2874 $alternate ^= 1;
2875 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2876 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2877 "<td>" .
2878 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2879 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2880 my $comment = $co{'comment'};
2881 foreach my $line (@$comment) {
2882 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2883 my $lead = esc_html($1) || "";
2884 $lead = chop_str($lead, 30, 10);
2885 my $match = esc_html($2) || "";
2886 my $trail = esc_html($3) || "";
2887 $trail = chop_str($trail, 30, 10);
2888 my $text = "$lead<span class=\"match\">$match</span>$trail";
2889 print chop_str($text, 80, 5) . "<br/>\n";
2890 }
2891 }
2892 print "</td>\n" .
2893 "<td class=\"link\">" .
2894 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2895 " | " .
2896 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2897 print "</td>\n" .
2898 "</tr>\n";
2899 }
2900 if (defined $extra) {
2901 print "<tr>\n" .
2902 "<td colspan=\"3\">$extra</td>\n" .
2903 "</tr>\n";
2904 }
2905 print "</table>\n";
2906 }
2907
2908 ## ======================================================================
2909 ## ======================================================================
2910 ## actions
2911
2912 sub git_project_list {
2913 my $order = $cgi->param('o');
2914 if (defined $order && $order !~ m/project|descr|owner|age/) {
2915 die_error(undef, "Unknown order parameter");
2916 }
2917
2918 my @list = git_get_projects_list();
2919 if (!@list) {
2920 die_error(undef, "No projects found");
2921 }
2922
2923 git_header_html();
2924 if (-f $home_text) {
2925 print "<div class=\"index_include\">\n";
2926 open (my $fd, $home_text);
2927 print <$fd>;
2928 close $fd;
2929 print "</div>\n";
2930 }
2931 git_project_list_body(\@list, $order);
2932 git_footer_html();
2933 }
2934
2935 sub git_forks {
2936 my $order = $cgi->param('o');
2937 if (defined $order && $order !~ m/project|descr|owner|age/) {
2938 die_error(undef, "Unknown order parameter");
2939 }
2940
2941 my @list = git_get_projects_list($project);
2942 if (!@list) {
2943 die_error(undef, "No forks found");
2944 }
2945
2946 git_header_html();
2947 git_print_page_nav('','');
2948 git_print_header_div('summary', "$project forks");
2949 git_project_list_body(\@list, $order);
2950 git_footer_html();
2951 }
2952
2953 sub git_project_index {
2954 my @projects = git_get_projects_list($project);
2955
2956 print $cgi->header(
2957 -type => 'text/plain',
2958 -charset => 'utf-8',
2959 -content_disposition => 'inline; filename="index.aux"');
2960
2961 foreach my $pr (@projects) {
2962 if (!exists $pr->{'owner'}) {
2963 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2964 }
2965
2966 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2967 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2968 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2969 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2970 $path =~ s/ /\+/g;
2971 $owner =~ s/ /\+/g;
2972
2973 print "$path $owner\n";
2974 }
2975 }
2976
2977 sub git_summary {
2978 my $descr = git_get_project_description($project) || "none";
2979 my %co = parse_commit("HEAD");
2980 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2981 my $head = $co{'id'};
2982
2983 my $owner = git_get_project_owner($project);
2984
2985 my $refs = git_get_references();
2986 # These get_*_list functions return one more to allow us to see if
2987 # there are more ...
2988 my @taglist = git_get_tags_list(16);
2989 my @headlist = git_get_heads_list(16);
2990 my @forklist;
2991 my ($check_forks) = gitweb_check_feature('forks');
2992
2993 if ($check_forks) {
2994 @forklist = git_get_projects_list($project);
2995 }
2996
2997 git_header_html();
2998 git_print_page_nav('summary','', $head);
2999
3000 print "<div class=\"title\">&nbsp;</div>\n";
3001 print "<table cellspacing=\"0\">\n" .
3002 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3003 "<tr><td>owner</td><td>$owner</td></tr>\n" .
3004 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3005 # use per project git URL list in $projectroot/$project/cloneurl
3006 # or make project git URL from git base URL and project name
3007 my $url_tag = "URL";
3008 my @url_list = git_get_project_url_list($project);
3009 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3010 foreach my $git_url (@url_list) {
3011 next unless $git_url;
3012 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3013 $url_tag = "";
3014 }
3015 print "</table>\n";
3016
3017 if (-s "$projectroot/$project/README.html") {
3018 if (open my $fd, "$projectroot/$project/README.html") {
3019 print "<div class=\"title\">readme</div>\n";
3020 print $_ while (<$fd>);
3021 close $fd;
3022 }
3023 }
3024
3025 # we need to request one more than 16 (0..15) to check if
3026 # those 16 are all
3027 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
3028 $head, "--"
3029 or die_error(undef, "Open git-rev-list failed");
3030 my @revlist = map { chomp; $_ } <$fd>;
3031 close $fd;
3032 git_print_header_div('shortlog');
3033 git_shortlog_body(\@revlist, 0, 15, $refs,
3034 $#revlist <= 15 ? undef :
3035 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3036
3037 if (@taglist) {
3038 git_print_header_div('tags');
3039 git_tags_body(\@taglist, 0, 15,
3040 $#taglist <= 15 ? undef :
3041 $cgi->a({-href => href(action=>"tags")}, "..."));
3042 }
3043
3044 if (@headlist) {
3045 git_print_header_div('heads');
3046 git_heads_body(\@headlist, $head, 0, 15,
3047 $#headlist <= 15 ? undef :
3048 $cgi->a({-href => href(action=>"heads")}, "..."));
3049 }
3050
3051 if (@forklist) {
3052 git_print_header_div('forks');
3053 git_project_list_body(\@forklist, undef, 0, 15,
3054 $#forklist <= 15 ? undef :
3055 $cgi->a({-href => href(action=>"forks")}, "..."),
3056 'noheader');
3057 }
3058
3059 git_footer_html();
3060 }
3061
3062 sub git_tag {
3063 my $head = git_get_head_hash($project);
3064 git_header_html();
3065 git_print_page_nav('','', $head,undef,$head);
3066 my %tag = parse_tag($hash);
3067 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3068 print "<div class=\"title_text\">\n" .
3069 "<table cellspacing=\"0\">\n" .
3070 "<tr>\n" .
3071 "<td>object</td>\n" .
3072 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3073 $tag{'object'}) . "</td>\n" .
3074 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3075 $tag{'type'}) . "</td>\n" .
3076 "</tr>\n";
3077 if (defined($tag{'author'})) {
3078 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3079 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3080 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3081 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3082 "</td></tr>\n";
3083 }
3084 print "</table>\n\n" .
3085 "</div>\n";
3086 print "<div class=\"page_body\">";
3087 my $comment = $tag{'comment'};
3088 foreach my $line (@$comment) {
3089 chomp $line;
3090 print esc_html($line, -nbsp=>1) . "<br/>\n";
3091 }
3092 print "</div>\n";
3093 git_footer_html();
3094 }
3095
3096 sub git_blame2 {
3097 my $fd;
3098 my $ftype;
3099
3100 my ($have_blame) = gitweb_check_feature('blame');
3101 if (!$have_blame) {
3102 die_error('403 Permission denied', "Permission denied");
3103 }
3104 die_error('404 Not Found', "File name not defined") if (!$file_name);
3105 $hash_base ||= git_get_head_hash($project);
3106 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3107 my %co = parse_commit($hash_base)
3108 or die_error(undef, "Reading commit failed");
3109 if (!defined $hash) {
3110 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3111 or die_error(undef, "Error looking up file");
3112 }
3113 $ftype = git_get_type($hash);
3114 if ($ftype !~ "blob") {
3115 die_error("400 Bad Request", "Object is not a blob");
3116 }
3117 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3118 $file_name, $hash_base)
3119 or die_error(undef, "Open git-blame failed");
3120 git_header_html();
3121 my $formats_nav =
3122 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3123 "blob") .
3124 " | " .
3125 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3126 "history") .
3127 " | " .
3128 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3129 "HEAD");
3130 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3131 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3132 git_print_page_path($file_name, $ftype, $hash_base);
3133 my @rev_color = (qw(light2 dark2));
3134 my $num_colors = scalar(@rev_color);
3135 my $current_color = 0;
3136 my $last_rev;
3137 print <<HTML;
3138 <div class="page_body">
3139 <table class="blame">
3140 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3141 HTML
3142 my %metainfo = ();
3143 while (1) {
3144 $_ = <$fd>;
3145 last unless defined $_;
3146 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3147 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3148 if (!exists $metainfo{$full_rev}) {
3149 $metainfo{$full_rev} = {};
3150 }
3151 my $meta = $metainfo{$full_rev};
3152 while (<$fd>) {
3153 last if (s/^\t//);
3154 if (/^(\S+) (.*)$/) {
3155 $meta->{$1} = $2;
3156 }
3157 }
3158 my $data = $_;
3159 chomp $data;
3160 my $rev = substr($full_rev, 0, 8);
3161 my $author = $meta->{'author'};
3162 my %date = parse_date($meta->{'author-time'},
3163 $meta->{'author-tz'});
3164 my $date = $date{'iso-tz'};
3165 if ($group_size) {
3166 $current_color = ++$current_color % $num_colors;
3167 }
3168 print "<tr class=\"$rev_color[$current_color]\">\n";
3169 if ($group_size) {
3170 print "<td class=\"sha1\"";
3171 print " title=\"". esc_html($author) . ", $date\"";
3172 print " rowspan=\"$group_size\"" if ($group_size > 1);
3173 print ">";
3174 print $cgi->a({-href => href(action=>"commit",
3175 hash=>$full_rev,
3176 file_name=>$file_name)},
3177 esc_html($rev));
3178 print "</td>\n";
3179 }
3180 my $blamed = href(action => 'blame',
3181 file_name => $meta->{'filename'},
3182 hash_base => $full_rev);
3183 print "<td class=\"linenr\">";
3184 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3185 -id => "l$lineno",
3186 -class => "linenr" },
3187 esc_html($lineno));
3188 print "</td>";
3189 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3190 print "</tr>\n";
3191 }
3192 print "</table>\n";
3193 print "</div>";
3194 close $fd
3195 or print "Reading blob failed\n";
3196 git_footer_html();
3197 }
3198
3199 sub git_blame {
3200 my $fd;
3201
3202 my ($have_blame) = gitweb_check_feature('blame');
3203 if (!$have_blame) {
3204 die_error('403 Permission denied', "Permission denied");
3205 }
3206 die_error('404 Not Found', "File name not defined") if (!$file_name);
3207 $hash_base ||= git_get_head_hash($project);
3208 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3209 my %co = parse_commit($hash_base)
3210 or die_error(undef, "Reading commit failed");
3211 if (!defined $hash) {
3212 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3213 or die_error(undef, "Error lookup file");
3214 }
3215 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3216 or die_error(undef, "Open git-annotate failed");
3217 git_header_html();
3218 my $formats_nav =
3219 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3220 "blob") .
3221 " | " .
3222 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3223 "history") .
3224 " | " .
3225 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3226 "HEAD");
3227 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3228 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3229 git_print_page_path($file_name, 'blob', $hash_base);
3230 print "<div class=\"page_body\">\n";
3231 print <<HTML;
3232 <table class="blame">
3233 <tr>
3234 <th>Commit</th>
3235 <th>Age</th>
3236 <th>Author</th>
3237 <th>Line</th>
3238 <th>Data</th>
3239 </tr>
3240 HTML
3241 my @line_class = (qw(light dark));
3242 my $line_class_len = scalar (@line_class);
3243 my $line_class_num = $#line_class;
3244 while (my $line = <$fd>) {
3245 my $long_rev;
3246 my $short_rev;
3247 my $author;
3248 my $time;
3249 my $lineno;
3250 my $data;
3251 my $age;
3252 my $age_str;
3253 my $age_class;
3254
3255 chomp $line;
3256 $line_class_num = ($line_class_num + 1) % $line_class_len;
3257
3258 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3259 $long_rev = $1;
3260 $author = $2;
3261 $time = $3;
3262 $lineno = $4;
3263 $data = $5;
3264 } else {
3265 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3266 next;
3267 }
3268 $short_rev = substr ($long_rev, 0, 8);
3269 $age = time () - $time;
3270 $age_str = age_string ($age);
3271 $age_str =~ s/ /&nbsp;/g;
3272 $age_class = age_class($age);
3273 $author = esc_html ($author);
3274 $author =~ s/ /&nbsp;/g;
3275
3276 $data = untabify($data);
3277 $data = esc_html ($data);
3278
3279 print <<HTML;
3280 <tr class="$line_class[$line_class_num]">
3281 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3282 <td class="$age_class">$age_str</td>
3283 <td>$author</td>
3284 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3285 <td class="pre">$data</td>
3286 </tr>
3287 HTML
3288 } # while (my $line = <$fd>)
3289 print "</table>\n\n";
3290 close $fd
3291 or print "Reading blob failed.\n";
3292 print "</div>";
3293 git_footer_html();
3294 }
3295
3296 sub git_tags {
3297 my $head = git_get_head_hash($project);
3298 git_header_html();
3299 git_print_page_nav('','', $head,undef,$head);
3300 git_print_header_div('summary', $project);
3301
3302 my @tagslist = git_get_tags_list();
3303 if (@tagslist) {
3304 git_tags_body(\@tagslist);
3305 }
3306 git_footer_html();
3307 }
3308
3309 sub git_heads {
3310 my $head = git_get_head_hash($project);
3311 git_header_html();
3312 git_print_page_nav('','', $head,undef,$head);
3313 git_print_header_div('summary', $project);
3314
3315 my @headslist = git_get_heads_list();
3316 if (@headslist) {
3317 git_heads_body(\@headslist, $head);
3318 }
3319 git_footer_html();
3320 }
3321
3322 sub git_blob_plain {
3323 my $expires;
3324
3325 if (!defined $hash) {
3326 if (defined $file_name) {
3327 my $base = $hash_base || git_get_head_hash($project);
3328 $hash = git_get_hash_by_path($base, $file_name, "blob")
3329 or die_error(undef, "Error lookup file");
3330 } else {
3331 die_error(undef, "No file name defined");
3332 }
3333 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3334 # blobs defined by non-textual hash id's can be cached
3335 $expires = "+1d";
3336 }
3337
3338 my $type = shift;
3339 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3340 or die_error(undef, "Couldn't cat $file_name, $hash");
3341
3342 $type ||= blob_mimetype($fd, $file_name);
3343
3344 # save as filename, even when no $file_name is given
3345 my $save_as = "$hash";
3346 if (defined $file_name) {
3347 $save_as = $file_name;
3348 } elsif ($type =~ m/^text\//) {
3349 $save_as .= '.txt';
3350 }
3351
3352 print $cgi->header(
3353 -type => "$type",
3354 -expires=>$expires,
3355 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3356 undef $/;
3357 binmode STDOUT, ':raw';
3358 print <$fd>;
3359 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3360 $/ = "\n";
3361 close $fd;
3362 }
3363
3364 sub git_blob {
3365 my $expires;
3366
3367 if (!defined $hash) {
3368 if (defined $file_name) {
3369 my $base = $hash_base || git_get_head_hash($project);
3370 $hash = git_get_hash_by_path($base, $file_name, "blob")
3371 or die_error(undef, "Error lookup file");
3372 } else {
3373 die_error(undef, "No file name defined");
3374 }
3375 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3376 # blobs defined by non-textual hash id's can be cached
3377 $expires = "+1d";
3378 }
3379
3380 my ($have_blame) = gitweb_check_feature('blame');
3381 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3382 or die_error(undef, "Couldn't cat $file_name, $hash");
3383 my $mimetype = blob_mimetype($fd, $file_name);
3384 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3385 close $fd;
3386 return git_blob_plain($mimetype);
3387 }
3388 # we can have blame only for text/* mimetype
3389 $have_blame &&= ($mimetype =~ m!^text/!);
3390
3391 git_header_html(undef, $expires);
3392 my $formats_nav = '';
3393 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3394 if (defined $file_name) {
3395 if ($have_blame) {
3396 $formats_nav .=
3397 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3398 hash=>$hash, file_name=>$file_name)},
3399 "blame") .
3400 " | ";
3401 }
3402 $formats_nav .=
3403 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3404 hash=>$hash, file_name=>$file_name)},
3405 "history") .
3406 " | " .
3407 $cgi->a({-href => href(action=>"blob_plain",
3408 hash=>$hash, file_name=>$file_name)},
3409 "raw") .
3410 " | " .
3411 $cgi->a({-href => href(action=>"blob",
3412 hash_base=>"HEAD", file_name=>$file_name)},
3413 "HEAD");
3414 } else {
3415 $formats_nav .=
3416 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3417 }
3418 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3419 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3420 } else {
3421 print "<div class=\"page_nav\">\n" .
3422 "<br/><br/></div>\n" .
3423 "<div class=\"title\">$hash</div>\n";
3424 }
3425 git_print_page_path($file_name, "blob", $hash_base);
3426 print "<div class=\"page_body\">\n";
3427 if ($mimetype =~ m!^text/!) {
3428 my $nr;
3429 while (my $line = <$fd>) {
3430 chomp $line;
3431 $nr++;
3432 $line = untabify($line);
3433 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3434 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3435 }
3436 } elsif ($mimetype =~ m!^image/!) {
3437 print qq!<img type="$mimetype"!;
3438 if ($file_name) {
3439 print qq! alt="$file_name" title="$file_name"!;
3440 }
3441 print qq! src="! .
3442 href(action=>"blob_plain", hash=>$hash,
3443 hash_base=>$hash_base, file_name=>$file_name) .
3444 qq!" />\n!;
3445 }
3446 close $fd
3447 or print "Reading blob failed.\n";
3448 print "</div>";
3449 git_footer_html();
3450 }
3451
3452 sub git_tree {
3453 my $have_snapshot = gitweb_have_snapshot();
3454
3455 if (!defined $hash_base) {
3456 $hash_base = "HEAD";
3457 }
3458 if (!defined $hash) {
3459 if (defined $file_name) {
3460 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3461 } else {
3462 $hash = $hash_base;
3463 }
3464 }
3465 $/ = "\0";
3466 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3467 or die_error(undef, "Open git-ls-tree failed");
3468 my @entries = map { chomp; $_ } <$fd>;
3469 close $fd or die_error(undef, "Reading tree failed");
3470 $/ = "\n";
3471
3472 my $refs = git_get_references();
3473 my $ref = format_ref_marker($refs, $hash_base);
3474 git_header_html();
3475 my $basedir = '';
3476 my ($have_blame) = gitweb_check_feature('blame');
3477 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3478 my @views_nav = ();
3479 if (defined $file_name) {
3480 push @views_nav,
3481 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3482 hash=>$hash, file_name=>$file_name)},
3483 "history"),
3484 $cgi->a({-href => href(action=>"tree",
3485 hash_base=>"HEAD", file_name=>$file_name)},
3486 "HEAD"),
3487 }
3488 if ($have_snapshot) {
3489 # FIXME: Should be available when we have no hash base as well.
3490 push @views_nav,
3491 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3492 "snapshot");
3493 }
3494 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3495 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3496 } else {
3497 undef $hash_base;
3498 print "<div class=\"page_nav\">\n";
3499 print "<br/><br/></div>\n";
3500 print "<div class=\"title\">$hash</div>\n";
3501 }
3502 if (defined $file_name) {
3503 $basedir = $file_name;
3504 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3505 $basedir .= '/';
3506 }
3507 }
3508 git_print_page_path($file_name, 'tree', $hash_base);
3509 print "<div class=\"page_body\">\n";
3510 print "<table cellspacing=\"0\">\n";
3511 my $alternate = 1;
3512 # '..' (top directory) link if possible
3513 if (defined $hash_base &&
3514 defined $file_name && $file_name =~ m![^/]+$!) {
3515 if ($alternate) {
3516 print "<tr class=\"dark\">\n";
3517 } else {
3518 print "<tr class=\"light\">\n";
3519 }
3520 $alternate ^= 1;
3521
3522 my $up = $file_name;
3523 $up =~ s!/?[^/]+$!!;
3524 undef $up unless $up;
3525 # based on git_print_tree_entry
3526 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3527 print '<td class="list">';
3528 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3529 file_name=>$up)},
3530 "..");
3531 print "</td>\n";
3532 print "<td class=\"link\"></td>\n";
3533
3534 print "</tr>\n";
3535 }
3536 foreach my $line (@entries) {
3537 my %t = parse_ls_tree_line($line, -z => 1);
3538
3539 if ($alternate) {
3540 print "<tr class=\"dark\">\n";
3541 } else {
3542 print "<tr class=\"light\">\n";
3543 }
3544 $alternate ^= 1;
3545
3546 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3547
3548 print "</tr>\n";
3549 }
3550 print "</table>\n" .
3551 "</div>";
3552 git_footer_html();
3553 }
3554
3555 sub git_snapshot {
3556 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3557 my $have_snapshot = (defined $ctype && defined $suffix);
3558 if (!$have_snapshot) {
3559 die_error('403 Permission denied', "Permission denied");
3560 }
3561
3562 if (!defined $hash) {
3563 $hash = git_get_head_hash($project);
3564 }
3565
3566 my $filename = basename($project) . "-$hash.tar.$suffix";
3567
3568 print $cgi->header(
3569 -type => "application/$ctype",
3570 -content_disposition => 'inline; filename="' . "$filename" . '"',
3571 -status => '200 OK');
3572
3573 my $git = git_cmd_str();
3574 my $name = $project;
3575 $name =~ s/\047/\047\\\047\047/g;
3576 open my $fd, "-|",
3577 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3578 or die_error(undef, "Execute git-tar-tree failed.");
3579 binmode STDOUT, ':raw';
3580 print <$fd>;
3581 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3582 close $fd;
3583
3584 }
3585
3586 sub git_log {
3587 my $head = git_get_head_hash($project);
3588 if (!defined $hash) {
3589 $hash = $head;
3590 }
3591 if (!defined $page) {
3592 $page = 0;
3593 }
3594 my $refs = git_get_references();
3595
3596 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3597 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3598 or die_error(undef, "Open git-rev-list failed");
3599 my @revlist = map { chomp; $_ } <$fd>;
3600 close $fd;
3601
3602 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3603
3604 git_header_html();
3605 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3606
3607 if (!@revlist) {
3608 my %co = parse_commit($hash);
3609
3610 git_print_header_div('summary', $project);
3611 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3612 }
3613 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3614 my $commit = $revlist[$i];
3615 my $ref = format_ref_marker($refs, $commit);
3616 my %co = parse_commit($commit);
3617 next if !%co;
3618 my %ad = parse_date($co{'author_epoch'});
3619 git_print_header_div('commit',
3620 "<span class=\"age\">$co{'age_string'}</span>" .
3621 esc_html($co{'title'}) . $ref,
3622 $commit);
3623 print "<div class=\"title_text\">\n" .
3624 "<div class=\"log_link\">\n" .
3625 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3626 " | " .
3627 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3628 " | " .
3629 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3630 "<br/>\n" .
3631 "</div>\n" .
3632 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3633 "</div>\n";
3634
3635 print "<div class=\"log_body\">\n";
3636 git_print_log($co{'comment'}, -final_empty_line=> 1);
3637 print "</div>\n";
3638 }
3639 git_footer_html();
3640 }
3641
3642 sub git_commit {
3643 $hash ||= $hash_base || "HEAD";
3644 my %co = parse_commit($hash);
3645 if (!%co) {
3646 die_error(undef, "Unknown commit object");
3647 }
3648 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3649 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3650
3651 my $parent = $co{'parent'};
3652 my $parents = $co{'parents'}; # listref
3653
3654 # we need to prepare $formats_nav before any parameter munging
3655 my $formats_nav;
3656 if (!defined $parent) {
3657 # --root commitdiff
3658 $formats_nav .= '(initial)';
3659 } elsif (@$parents == 1) {
3660 # single parent commit
3661 $formats_nav .=
3662 '(parent: ' .
3663 $cgi->a({-href => href(action=>"commit",
3664 hash=>$parent)},
3665 esc_html(substr($parent, 0, 7))) .
3666 ')';
3667 } else {
3668 # merge commit
3669 $formats_nav .=
3670 '(merge: ' .
3671 join(' ', map {
3672 $cgi->a({-href => href(action=>"commitdiff",
3673 hash=>$_)},
3674 esc_html(substr($_, 0, 7)));
3675 } @$parents ) .
3676 ')';
3677 }
3678
3679 if (!defined $parent) {
3680 $parent = "--root";
3681 }
3682 my @difftree;
3683 if (@$parents <= 1) {
3684 # difftree output is not printed for merges
3685 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3686 @diff_opts, $parent, $hash, "--"
3687 or die_error(undef, "Open git-diff-tree failed");
3688 @difftree = map { chomp; $_ } <$fd>;
3689 close $fd or die_error(undef, "Reading git-diff-tree failed");
3690 }
3691
3692 # non-textual hash id's can be cached
3693 my $expires;
3694 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3695 $expires = "+1d";
3696 }
3697 my $refs = git_get_references();
3698 my $ref = format_ref_marker($refs, $co{'id'});
3699
3700 my $have_snapshot = gitweb_have_snapshot();
3701
3702 git_header_html(undef, $expires);
3703 git_print_page_nav('commit', '',
3704 $hash, $co{'tree'}, $hash,
3705 $formats_nav);
3706
3707 if (defined $co{'parent'}) {
3708 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3709 } else {
3710 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3711 }
3712 print "<div class=\"title_text\">\n" .
3713 "<table cellspacing=\"0\">\n";
3714 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3715 "<tr>" .
3716 "<td></td><td> $ad{'rfc2822'}";
3717 if ($ad{'hour_local'} < 6) {
3718 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3719 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3720 } else {
3721 printf(" (%02d:%02d %s)",
3722 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3723 }
3724 print "</td>" .
3725 "</tr>\n";
3726 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3727 print "<tr><td></td><td> $cd{'rfc2822'}" .
3728 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3729 "</td></tr>\n";
3730 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3731 print "<tr>" .
3732 "<td>tree</td>" .
3733 "<td class=\"sha1\">" .
3734 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3735 class => "list"}, $co{'tree'}) .
3736 "</td>" .
3737 "<td class=\"link\">" .
3738 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3739 "tree");
3740 if ($have_snapshot) {
3741 print " | " .
3742 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3743 }
3744 print "</td>" .
3745 "</tr>\n";
3746
3747 foreach my $par (@$parents) {
3748 print "<tr>" .
3749 "<td>parent</td>" .
3750 "<td class=\"sha1\">" .
3751 $cgi->a({-href => href(action=>"commit", hash=>$par),
3752 class => "list"}, $par) .
3753 "</td>" .
3754 "<td class=\"link\">" .
3755 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3756 " | " .
3757 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3758 "</td>" .
3759 "</tr>\n";
3760 }
3761 print "</table>".
3762 "</div>\n";
3763
3764 print "<div class=\"page_body\">\n";
3765 git_print_log($co{'comment'});
3766 print "</div>\n";
3767
3768 if (@$parents <= 1) {
3769 # do not output difftree/whatchanged for merges
3770 git_difftree_body(\@difftree, $hash, $parent);
3771 }
3772
3773 git_footer_html();
3774 }
3775
3776 sub git_object {
3777 # object is defined by:
3778 # - hash or hash_base alone
3779 # - hash_base and file_name
3780 my $type;
3781
3782 # - hash or hash_base alone
3783 if ($hash || ($hash_base && !defined $file_name)) {
3784 my $object_id = $hash || $hash_base;
3785
3786 my $git_command = git_cmd_str();
3787 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
3788 or die_error('404 Not Found', "Object does not exist");
3789 $type = <$fd>;
3790 chomp $type;
3791 close $fd
3792 or die_error('404 Not Found', "Object does not exist");
3793
3794 # - hash_base and file_name
3795 } elsif ($hash_base && defined $file_name) {
3796 $file_name =~ s,/+$,,;
3797
3798 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
3799 or die_error('404 Not Found', "Base object does not exist");
3800
3801 # here errors should not hapen
3802 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
3803 or die_error(undef, "Open git-ls-tree failed");
3804 my $line = <$fd>;
3805 close $fd;
3806
3807 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3808 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
3809 die_error('404 Not Found', "File or directory for given base does not exist");
3810 }
3811 $type = $2;
3812 $hash = $3;
3813 } else {
3814 die_error('404 Not Found', "Not enough information to find object");
3815 }
3816
3817 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
3818 hash=>$hash, hash_base=>$hash_base,
3819 file_name=>$file_name),
3820 -status => '302 Found');
3821 }
3822
3823 sub git_blobdiff {
3824 my $format = shift || 'html';
3825
3826 my $fd;
3827 my @difftree;
3828 my %diffinfo;
3829 my $expires;
3830
3831 # preparing $fd and %diffinfo for git_patchset_body
3832 # new style URI
3833 if (defined $hash_base && defined $hash_parent_base) {
3834 if (defined $file_name) {
3835 # read raw output
3836 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3837 $hash_parent_base, $hash_base,
3838 "--", $file_name
3839 or die_error(undef, "Open git-diff-tree failed");
3840 @difftree = map { chomp; $_ } <$fd>;
3841 close $fd
3842 or die_error(undef, "Reading git-diff-tree failed");
3843 @difftree
3844 or die_error('404 Not Found', "Blob diff not found");
3845
3846 } elsif (defined $hash &&
3847 $hash =~ /[0-9a-fA-F]{40}/) {
3848 # try to find filename from $hash
3849
3850 # read filtered raw output
3851 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3852 $hash_parent_base, $hash_base, "--"
3853 or die_error(undef, "Open git-diff-tree failed");
3854 @difftree =
3855 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3856 # $hash == to_id
3857 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3858 map { chomp; $_ } <$fd>;
3859 close $fd
3860 or die_error(undef, "Reading git-diff-tree failed");
3861 @difftree
3862 or die_error('404 Not Found', "Blob diff not found");
3863
3864 } else {
3865 die_error('404 Not Found', "Missing one of the blob diff parameters");
3866 }
3867
3868 if (@difftree > 1) {
3869 die_error('404 Not Found', "Ambiguous blob diff specification");
3870 }
3871
3872 %diffinfo = parse_difftree_raw_line($difftree[0]);
3873 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3874 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3875
3876 $hash_parent ||= $diffinfo{'from_id'};
3877 $hash ||= $diffinfo{'to_id'};
3878
3879 # non-textual hash id's can be cached
3880 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3881 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3882 $expires = '+1d';
3883 }
3884
3885 # open patch output
3886 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3887 '-p', $hash_parent_base, $hash_base,
3888 "--", $file_name
3889 or die_error(undef, "Open git-diff-tree failed");
3890 }
3891
3892 # old/legacy style URI
3893 if (!%diffinfo && # if new style URI failed
3894 defined $hash && defined $hash_parent) {
3895 # fake git-diff-tree raw output
3896 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3897 $diffinfo{'from_id'} = $hash_parent;
3898 $diffinfo{'to_id'} = $hash;
3899 if (defined $file_name) {
3900 if (defined $file_parent) {
3901 $diffinfo{'status'} = '2';
3902 $diffinfo{'from_file'} = $file_parent;
3903 $diffinfo{'to_file'} = $file_name;
3904 } else { # assume not renamed
3905 $diffinfo{'status'} = '1';
3906 $diffinfo{'from_file'} = $file_name;
3907 $diffinfo{'to_file'} = $file_name;
3908 }
3909 } else { # no filename given
3910 $diffinfo{'status'} = '2';
3911 $diffinfo{'from_file'} = $hash_parent;
3912 $diffinfo{'to_file'} = $hash;
3913 }
3914
3915 # non-textual hash id's can be cached
3916 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3917 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3918 $expires = '+1d';
3919 }
3920
3921 # open patch output
3922 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts,
3923 $hash_parent, $hash, "--"
3924 or die_error(undef, "Open git-diff failed");
3925 } else {
3926 die_error('404 Not Found', "Missing one of the blob diff parameters")
3927 unless %diffinfo;
3928 }
3929
3930 # header
3931 if ($format eq 'html') {
3932 my $formats_nav =
3933 $cgi->a({-href => href(action=>"blobdiff_plain",
3934 hash=>$hash, hash_parent=>$hash_parent,
3935 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3936 file_name=>$file_name, file_parent=>$file_parent)},
3937 "raw");
3938 git_header_html(undef, $expires);
3939 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3940 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3941 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3942 } else {
3943 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3944 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3945 }
3946 if (defined $file_name) {
3947 git_print_page_path($file_name, "blob", $hash_base);
3948 } else {
3949 print "<div class=\"page_path\"></div>\n";
3950 }
3951
3952 } elsif ($format eq 'plain') {
3953 print $cgi->header(
3954 -type => 'text/plain',
3955 -charset => 'utf-8',
3956 -expires => $expires,
3957 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3958
3959 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3960
3961 } else {
3962 die_error(undef, "Unknown blobdiff format");
3963 }
3964
3965 # patch
3966 if ($format eq 'html') {
3967 print "<div class=\"page_body\">\n";
3968
3969 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3970 close $fd;
3971
3972 print "</div>\n"; # class="page_body"
3973 git_footer_html();
3974
3975 } else {
3976 while (my $line = <$fd>) {
3977 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
3978 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
3979
3980 print $line;
3981
3982 last if $line =~ m!^\+\+\+!;
3983 }
3984 local $/ = undef;
3985 print <$fd>;
3986 close $fd;
3987 }
3988 }
3989
3990 sub git_blobdiff_plain {
3991 git_blobdiff('plain');
3992 }
3993
3994 sub git_commitdiff {
3995 my $format = shift || 'html';
3996 $hash ||= $hash_base || "HEAD";
3997 my %co = parse_commit($hash);
3998 if (!%co) {
3999 die_error(undef, "Unknown commit object");
4000 }
4001
4002 # we need to prepare $formats_nav before any parameter munging
4003 my $formats_nav;
4004 if ($format eq 'html') {
4005 $formats_nav =
4006 $cgi->a({-href => href(action=>"commitdiff_plain",
4007 hash=>$hash, hash_parent=>$hash_parent)},
4008 "raw");
4009
4010 if (defined $hash_parent) {
4011 # commitdiff with two commits given
4012 my $hash_parent_short = $hash_parent;
4013 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4014 $hash_parent_short = substr($hash_parent, 0, 7);
4015 }
4016 $formats_nav .=
4017 ' (from: ' .
4018 $cgi->a({-href => href(action=>"commitdiff",
4019 hash=>$hash_parent)},
4020 esc_html($hash_parent_short)) .
4021 ')';
4022 } elsif (!$co{'parent'}) {
4023 # --root commitdiff
4024 $formats_nav .= ' (initial)';
4025 } elsif (scalar @{$co{'parents'}} == 1) {
4026 # single parent commit
4027 $formats_nav .=
4028 ' (parent: ' .
4029 $cgi->a({-href => href(action=>"commitdiff",
4030 hash=>$co{'parent'})},
4031 esc_html(substr($co{'parent'}, 0, 7))) .
4032 ')';
4033 } else {
4034 # merge commit
4035 $formats_nav .=
4036 ' (merge: ' .
4037 join(' ', map {
4038 $cgi->a({-href => href(action=>"commitdiff",
4039 hash=>$_)},
4040 esc_html(substr($_, 0, 7)));
4041 } @{$co{'parents'}} ) .
4042 ')';
4043 }
4044 }
4045
4046 if (!defined $hash_parent) {
4047 $hash_parent = $co{'parent'} || '--root';
4048 }
4049
4050 # read commitdiff
4051 my $fd;
4052 my @difftree;
4053 if ($format eq 'html') {
4054 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4055 "--no-commit-id", "--patch-with-raw", "--full-index",
4056 $hash_parent, $hash, "--"
4057 or die_error(undef, "Open git-diff-tree failed");
4058
4059 while (my $line = <$fd>) {
4060 chomp $line;
4061 # empty line ends raw part of diff-tree output
4062 last unless $line;
4063 push @difftree, $line;
4064 }
4065
4066 } elsif ($format eq 'plain') {
4067 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4068 '-p', $hash_parent, $hash, "--"
4069 or die_error(undef, "Open git-diff-tree failed");
4070
4071 } else {
4072 die_error(undef, "Unknown commitdiff format");
4073 }
4074
4075 # non-textual hash id's can be cached
4076 my $expires;
4077 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4078 $expires = "+1d";
4079 }
4080
4081 # write commit message
4082 if ($format eq 'html') {
4083 my $refs = git_get_references();
4084 my $ref = format_ref_marker($refs, $co{'id'});
4085
4086 git_header_html(undef, $expires);
4087 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4088 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4089 git_print_authorship(\%co);
4090 print "<div class=\"page_body\">\n";
4091 if (@{$co{'comment'}} > 1) {
4092 print "<div class=\"log\">\n";
4093 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4094 print "</div>\n"; # class="log"
4095 }
4096
4097 } elsif ($format eq 'plain') {
4098 my $refs = git_get_references("tags");
4099 my $tagname = git_get_rev_name_tags($hash);
4100 my $filename = basename($project) . "-$hash.patch";
4101
4102 print $cgi->header(
4103 -type => 'text/plain',
4104 -charset => 'utf-8',
4105 -expires => $expires,
4106 -content_disposition => 'inline; filename="' . "$filename" . '"');
4107 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4108 print <<TEXT;
4109 From: $co{'author'}
4110 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4111 Subject: $co{'title'}
4112 TEXT
4113 print "X-Git-Tag: $tagname\n" if $tagname;
4114 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4115
4116 foreach my $line (@{$co{'comment'}}) {
4117 print "$line\n";
4118 }
4119 print "---\n\n";
4120 }
4121
4122 # write patch
4123 if ($format eq 'html') {
4124 git_difftree_body(\@difftree, $hash, $hash_parent);
4125 print "<br/>\n";
4126
4127 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
4128 close $fd;
4129 print "</div>\n"; # class="page_body"
4130 git_footer_html();
4131
4132 } elsif ($format eq 'plain') {
4133 local $/ = undef;
4134 print <$fd>;
4135 close $fd
4136 or print "Reading git-diff-tree failed\n";
4137 }
4138 }
4139
4140 sub git_commitdiff_plain {
4141 git_commitdiff('plain');
4142 }
4143
4144 sub git_history {
4145 if (!defined $hash_base) {
4146 $hash_base = git_get_head_hash($project);
4147 }
4148 if (!defined $page) {
4149 $page = 0;
4150 }
4151 my $ftype;
4152 my %co = parse_commit($hash_base);
4153 if (!%co) {
4154 die_error(undef, "Unknown commit object");
4155 }
4156
4157 my $refs = git_get_references();
4158 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4159
4160 if (!defined $hash && defined $file_name) {
4161 $hash = git_get_hash_by_path($hash_base, $file_name);
4162 }
4163 if (defined $hash) {
4164 $ftype = git_get_type($hash);
4165 }
4166
4167 open my $fd, "-|",
4168 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
4169 or die_error(undef, "Open git-rev-list-failed");
4170 my @revlist = map { chomp; $_ } <$fd>;
4171 close $fd
4172 or die_error(undef, "Reading git-rev-list failed");
4173
4174 my $paging_nav = '';
4175 if ($page > 0) {
4176 $paging_nav .=
4177 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4178 file_name=>$file_name)},
4179 "first");
4180 $paging_nav .= " &sdot; " .
4181 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4182 file_name=>$file_name, page=>$page-1),
4183 -accesskey => "p", -title => "Alt-p"}, "prev");
4184 } else {
4185 $paging_nav .= "first";
4186 $paging_nav .= " &sdot; prev";
4187 }
4188 if ($#revlist >= (100 * ($page+1)-1)) {
4189 $paging_nav .= " &sdot; " .
4190 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4191 file_name=>$file_name, page=>$page+1),
4192 -accesskey => "n", -title => "Alt-n"}, "next");
4193 } else {
4194 $paging_nav .= " &sdot; next";
4195 }
4196 my $next_link = '';
4197 if ($#revlist >= (100 * ($page+1)-1)) {
4198 $next_link =
4199 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4200 file_name=>$file_name, page=>$page+1),
4201 -title => "Alt-n"}, "next");
4202 }
4203
4204 git_header_html();
4205 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4206 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4207 git_print_page_path($file_name, $ftype, $hash_base);
4208
4209 git_history_body(\@revlist, ($page * 100), $#revlist,
4210 $refs, $hash_base, $ftype, $next_link);
4211
4212 git_footer_html();
4213 }
4214
4215 sub git_search {
4216 my ($have_search) = gitweb_check_feature('search');
4217 if (!$have_search) {
4218 die_error('403 Permission denied', "Permission denied");
4219 }
4220 if (!defined $searchtext) {
4221 die_error(undef, "Text field empty");
4222 }
4223 if (!defined $hash) {
4224 $hash = git_get_head_hash($project);
4225 }
4226 my %co = parse_commit($hash);
4227 if (!%co) {
4228 die_error(undef, "Unknown commit object");
4229 }
4230 if (!defined $page) {
4231 $page = 0;
4232 }
4233
4234 $searchtype ||= 'commit';
4235 if ($searchtype eq 'pickaxe') {
4236 # pickaxe may take all resources of your box and run for several minutes
4237 # with every query - so decide by yourself how public you make this feature
4238 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4239 if (!$have_pickaxe) {
4240 die_error('403 Permission denied', "Permission denied");
4241 }
4242 }
4243
4244 git_header_html();
4245
4246 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4247 my $greptype;
4248 if ($searchtype eq 'commit') {
4249 $greptype = "--grep=";
4250 } elsif ($searchtype eq 'author') {
4251 $greptype = "--author=";
4252 } elsif ($searchtype eq 'committer') {
4253 $greptype = "--committer=";
4254 }
4255 open my $fd, "-|", git_cmd(), "rev-list",
4256 ("--max-count=" . (100 * ($page+1))),
4257 ($greptype . $searchtext),
4258 $hash, "--"
4259 or next;
4260 my @revlist = map { chomp; $_ } <$fd>;
4261 close $fd;
4262
4263 my $paging_nav = '';
4264 if ($page > 0) {
4265 $paging_nav .=
4266 $cgi->a({-href => href(action=>"search", hash=>$hash,
4267 searchtext=>$searchtext, searchtype=>$searchtype)},
4268 "first");
4269 $paging_nav .= " &sdot; " .
4270 $cgi->a({-href => href(action=>"search", hash=>$hash,
4271 searchtext=>$searchtext, searchtype=>$searchtype,
4272 page=>$page-1),
4273 -accesskey => "p", -title => "Alt-p"}, "prev");
4274 } else {
4275 $paging_nav .= "first";
4276 $paging_nav .= " &sdot; prev";
4277 }
4278 if ($#revlist >= (100 * ($page+1)-1)) {
4279 $paging_nav .= " &sdot; " .
4280 $cgi->a({-href => href(action=>"search", hash=>$hash,
4281 searchtext=>$searchtext, searchtype=>$searchtype,
4282 page=>$page+1),
4283 -accesskey => "n", -title => "Alt-n"}, "next");
4284 } else {
4285 $paging_nav .= " &sdot; next";
4286 }
4287 my $next_link = '';
4288 if ($#revlist >= (100 * ($page+1)-1)) {
4289 $next_link =
4290 $cgi->a({-href => href(action=>"search", hash=>$hash,
4291 searchtext=>$searchtext, searchtype=>$searchtype,
4292 page=>$page+1),
4293 -accesskey => "n", -title => "Alt-n"}, "next");
4294 }
4295
4296 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4297 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4298 git_search_grep_body(\@revlist, ($page * 100), $#revlist, $next_link);
4299 }
4300
4301 if ($searchtype eq 'pickaxe') {
4302 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4303 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4304
4305 print "<table cellspacing=\"0\">\n";
4306 my $alternate = 1;
4307 $/ = "\n";
4308 my $git_command = git_cmd_str();
4309 open my $fd, "-|", "$git_command rev-list $hash | " .
4310 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4311 undef %co;
4312 my @files;
4313 while (my $line = <$fd>) {
4314 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4315 my %set;
4316 $set{'file'} = $6;
4317 $set{'from_id'} = $3;
4318 $set{'to_id'} = $4;
4319 $set{'id'} = $set{'to_id'};
4320 if ($set{'id'} =~ m/0{40}/) {
4321 $set{'id'} = $set{'from_id'};
4322 }
4323 if ($set{'id'} =~ m/0{40}/) {
4324 next;
4325 }
4326 push @files, \%set;
4327 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4328 if (%co) {
4329 if ($alternate) {
4330 print "<tr class=\"dark\">\n";
4331 } else {
4332 print "<tr class=\"light\">\n";
4333 }
4334 $alternate ^= 1;
4335 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4336 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4337 "<td>" .
4338 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4339 -class => "list subject"},
4340 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4341 while (my $setref = shift @files) {
4342 my %set = %$setref;
4343 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4344 hash=>$set{'id'}, file_name=>$set{'file'}),
4345 -class => "list"},
4346 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4347 "<br/>\n";
4348 }
4349 print "</td>\n" .
4350 "<td class=\"link\">" .
4351 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4352 " | " .
4353 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4354 print "</td>\n" .
4355 "</tr>\n";
4356 }
4357 %co = parse_commit($1);
4358 }
4359 }
4360 close $fd;
4361
4362 print "</table>\n";
4363 }
4364 git_footer_html();
4365 }
4366
4367 sub git_search_help {
4368 git_header_html();
4369 git_print_page_nav('','', $hash,$hash,$hash);
4370 print <<EOT;
4371 <dl>
4372 <dt><b>commit</b></dt>
4373 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4374 <dt><b>author</b></dt>
4375 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4376 <dt><b>committer</b></dt>
4377 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4378 EOT
4379 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4380 if ($have_pickaxe) {
4381 print <<EOT;
4382 <dt><b>pickaxe</b></dt>
4383 <dd>All commits that caused the string to appear or disappear from any file (changes that
4384 added, removed or "modified" the string) will be listed. This search can take a while and
4385 takes a lot of strain on the server, so please use it wisely.</dd>
4386 EOT
4387 }
4388 print "</dl>\n";
4389 git_footer_html();
4390 }
4391
4392 sub git_shortlog {
4393 my $head = git_get_head_hash($project);
4394 if (!defined $hash) {
4395 $hash = $head;
4396 }
4397 if (!defined $page) {
4398 $page = 0;
4399 }
4400 my $refs = git_get_references();
4401
4402 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4403 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
4404 or die_error(undef, "Open git-rev-list failed");
4405 my @revlist = map { chomp; $_ } <$fd>;
4406 close $fd;
4407
4408 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
4409 my $next_link = '';
4410 if ($#revlist >= (100 * ($page+1)-1)) {
4411 $next_link =
4412 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4413 -title => "Alt-n"}, "next");
4414 }
4415
4416
4417 git_header_html();
4418 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4419 git_print_header_div('summary', $project);
4420
4421 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
4422
4423 git_footer_html();
4424 }
4425
4426 ## ......................................................................
4427 ## feeds (RSS, Atom; OPML)
4428
4429 sub git_feed {
4430 my $format = shift || 'atom';
4431 my ($have_blame) = gitweb_check_feature('blame');
4432
4433 # Atom: http://www.atomenabled.org/developers/syndication/
4434 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4435 if ($format ne 'rss' && $format ne 'atom') {
4436 die_error(undef, "Unknown web feed format");
4437 }
4438
4439 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4440 my $head = $hash || 'HEAD';
4441 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150",
4442 $head, "--", (defined $file_name ? $file_name : ())
4443 or die_error(undef, "Open git-rev-list failed");
4444 my @revlist = map { chomp; $_ } <$fd>;
4445 close $fd or die_error(undef, "Reading git-rev-list failed");
4446
4447 my %latest_commit;
4448 my %latest_date;
4449 my $content_type = "application/$format+xml";
4450 if (defined $cgi->http('HTTP_ACCEPT') &&
4451 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4452 # browser (feed reader) prefers text/xml
4453 $content_type = 'text/xml';
4454 }
4455 if (defined($revlist[0])) {
4456 %latest_commit = parse_commit($revlist[0]);
4457 %latest_date = parse_date($latest_commit{'author_epoch'});
4458 print $cgi->header(
4459 -type => $content_type,
4460 -charset => 'utf-8',
4461 -last_modified => $latest_date{'rfc2822'});
4462 } else {
4463 print $cgi->header(
4464 -type => $content_type,
4465 -charset => 'utf-8');
4466 }
4467
4468 # Optimization: skip generating the body if client asks only
4469 # for Last-Modified date.
4470 return if ($cgi->request_method() eq 'HEAD');
4471
4472 # header variables
4473 my $title = "$site_name - $project/$action";
4474 my $feed_type = 'log';
4475 if (defined $hash) {
4476 $title .= " - '$hash'";
4477 $feed_type = 'branch log';
4478 if (defined $file_name) {
4479 $title .= " :: $file_name";
4480 $feed_type = 'history';
4481 }
4482 } elsif (defined $file_name) {
4483 $title .= " - $file_name";
4484 $feed_type = 'history';
4485 }
4486 $title .= " $feed_type";
4487 my $descr = git_get_project_description($project);
4488 if (defined $descr) {
4489 $descr = esc_html($descr);
4490 } else {
4491 $descr = "$project " .
4492 ($format eq 'rss' ? 'RSS' : 'Atom') .
4493 " feed";
4494 }
4495 my $owner = git_get_project_owner($project);
4496 $owner = esc_html($owner);
4497
4498 #header
4499 my $alt_url;
4500 if (defined $file_name) {
4501 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4502 } elsif (defined $hash) {
4503 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4504 } else {
4505 $alt_url = href(-full=>1, action=>"summary");
4506 }
4507 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4508 if ($format eq 'rss') {
4509 print <<XML;
4510 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4511 <channel>
4512 XML
4513 print "<title>$title</title>\n" .
4514 "<link>$alt_url</link>\n" .
4515 "<description>$descr</description>\n" .
4516 "<language>en</language>\n";
4517 } elsif ($format eq 'atom') {
4518 print <<XML;
4519 <feed xmlns="http://www.w3.org/2005/Atom">
4520 XML
4521 print "<title>$title</title>\n" .
4522 "<subtitle>$descr</subtitle>\n" .
4523 '<link rel="alternate" type="text/html" href="' .
4524 $alt_url . '" />' . "\n" .
4525 '<link rel="self" type="' . $content_type . '" href="' .
4526 $cgi->self_url() . '" />' . "\n" .
4527 "<id>" . href(-full=>1) . "</id>\n" .
4528 # use project owner for feed author
4529 "<author><name>$owner</name></author>\n";
4530 if (defined $favicon) {
4531 print "<icon>" . esc_url($favicon) . "</icon>\n";
4532 }
4533 if (defined $logo_url) {
4534 # not twice as wide as tall: 72 x 27 pixels
4535 print "<logo>" . esc_url($logo) . "</logo>\n";
4536 }
4537 if (! %latest_date) {
4538 # dummy date to keep the feed valid until commits trickle in:
4539 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4540 } else {
4541 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4542 }
4543 }
4544
4545 # contents
4546 for (my $i = 0; $i <= $#revlist; $i++) {
4547 my $commit = $revlist[$i];
4548 my %co = parse_commit($commit);
4549 # we read 150, we always show 30 and the ones more recent than 48 hours
4550 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4551 last;
4552 }
4553 my %cd = parse_date($co{'author_epoch'});
4554
4555 # get list of changed files
4556 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4557 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4558 or next;
4559 my @difftree = map { chomp; $_ } <$fd>;
4560 close $fd
4561 or next;
4562
4563 # print element (entry, item)
4564 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4565 if ($format eq 'rss') {
4566 print "<item>\n" .
4567 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4568 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4569 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4570 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4571 "<link>$co_url</link>\n" .
4572 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4573 "<content:encoded>" .
4574 "<![CDATA[\n";
4575 } elsif ($format eq 'atom') {
4576 print "<entry>\n" .
4577 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4578 "<updated>$cd{'iso-8601'}</updated>\n" .
4579 "<author>\n" .
4580 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4581 if ($co{'author_email'}) {
4582 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4583 }
4584 print "</author>\n" .
4585 # use committer for contributor
4586 "<contributor>\n" .
4587 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4588 if ($co{'committer_email'}) {
4589 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4590 }
4591 print "</contributor>\n" .
4592 "<published>$cd{'iso-8601'}</published>\n" .
4593 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4594 "<id>$co_url</id>\n" .
4595 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4596 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4597 }
4598 my $comment = $co{'comment'};
4599 print "<pre>\n";
4600 foreach my $line (@$comment) {
4601 $line = esc_html($line);
4602 print "$line\n";
4603 }
4604 print "</pre><ul>\n";
4605 foreach my $difftree_line (@difftree) {
4606 my %difftree = parse_difftree_raw_line($difftree_line);
4607 next if !$difftree{'from_id'};
4608
4609 my $file = $difftree{'file'} || $difftree{'to_file'};
4610
4611 print "<li>" .
4612 "[" .
4613 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4614 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4615 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4616 file_name=>$file, file_parent=>$difftree{'from_file'}),
4617 -title => "diff"}, 'D');
4618 if ($have_blame) {
4619 print $cgi->a({-href => href(-full=>1, action=>"blame",
4620 file_name=>$file, hash_base=>$commit),
4621 -title => "blame"}, 'B');
4622 }
4623 # if this is not a feed of a file history
4624 if (!defined $file_name || $file_name ne $file) {
4625 print $cgi->a({-href => href(-full=>1, action=>"history",
4626 file_name=>$file, hash=>$commit),
4627 -title => "history"}, 'H');
4628 }
4629 $file = esc_path($file);
4630 print "] ".
4631 "$file</li>\n";
4632 }
4633 if ($format eq 'rss') {
4634 print "</ul>]]>\n" .
4635 "</content:encoded>\n" .
4636 "</item>\n";
4637 } elsif ($format eq 'atom') {
4638 print "</ul>\n</div>\n" .
4639 "</content>\n" .
4640 "</entry>\n";
4641 }
4642 }
4643
4644 # end of feed
4645 if ($format eq 'rss') {
4646 print "</channel>\n</rss>\n";
4647 } elsif ($format eq 'atom') {
4648 print "</feed>\n";
4649 }
4650 }
4651
4652 sub git_rss {
4653 git_feed('rss');
4654 }
4655
4656 sub git_atom {
4657 git_feed('atom');
4658 }
4659
4660 sub git_opml {
4661 my @list = git_get_projects_list();
4662
4663 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4664 print <<XML;
4665 <?xml version="1.0" encoding="utf-8"?>
4666 <opml version="1.0">
4667 <head>
4668 <title>$site_name OPML Export</title>
4669 </head>
4670 <body>
4671 <outline text="git RSS feeds">
4672 XML
4673
4674 foreach my $pr (@list) {
4675 my %proj = %$pr;
4676 my $head = git_get_head_hash($proj{'path'});
4677 if (!defined $head) {
4678 next;
4679 }
4680 $git_dir = "$projectroot/$proj{'path'}";
4681 my %co = parse_commit($head);
4682 if (!%co) {
4683 next;
4684 }
4685
4686 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
4687 my $rss = "$my_url?p=$proj{'path'};a=rss";
4688 my $html = "$my_url?p=$proj{'path'};a=summary";
4689 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
4690 }
4691 print <<XML;
4692 </outline>
4693 </body>
4694 </opml>
4695 XML
4696 }
This page took 4.241254 seconds and 5 git commands to generate.