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