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