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