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