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