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