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