]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Allow configuring the default projects order and add order 'none'
[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_line;
2403 my $diffinfo;
2404 my (%from, %to);
2405
2406 print "<div class=\"patchset\">\n";
2407
2408 # skip to first patch
2409 while ($patch_line = <$fd>) {
2410 chomp $patch_line;
2411
2412 last if ($patch_line =~ m/^diff /);
2413 }
2414
2415 PATCH:
2416 while ($patch_line) {
2417 my @diff_header;
2418 my ($from_id, $to_id);
2419
2420 # git diff header
2421 #assert($patch_line =~ m/^diff /) if DEBUG;
2422 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2423 push @diff_header, $patch_line;
2424
2425 # extended diff header
2426 EXTENDED_HEADER:
2427 while ($patch_line = <$fd>) {
2428 chomp $patch_line;
2429
2430 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2431
2432 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2433 $from_id = $1;
2434 $to_id = $2;
2435 }
2436
2437 push @diff_header, $patch_line;
2438 }
2439 my $last_patch_line = $patch_line;
2440
2441 # check if current patch belong to current raw line
2442 # and parse raw git-diff line if needed
2443 if (defined $diffinfo &&
2444 $diffinfo->{'from_id'} eq $from_id &&
2445 $diffinfo->{'to_id'} eq $to_id) {
2446 # this is split patch
2447 print "<div class=\"patch cont\">\n";
2448 } else {
2449 # advance raw git-diff output if needed
2450 $patch_idx++ if defined $diffinfo;
2451
2452 # read and prepare patch information
2453 if (ref($difftree->[$patch_idx]) eq "HASH") {
2454 # pre-parsed (or generated by hand)
2455 $diffinfo = $difftree->[$patch_idx];
2456 } else {
2457 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2458 }
2459 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2460 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2461 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2462 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2463 hash=>$diffinfo->{'from_id'},
2464 file_name=>$from{'file'});
2465 } else {
2466 delete $from{'href'};
2467 }
2468 if ($diffinfo->{'status'} ne "D") { # not deleted file
2469 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2470 hash=>$diffinfo->{'to_id'},
2471 file_name=>$to{'file'});
2472 } else {
2473 delete $to{'href'};
2474 }
2475 # this is first patch for raw difftree line with $patch_idx index
2476 # we index @$difftree array from 0, but number patches from 1
2477 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2478 }
2479
2480 # print "git diff" header
2481 $patch_line = shift @diff_header;
2482 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2483 if ($from{'href'}) {
2484 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2485 'a/' . esc_path($from{'file'}));
2486 } else { # file was added
2487 $patch_line .= 'a/' . esc_path($from{'file'});
2488 }
2489 $patch_line .= ' ';
2490 if ($to{'href'}) {
2491 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2492 'b/' . esc_path($to{'file'}));
2493 } else { # file was deleted
2494 $patch_line .= 'b/' . esc_path($to{'file'});
2495 }
2496 print "<div class=\"diff header\">$patch_line</div>\n";
2497
2498 # print extended diff header
2499 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2500 EXTENDED_HEADER:
2501 foreach $patch_line (@diff_header) {
2502 # match <path>
2503 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2504 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2505 esc_path($from{'file'}));
2506 }
2507 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2508 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2509 esc_path($to{'file'}));
2510 }
2511 # match <mode>
2512 if ($patch_line =~ m/\s(\d{6})$/) {
2513 $patch_line .= '<span class="info"> (' .
2514 file_type_long($1) .
2515 ')</span>';
2516 }
2517 # match <hash>
2518 if ($patch_line =~ m/^index/) {
2519 my ($from_link, $to_link);
2520 if ($from{'href'}) {
2521 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2522 substr($diffinfo->{'from_id'},0,7));
2523 } else {
2524 $from_link = '0' x 7;
2525 }
2526 if ($to{'href'}) {
2527 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2528 substr($diffinfo->{'to_id'},0,7));
2529 } else {
2530 $to_link = '0' x 7;
2531 }
2532 #affirm {
2533 # my ($from_hash, $to_hash) =
2534 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2535 # my ($from_id, $to_id) =
2536 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2537 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2538 #} if DEBUG;
2539 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2540 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2541 }
2542 print $patch_line . "<br/>\n";
2543 }
2544 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2545
2546 # from-file/to-file diff header
2547 $patch_line = $last_patch_line;
2548 if (! $patch_line) {
2549 print "</div>\n"; # class="patch"
2550 last PATCH;
2551 }
2552 next PATCH if ($patch_line =~ m/^diff /);
2553 #assert($patch_line =~ m/^---/) if DEBUG;
2554 if ($from{'href'} && $patch_line =~ m!^--- "?a/!) {
2555 $patch_line = '--- a/' .
2556 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2557 esc_path($from{'file'}));
2558 }
2559 print "<div class=\"diff from_file\">$patch_line</div>\n";
2560
2561 $patch_line = <$fd>;
2562 chomp $patch_line;
2563
2564 #assert($patch_line =~ m/^+++/) if DEBUG;
2565 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2566 $patch_line = '+++ b/' .
2567 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2568 esc_path($to{'file'}));
2569 }
2570 print "<div class=\"diff to_file\">$patch_line</div>\n";
2571
2572 # the patch itself
2573 LINE:
2574 while ($patch_line = <$fd>) {
2575 chomp $patch_line;
2576
2577 next PATCH if ($patch_line =~ m/^diff /);
2578
2579 print format_diff_line($patch_line, \%from, \%to);
2580 }
2581
2582 } continue {
2583 print "</div>\n"; # class="patch"
2584 }
2585
2586 print "</div>\n"; # class="patchset"
2587 }
2588
2589 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2590
2591 sub git_project_list_body {
2592 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2593
2594 my ($check_forks) = gitweb_check_feature('forks');
2595
2596 my @projects;
2597 foreach my $pr (@$projlist) {
2598 my (@aa) = git_get_last_activity($pr->{'path'});
2599 unless (@aa) {
2600 next;
2601 }
2602 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2603 if (!defined $pr->{'descr'}) {
2604 my $descr = git_get_project_description($pr->{'path'}) || "";
2605 $pr->{'descr_long'} = to_utf8($descr);
2606 $pr->{'descr'} = chop_str($descr, 25, 5);
2607 }
2608 if (!defined $pr->{'owner'}) {
2609 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2610 }
2611 if ($check_forks) {
2612 my $pname = $pr->{'path'};
2613 if (($pname =~ s/\.git$//) &&
2614 ($pname !~ /\/$/) &&
2615 (-d "$projectroot/$pname")) {
2616 $pr->{'forks'} = "-d $projectroot/$pname";
2617 }
2618 else {
2619 $pr->{'forks'} = 0;
2620 }
2621 }
2622 push @projects, $pr;
2623 }
2624
2625 $order ||= $default_projects_order;
2626 $from = 0 unless defined $from;
2627 $to = $#projects if (!defined $to || $#projects < $to);
2628
2629 print "<table class=\"project_list\">\n";
2630 unless ($no_header) {
2631 print "<tr>\n";
2632 if ($check_forks) {
2633 print "<th></th>\n";
2634 }
2635 if ($order eq "project") {
2636 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2637 print "<th>Project</th>\n";
2638 } else {
2639 print "<th>" .
2640 $cgi->a({-href => href(project=>undef, order=>'project'),
2641 -class => "header"}, "Project") .
2642 "</th>\n";
2643 }
2644 if ($order eq "descr") {
2645 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2646 print "<th>Description</th>\n";
2647 } else {
2648 print "<th>" .
2649 $cgi->a({-href => href(project=>undef, order=>'descr'),
2650 -class => "header"}, "Description") .
2651 "</th>\n";
2652 }
2653 if ($order eq "owner") {
2654 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2655 print "<th>Owner</th>\n";
2656 } else {
2657 print "<th>" .
2658 $cgi->a({-href => href(project=>undef, order=>'owner'),
2659 -class => "header"}, "Owner") .
2660 "</th>\n";
2661 }
2662 if ($order eq "age") {
2663 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2664 print "<th>Last Change</th>\n";
2665 } else {
2666 print "<th>" .
2667 $cgi->a({-href => href(project=>undef, order=>'age'),
2668 -class => "header"}, "Last Change") .
2669 "</th>\n";
2670 }
2671 print "<th></th>\n" .
2672 "</tr>\n";
2673 }
2674 my $alternate = 1;
2675 for (my $i = $from; $i <= $to; $i++) {
2676 my $pr = $projects[$i];
2677 if ($alternate) {
2678 print "<tr class=\"dark\">\n";
2679 } else {
2680 print "<tr class=\"light\">\n";
2681 }
2682 $alternate ^= 1;
2683 if ($check_forks) {
2684 print "<td>";
2685 if ($pr->{'forks'}) {
2686 print "<!-- $pr->{'forks'} -->\n";
2687 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2688 }
2689 print "</td>\n";
2690 }
2691 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2692 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2693 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2694 -class => "list", -title => $pr->{'descr_long'}},
2695 esc_html($pr->{'descr'})) . "</td>\n" .
2696 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2697 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2698 $pr->{'age_string'} . "</td>\n" .
2699 "<td class=\"link\">" .
2700 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2701 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2702 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2703 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2704 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2705 "</td>\n" .
2706 "</tr>\n";
2707 }
2708 if (defined $extra) {
2709 print "<tr>\n";
2710 if ($check_forks) {
2711 print "<td></td>\n";
2712 }
2713 print "<td colspan=\"5\">$extra</td>\n" .
2714 "</tr>\n";
2715 }
2716 print "</table>\n";
2717 }
2718
2719 sub git_shortlog_body {
2720 # uses global variable $project
2721 my ($commitlist, $from, $to, $refs, $extra) = @_;
2722
2723 my $have_snapshot = gitweb_have_snapshot();
2724
2725 $from = 0 unless defined $from;
2726 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
2727
2728 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2729 my $alternate = 1;
2730 for (my $i = $from; $i <= $to; $i++) {
2731 my %co = %{$commitlist->[$i]};
2732 my $commit = $co{'id'};
2733 my $ref = format_ref_marker($refs, $commit);
2734 if ($alternate) {
2735 print "<tr class=\"dark\">\n";
2736 } else {
2737 print "<tr class=\"light\">\n";
2738 }
2739 $alternate ^= 1;
2740 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2741 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2742 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2743 "<td>";
2744 print format_subject_html($co{'title'}, $co{'title_short'},
2745 href(action=>"commit", hash=>$commit), $ref);
2746 print "</td>\n" .
2747 "<td class=\"link\">" .
2748 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2749 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2750 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2751 if ($have_snapshot) {
2752 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2753 }
2754 print "</td>\n" .
2755 "</tr>\n";
2756 }
2757 if (defined $extra) {
2758 print "<tr>\n" .
2759 "<td colspan=\"4\">$extra</td>\n" .
2760 "</tr>\n";
2761 }
2762 print "</table>\n";
2763 }
2764
2765 sub git_history_body {
2766 # Warning: assumes constant type (blob or tree) during history
2767 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2768
2769 $from = 0 unless defined $from;
2770 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
2771
2772 print "<table class=\"history\" cellspacing=\"0\">\n";
2773 my $alternate = 1;
2774 for (my $i = $from; $i <= $to; $i++) {
2775 my %co = %{$commitlist->[$i]};
2776 if (!%co) {
2777 next;
2778 }
2779 my $commit = $co{'id'};
2780
2781 my $ref = format_ref_marker($refs, $commit);
2782
2783 if ($alternate) {
2784 print "<tr class=\"dark\">\n";
2785 } else {
2786 print "<tr class=\"light\">\n";
2787 }
2788 $alternate ^= 1;
2789 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2790 # shortlog uses chop_str($co{'author_name'}, 10)
2791 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2792 "<td>";
2793 # originally git_history used chop_str($co{'title'}, 50)
2794 print format_subject_html($co{'title'}, $co{'title_short'},
2795 href(action=>"commit", hash=>$commit), $ref);
2796 print "</td>\n" .
2797 "<td class=\"link\">" .
2798 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2799 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2800
2801 if ($ftype eq 'blob') {
2802 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2803 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2804 if (defined $blob_current && defined $blob_parent &&
2805 $blob_current ne $blob_parent) {
2806 print " | " .
2807 $cgi->a({-href => href(action=>"blobdiff",
2808 hash=>$blob_current, hash_parent=>$blob_parent,
2809 hash_base=>$hash_base, hash_parent_base=>$commit,
2810 file_name=>$file_name)},
2811 "diff to current");
2812 }
2813 }
2814 print "</td>\n" .
2815 "</tr>\n";
2816 }
2817 if (defined $extra) {
2818 print "<tr>\n" .
2819 "<td colspan=\"4\">$extra</td>\n" .
2820 "</tr>\n";
2821 }
2822 print "</table>\n";
2823 }
2824
2825 sub git_tags_body {
2826 # uses global variable $project
2827 my ($taglist, $from, $to, $extra) = @_;
2828 $from = 0 unless defined $from;
2829 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2830
2831 print "<table class=\"tags\" cellspacing=\"0\">\n";
2832 my $alternate = 1;
2833 for (my $i = $from; $i <= $to; $i++) {
2834 my $entry = $taglist->[$i];
2835 my %tag = %$entry;
2836 my $comment = $tag{'subject'};
2837 my $comment_short;
2838 if (defined $comment) {
2839 $comment_short = chop_str($comment, 30, 5);
2840 }
2841 if ($alternate) {
2842 print "<tr class=\"dark\">\n";
2843 } else {
2844 print "<tr class=\"light\">\n";
2845 }
2846 $alternate ^= 1;
2847 if (defined $tag{'age'}) {
2848 print "<td><i>$tag{'age'}</i></td>\n";
2849 } else {
2850 print "<td></td>\n";
2851 }
2852 print "<td>" .
2853 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2854 -class => "list name"}, esc_html($tag{'name'})) .
2855 "</td>\n" .
2856 "<td>";
2857 if (defined $comment) {
2858 print format_subject_html($comment, $comment_short,
2859 href(action=>"tag", hash=>$tag{'id'}));
2860 }
2861 print "</td>\n" .
2862 "<td class=\"selflink\">";
2863 if ($tag{'type'} eq "tag") {
2864 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2865 } else {
2866 print "&nbsp;";
2867 }
2868 print "</td>\n" .
2869 "<td class=\"link\">" . " | " .
2870 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2871 if ($tag{'reftype'} eq "commit") {
2872 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2873 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2874 } elsif ($tag{'reftype'} eq "blob") {
2875 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2876 }
2877 print "</td>\n" .
2878 "</tr>";
2879 }
2880 if (defined $extra) {
2881 print "<tr>\n" .
2882 "<td colspan=\"5\">$extra</td>\n" .
2883 "</tr>\n";
2884 }
2885 print "</table>\n";
2886 }
2887
2888 sub git_heads_body {
2889 # uses global variable $project
2890 my ($headlist, $head, $from, $to, $extra) = @_;
2891 $from = 0 unless defined $from;
2892 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2893
2894 print "<table class=\"heads\" cellspacing=\"0\">\n";
2895 my $alternate = 1;
2896 for (my $i = $from; $i <= $to; $i++) {
2897 my $entry = $headlist->[$i];
2898 my %ref = %$entry;
2899 my $curr = $ref{'id'} eq $head;
2900 if ($alternate) {
2901 print "<tr class=\"dark\">\n";
2902 } else {
2903 print "<tr class=\"light\">\n";
2904 }
2905 $alternate ^= 1;
2906 print "<td><i>$ref{'age'}</i></td>\n" .
2907 ($curr ? "<td class=\"current_head\">" : "<td>") .
2908 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2909 -class => "list name"},esc_html($ref{'name'})) .
2910 "</td>\n" .
2911 "<td class=\"link\">" .
2912 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2913 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2914 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2915 "</td>\n" .
2916 "</tr>";
2917 }
2918 if (defined $extra) {
2919 print "<tr>\n" .
2920 "<td colspan=\"3\">$extra</td>\n" .
2921 "</tr>\n";
2922 }
2923 print "</table>\n";
2924 }
2925
2926 sub git_search_grep_body {
2927 my ($commitlist, $from, $to, $extra) = @_;
2928 $from = 0 unless defined $from;
2929 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
2930
2931 print "<table class=\"grep\" cellspacing=\"0\">\n";
2932 my $alternate = 1;
2933 for (my $i = $from; $i <= $to; $i++) {
2934 my %co = %{$commitlist->[$i]};
2935 if (!%co) {
2936 next;
2937 }
2938 my $commit = $co{'id'};
2939 if ($alternate) {
2940 print "<tr class=\"dark\">\n";
2941 } else {
2942 print "<tr class=\"light\">\n";
2943 }
2944 $alternate ^= 1;
2945 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2946 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2947 "<td>" .
2948 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2949 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2950 my $comment = $co{'comment'};
2951 foreach my $line (@$comment) {
2952 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2953 my $lead = esc_html($1) || "";
2954 $lead = chop_str($lead, 30, 10);
2955 my $match = esc_html($2) || "";
2956 my $trail = esc_html($3) || "";
2957 $trail = chop_str($trail, 30, 10);
2958 my $text = "$lead<span class=\"match\">$match</span>$trail";
2959 print chop_str($text, 80, 5) . "<br/>\n";
2960 }
2961 }
2962 print "</td>\n" .
2963 "<td class=\"link\">" .
2964 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2965 " | " .
2966 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2967 print "</td>\n" .
2968 "</tr>\n";
2969 }
2970 if (defined $extra) {
2971 print "<tr>\n" .
2972 "<td colspan=\"3\">$extra</td>\n" .
2973 "</tr>\n";
2974 }
2975 print "</table>\n";
2976 }
2977
2978 ## ======================================================================
2979 ## ======================================================================
2980 ## actions
2981
2982 sub git_project_list {
2983 my $order = $cgi->param('o');
2984 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
2985 die_error(undef, "Unknown order parameter");
2986 }
2987
2988 my @list = git_get_projects_list();
2989 if (!@list) {
2990 die_error(undef, "No projects found");
2991 }
2992
2993 git_header_html();
2994 if (-f $home_text) {
2995 print "<div class=\"index_include\">\n";
2996 open (my $fd, $home_text);
2997 print <$fd>;
2998 close $fd;
2999 print "</div>\n";
3000 }
3001 git_project_list_body(\@list, $order);
3002 git_footer_html();
3003 }
3004
3005 sub git_forks {
3006 my $order = $cgi->param('o');
3007 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3008 die_error(undef, "Unknown order parameter");
3009 }
3010
3011 my @list = git_get_projects_list($project);
3012 if (!@list) {
3013 die_error(undef, "No forks found");
3014 }
3015
3016 git_header_html();
3017 git_print_page_nav('','');
3018 git_print_header_div('summary', "$project forks");
3019 git_project_list_body(\@list, $order);
3020 git_footer_html();
3021 }
3022
3023 sub git_project_index {
3024 my @projects = git_get_projects_list($project);
3025
3026 print $cgi->header(
3027 -type => 'text/plain',
3028 -charset => 'utf-8',
3029 -content_disposition => 'inline; filename="index.aux"');
3030
3031 foreach my $pr (@projects) {
3032 if (!exists $pr->{'owner'}) {
3033 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3034 }
3035
3036 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3037 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3038 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3039 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3040 $path =~ s/ /\+/g;
3041 $owner =~ s/ /\+/g;
3042
3043 print "$path $owner\n";
3044 }
3045 }
3046
3047 sub git_summary {
3048 my $descr = git_get_project_description($project) || "none";
3049 my %co = parse_commit("HEAD");
3050 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3051 my $head = $co{'id'};
3052
3053 my $owner = git_get_project_owner($project);
3054
3055 my $refs = git_get_references();
3056 # These get_*_list functions return one more to allow us to see if
3057 # there are more ...
3058 my @taglist = git_get_tags_list(16);
3059 my @headlist = git_get_heads_list(16);
3060 my @forklist;
3061 my ($check_forks) = gitweb_check_feature('forks');
3062
3063 if ($check_forks) {
3064 @forklist = git_get_projects_list($project);
3065 }
3066
3067 git_header_html();
3068 git_print_page_nav('summary','', $head);
3069
3070 print "<div class=\"title\">&nbsp;</div>\n";
3071 print "<table cellspacing=\"0\">\n" .
3072 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3073 "<tr><td>owner</td><td>$owner</td></tr>\n" .
3074 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3075 # use per project git URL list in $projectroot/$project/cloneurl
3076 # or make project git URL from git base URL and project name
3077 my $url_tag = "URL";
3078 my @url_list = git_get_project_url_list($project);
3079 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3080 foreach my $git_url (@url_list) {
3081 next unless $git_url;
3082 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3083 $url_tag = "";
3084 }
3085 print "</table>\n";
3086
3087 if (-s "$projectroot/$project/README.html") {
3088 if (open my $fd, "$projectroot/$project/README.html") {
3089 print "<div class=\"title\">readme</div>\n";
3090 print $_ while (<$fd>);
3091 close $fd;
3092 }
3093 }
3094
3095 # we need to request one more than 16 (0..15) to check if
3096 # those 16 are all
3097 my @commitlist = parse_commits($head, 17);
3098 git_print_header_div('shortlog');
3099 git_shortlog_body(\@commitlist, 0, 15, $refs,
3100 $#commitlist <= 15 ? undef :
3101 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3102
3103 if (@taglist) {
3104 git_print_header_div('tags');
3105 git_tags_body(\@taglist, 0, 15,
3106 $#taglist <= 15 ? undef :
3107 $cgi->a({-href => href(action=>"tags")}, "..."));
3108 }
3109
3110 if (@headlist) {
3111 git_print_header_div('heads');
3112 git_heads_body(\@headlist, $head, 0, 15,
3113 $#headlist <= 15 ? undef :
3114 $cgi->a({-href => href(action=>"heads")}, "..."));
3115 }
3116
3117 if (@forklist) {
3118 git_print_header_div('forks');
3119 git_project_list_body(\@forklist, undef, 0, 15,
3120 $#forklist <= 15 ? undef :
3121 $cgi->a({-href => href(action=>"forks")}, "..."),
3122 'noheader');
3123 }
3124
3125 git_footer_html();
3126 }
3127
3128 sub git_tag {
3129 my $head = git_get_head_hash($project);
3130 git_header_html();
3131 git_print_page_nav('','', $head,undef,$head);
3132 my %tag = parse_tag($hash);
3133 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3134 print "<div class=\"title_text\">\n" .
3135 "<table cellspacing=\"0\">\n" .
3136 "<tr>\n" .
3137 "<td>object</td>\n" .
3138 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3139 $tag{'object'}) . "</td>\n" .
3140 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3141 $tag{'type'}) . "</td>\n" .
3142 "</tr>\n";
3143 if (defined($tag{'author'})) {
3144 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3145 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3146 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3147 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3148 "</td></tr>\n";
3149 }
3150 print "</table>\n\n" .
3151 "</div>\n";
3152 print "<div class=\"page_body\">";
3153 my $comment = $tag{'comment'};
3154 foreach my $line (@$comment) {
3155 chomp $line;
3156 print esc_html($line, -nbsp=>1) . "<br/>\n";
3157 }
3158 print "</div>\n";
3159 git_footer_html();
3160 }
3161
3162 sub git_blame2 {
3163 my $fd;
3164 my $ftype;
3165
3166 my ($have_blame) = gitweb_check_feature('blame');
3167 if (!$have_blame) {
3168 die_error('403 Permission denied', "Permission denied");
3169 }
3170 die_error('404 Not Found', "File name not defined") if (!$file_name);
3171 $hash_base ||= git_get_head_hash($project);
3172 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3173 my %co = parse_commit($hash_base)
3174 or die_error(undef, "Reading commit failed");
3175 if (!defined $hash) {
3176 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3177 or die_error(undef, "Error looking up file");
3178 }
3179 $ftype = git_get_type($hash);
3180 if ($ftype !~ "blob") {
3181 die_error('400 Bad Request', "Object is not a blob");
3182 }
3183 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3184 $file_name, $hash_base)
3185 or die_error(undef, "Open git-blame failed");
3186 git_header_html();
3187 my $formats_nav =
3188 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3189 "blob") .
3190 " | " .
3191 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3192 "history") .
3193 " | " .
3194 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3195 "HEAD");
3196 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3197 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3198 git_print_page_path($file_name, $ftype, $hash_base);
3199 my @rev_color = (qw(light2 dark2));
3200 my $num_colors = scalar(@rev_color);
3201 my $current_color = 0;
3202 my $last_rev;
3203 print <<HTML;
3204 <div class="page_body">
3205 <table class="blame">
3206 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3207 HTML
3208 my %metainfo = ();
3209 while (1) {
3210 $_ = <$fd>;
3211 last unless defined $_;
3212 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3213 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3214 if (!exists $metainfo{$full_rev}) {
3215 $metainfo{$full_rev} = {};
3216 }
3217 my $meta = $metainfo{$full_rev};
3218 while (<$fd>) {
3219 last if (s/^\t//);
3220 if (/^(\S+) (.*)$/) {
3221 $meta->{$1} = $2;
3222 }
3223 }
3224 my $data = $_;
3225 chomp $data;
3226 my $rev = substr($full_rev, 0, 8);
3227 my $author = $meta->{'author'};
3228 my %date = parse_date($meta->{'author-time'},
3229 $meta->{'author-tz'});
3230 my $date = $date{'iso-tz'};
3231 if ($group_size) {
3232 $current_color = ++$current_color % $num_colors;
3233 }
3234 print "<tr class=\"$rev_color[$current_color]\">\n";
3235 if ($group_size) {
3236 print "<td class=\"sha1\"";
3237 print " title=\"". esc_html($author) . ", $date\"";
3238 print " rowspan=\"$group_size\"" if ($group_size > 1);
3239 print ">";
3240 print $cgi->a({-href => href(action=>"commit",
3241 hash=>$full_rev,
3242 file_name=>$file_name)},
3243 esc_html($rev));
3244 print "</td>\n";
3245 }
3246 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3247 or die_error(undef, "Open git-rev-parse failed");
3248 my $parent_commit = <$dd>;
3249 close $dd;
3250 chomp($parent_commit);
3251 my $blamed = href(action => 'blame',
3252 file_name => $meta->{'filename'},
3253 hash_base => $parent_commit);
3254 print "<td class=\"linenr\">";
3255 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3256 -id => "l$lineno",
3257 -class => "linenr" },
3258 esc_html($lineno));
3259 print "</td>";
3260 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3261 print "</tr>\n";
3262 }
3263 print "</table>\n";
3264 print "</div>";
3265 close $fd
3266 or print "Reading blob failed\n";
3267 git_footer_html();
3268 }
3269
3270 sub git_blame {
3271 my $fd;
3272
3273 my ($have_blame) = gitweb_check_feature('blame');
3274 if (!$have_blame) {
3275 die_error('403 Permission denied', "Permission denied");
3276 }
3277 die_error('404 Not Found', "File name not defined") if (!$file_name);
3278 $hash_base ||= git_get_head_hash($project);
3279 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3280 my %co = parse_commit($hash_base)
3281 or die_error(undef, "Reading commit failed");
3282 if (!defined $hash) {
3283 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3284 or die_error(undef, "Error lookup file");
3285 }
3286 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3287 or die_error(undef, "Open git-annotate failed");
3288 git_header_html();
3289 my $formats_nav =
3290 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3291 "blob") .
3292 " | " .
3293 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3294 "history") .
3295 " | " .
3296 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3297 "HEAD");
3298 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3299 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3300 git_print_page_path($file_name, 'blob', $hash_base);
3301 print "<div class=\"page_body\">\n";
3302 print <<HTML;
3303 <table class="blame">
3304 <tr>
3305 <th>Commit</th>
3306 <th>Age</th>
3307 <th>Author</th>
3308 <th>Line</th>
3309 <th>Data</th>
3310 </tr>
3311 HTML
3312 my @line_class = (qw(light dark));
3313 my $line_class_len = scalar (@line_class);
3314 my $line_class_num = $#line_class;
3315 while (my $line = <$fd>) {
3316 my $long_rev;
3317 my $short_rev;
3318 my $author;
3319 my $time;
3320 my $lineno;
3321 my $data;
3322 my $age;
3323 my $age_str;
3324 my $age_class;
3325
3326 chomp $line;
3327 $line_class_num = ($line_class_num + 1) % $line_class_len;
3328
3329 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3330 $long_rev = $1;
3331 $author = $2;
3332 $time = $3;
3333 $lineno = $4;
3334 $data = $5;
3335 } else {
3336 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3337 next;
3338 }
3339 $short_rev = substr ($long_rev, 0, 8);
3340 $age = time () - $time;
3341 $age_str = age_string ($age);
3342 $age_str =~ s/ /&nbsp;/g;
3343 $age_class = age_class($age);
3344 $author = esc_html ($author);
3345 $author =~ s/ /&nbsp;/g;
3346
3347 $data = untabify($data);
3348 $data = esc_html ($data);
3349
3350 print <<HTML;
3351 <tr class="$line_class[$line_class_num]">
3352 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3353 <td class="$age_class">$age_str</td>
3354 <td>$author</td>
3355 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3356 <td class="pre">$data</td>
3357 </tr>
3358 HTML
3359 } # while (my $line = <$fd>)
3360 print "</table>\n\n";
3361 close $fd
3362 or print "Reading blob failed.\n";
3363 print "</div>";
3364 git_footer_html();
3365 }
3366
3367 sub git_tags {
3368 my $head = git_get_head_hash($project);
3369 git_header_html();
3370 git_print_page_nav('','', $head,undef,$head);
3371 git_print_header_div('summary', $project);
3372
3373 my @tagslist = git_get_tags_list();
3374 if (@tagslist) {
3375 git_tags_body(\@tagslist);
3376 }
3377 git_footer_html();
3378 }
3379
3380 sub git_heads {
3381 my $head = git_get_head_hash($project);
3382 git_header_html();
3383 git_print_page_nav('','', $head,undef,$head);
3384 git_print_header_div('summary', $project);
3385
3386 my @headslist = git_get_heads_list();
3387 if (@headslist) {
3388 git_heads_body(\@headslist, $head);
3389 }
3390 git_footer_html();
3391 }
3392
3393 sub git_blob_plain {
3394 my $expires;
3395
3396 if (!defined $hash) {
3397 if (defined $file_name) {
3398 my $base = $hash_base || git_get_head_hash($project);
3399 $hash = git_get_hash_by_path($base, $file_name, "blob")
3400 or die_error(undef, "Error lookup file");
3401 } else {
3402 die_error(undef, "No file name defined");
3403 }
3404 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3405 # blobs defined by non-textual hash id's can be cached
3406 $expires = "+1d";
3407 }
3408
3409 my $type = shift;
3410 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3411 or die_error(undef, "Couldn't cat $file_name, $hash");
3412
3413 $type ||= blob_mimetype($fd, $file_name);
3414
3415 # save as filename, even when no $file_name is given
3416 my $save_as = "$hash";
3417 if (defined $file_name) {
3418 $save_as = $file_name;
3419 } elsif ($type =~ m/^text\//) {
3420 $save_as .= '.txt';
3421 }
3422
3423 print $cgi->header(
3424 -type => "$type",
3425 -expires=>$expires,
3426 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3427 undef $/;
3428 binmode STDOUT, ':raw';
3429 print <$fd>;
3430 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3431 $/ = "\n";
3432 close $fd;
3433 }
3434
3435 sub git_blob {
3436 my $expires;
3437
3438 if (!defined $hash) {
3439 if (defined $file_name) {
3440 my $base = $hash_base || git_get_head_hash($project);
3441 $hash = git_get_hash_by_path($base, $file_name, "blob")
3442 or die_error(undef, "Error lookup file");
3443 } else {
3444 die_error(undef, "No file name defined");
3445 }
3446 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3447 # blobs defined by non-textual hash id's can be cached
3448 $expires = "+1d";
3449 }
3450
3451 my ($have_blame) = gitweb_check_feature('blame');
3452 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3453 or die_error(undef, "Couldn't cat $file_name, $hash");
3454 my $mimetype = blob_mimetype($fd, $file_name);
3455 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3456 close $fd;
3457 return git_blob_plain($mimetype);
3458 }
3459 # we can have blame only for text/* mimetype
3460 $have_blame &&= ($mimetype =~ m!^text/!);
3461
3462 git_header_html(undef, $expires);
3463 my $formats_nav = '';
3464 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3465 if (defined $file_name) {
3466 if ($have_blame) {
3467 $formats_nav .=
3468 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3469 hash=>$hash, file_name=>$file_name)},
3470 "blame") .
3471 " | ";
3472 }
3473 $formats_nav .=
3474 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3475 hash=>$hash, file_name=>$file_name)},
3476 "history") .
3477 " | " .
3478 $cgi->a({-href => href(action=>"blob_plain",
3479 hash=>$hash, file_name=>$file_name)},
3480 "raw") .
3481 " | " .
3482 $cgi->a({-href => href(action=>"blob",
3483 hash_base=>"HEAD", file_name=>$file_name)},
3484 "HEAD");
3485 } else {
3486 $formats_nav .=
3487 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3488 }
3489 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3490 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3491 } else {
3492 print "<div class=\"page_nav\">\n" .
3493 "<br/><br/></div>\n" .
3494 "<div class=\"title\">$hash</div>\n";
3495 }
3496 git_print_page_path($file_name, "blob", $hash_base);
3497 print "<div class=\"page_body\">\n";
3498 if ($mimetype =~ m!^text/!) {
3499 my $nr;
3500 while (my $line = <$fd>) {
3501 chomp $line;
3502 $nr++;
3503 $line = untabify($line);
3504 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3505 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3506 }
3507 } elsif ($mimetype =~ m!^image/!) {
3508 print qq!<img type="$mimetype"!;
3509 if ($file_name) {
3510 print qq! alt="$file_name" title="$file_name"!;
3511 }
3512 print qq! src="! .
3513 href(action=>"blob_plain", hash=>$hash,
3514 hash_base=>$hash_base, file_name=>$file_name) .
3515 qq!" />\n!;
3516 }
3517 close $fd
3518 or print "Reading blob failed.\n";
3519 print "</div>";
3520 git_footer_html();
3521 }
3522
3523 sub git_tree {
3524 my $have_snapshot = gitweb_have_snapshot();
3525
3526 if (!defined $hash_base) {
3527 $hash_base = "HEAD";
3528 }
3529 if (!defined $hash) {
3530 if (defined $file_name) {
3531 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3532 } else {
3533 $hash = $hash_base;
3534 }
3535 }
3536 $/ = "\0";
3537 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3538 or die_error(undef, "Open git-ls-tree failed");
3539 my @entries = map { chomp; $_ } <$fd>;
3540 close $fd or die_error(undef, "Reading tree failed");
3541 $/ = "\n";
3542
3543 my $refs = git_get_references();
3544 my $ref = format_ref_marker($refs, $hash_base);
3545 git_header_html();
3546 my $basedir = '';
3547 my ($have_blame) = gitweb_check_feature('blame');
3548 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3549 my @views_nav = ();
3550 if (defined $file_name) {
3551 push @views_nav,
3552 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3553 hash=>$hash, file_name=>$file_name)},
3554 "history"),
3555 $cgi->a({-href => href(action=>"tree",
3556 hash_base=>"HEAD", file_name=>$file_name)},
3557 "HEAD"),
3558 }
3559 if ($have_snapshot) {
3560 # FIXME: Should be available when we have no hash base as well.
3561 push @views_nav,
3562 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3563 "snapshot");
3564 }
3565 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3566 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3567 } else {
3568 undef $hash_base;
3569 print "<div class=\"page_nav\">\n";
3570 print "<br/><br/></div>\n";
3571 print "<div class=\"title\">$hash</div>\n";
3572 }
3573 if (defined $file_name) {
3574 $basedir = $file_name;
3575 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3576 $basedir .= '/';
3577 }
3578 }
3579 git_print_page_path($file_name, 'tree', $hash_base);
3580 print "<div class=\"page_body\">\n";
3581 print "<table cellspacing=\"0\">\n";
3582 my $alternate = 1;
3583 # '..' (top directory) link if possible
3584 if (defined $hash_base &&
3585 defined $file_name && $file_name =~ m![^/]+$!) {
3586 if ($alternate) {
3587 print "<tr class=\"dark\">\n";
3588 } else {
3589 print "<tr class=\"light\">\n";
3590 }
3591 $alternate ^= 1;
3592
3593 my $up = $file_name;
3594 $up =~ s!/?[^/]+$!!;
3595 undef $up unless $up;
3596 # based on git_print_tree_entry
3597 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3598 print '<td class="list">';
3599 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3600 file_name=>$up)},
3601 "..");
3602 print "</td>\n";
3603 print "<td class=\"link\"></td>\n";
3604
3605 print "</tr>\n";
3606 }
3607 foreach my $line (@entries) {
3608 my %t = parse_ls_tree_line($line, -z => 1);
3609
3610 if ($alternate) {
3611 print "<tr class=\"dark\">\n";
3612 } else {
3613 print "<tr class=\"light\">\n";
3614 }
3615 $alternate ^= 1;
3616
3617 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3618
3619 print "</tr>\n";
3620 }
3621 print "</table>\n" .
3622 "</div>";
3623 git_footer_html();
3624 }
3625
3626 sub git_snapshot {
3627 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3628 my $have_snapshot = (defined $ctype && defined $suffix);
3629 if (!$have_snapshot) {
3630 die_error('403 Permission denied', "Permission denied");
3631 }
3632
3633 if (!defined $hash) {
3634 $hash = git_get_head_hash($project);
3635 }
3636
3637 my $filename = to_utf8(basename($project)) . "-$hash.tar.$suffix";
3638
3639 print $cgi->header(
3640 -type => "application/$ctype",
3641 -content_disposition => 'inline; filename="' . "$filename" . '"',
3642 -status => '200 OK');
3643
3644 my $git = git_cmd_str();
3645 my $name = $project;
3646 $name =~ s/\047/\047\\\047\047/g;
3647 open my $fd, "-|",
3648 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3649 or die_error(undef, "Execute git-tar-tree failed");
3650 binmode STDOUT, ':raw';
3651 print <$fd>;
3652 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3653 close $fd;
3654
3655 }
3656
3657 sub git_log {
3658 my $head = git_get_head_hash($project);
3659 if (!defined $hash) {
3660 $hash = $head;
3661 }
3662 if (!defined $page) {
3663 $page = 0;
3664 }
3665 my $refs = git_get_references();
3666
3667 my @commitlist = parse_commits($hash, 101, (100 * $page));
3668
3669 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3670
3671 git_header_html();
3672 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3673
3674 if (!@commitlist) {
3675 my %co = parse_commit($hash);
3676
3677 git_print_header_div('summary', $project);
3678 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3679 }
3680 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3681 for (my $i = 0; $i <= $to; $i++) {
3682 my %co = %{$commitlist[$i]};
3683 next if !%co;
3684 my $commit = $co{'id'};
3685 my $ref = format_ref_marker($refs, $commit);
3686 my %ad = parse_date($co{'author_epoch'});
3687 git_print_header_div('commit',
3688 "<span class=\"age\">$co{'age_string'}</span>" .
3689 esc_html($co{'title'}) . $ref,
3690 $commit);
3691 print "<div class=\"title_text\">\n" .
3692 "<div class=\"log_link\">\n" .
3693 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3694 " | " .
3695 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3696 " | " .
3697 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3698 "<br/>\n" .
3699 "</div>\n" .
3700 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3701 "</div>\n";
3702
3703 print "<div class=\"log_body\">\n";
3704 git_print_log($co{'comment'}, -final_empty_line=> 1);
3705 print "</div>\n";
3706 }
3707 if ($#commitlist >= 100) {
3708 print "<div class=\"page_nav\">\n";
3709 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
3710 -accesskey => "n", -title => "Alt-n"}, "next");
3711 print "</div>\n";
3712 }
3713 git_footer_html();
3714 }
3715
3716 sub git_commit {
3717 $hash ||= $hash_base || "HEAD";
3718 my %co = parse_commit($hash);
3719 if (!%co) {
3720 die_error(undef, "Unknown commit object");
3721 }
3722 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3723 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3724
3725 my $parent = $co{'parent'};
3726 my $parents = $co{'parents'}; # listref
3727
3728 # we need to prepare $formats_nav before any parameter munging
3729 my $formats_nav;
3730 if (!defined $parent) {
3731 # --root commitdiff
3732 $formats_nav .= '(initial)';
3733 } elsif (@$parents == 1) {
3734 # single parent commit
3735 $formats_nav .=
3736 '(parent: ' .
3737 $cgi->a({-href => href(action=>"commit",
3738 hash=>$parent)},
3739 esc_html(substr($parent, 0, 7))) .
3740 ')';
3741 } else {
3742 # merge commit
3743 $formats_nav .=
3744 '(merge: ' .
3745 join(' ', map {
3746 $cgi->a({-href => href(action=>"commit",
3747 hash=>$_)},
3748 esc_html(substr($_, 0, 7)));
3749 } @$parents ) .
3750 ')';
3751 }
3752
3753 if (!defined $parent) {
3754 $parent = "--root";
3755 }
3756 my @difftree;
3757 if (@$parents <= 1) {
3758 # difftree output is not printed for merges
3759 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3760 @diff_opts, $parent, $hash, "--"
3761 or die_error(undef, "Open git-diff-tree failed");
3762 @difftree = map { chomp; $_ } <$fd>;
3763 close $fd or die_error(undef, "Reading git-diff-tree failed");
3764 }
3765
3766 # non-textual hash id's can be cached
3767 my $expires;
3768 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3769 $expires = "+1d";
3770 }
3771 my $refs = git_get_references();
3772 my $ref = format_ref_marker($refs, $co{'id'});
3773
3774 my $have_snapshot = gitweb_have_snapshot();
3775
3776 git_header_html(undef, $expires);
3777 git_print_page_nav('commit', '',
3778 $hash, $co{'tree'}, $hash,
3779 $formats_nav);
3780
3781 if (defined $co{'parent'}) {
3782 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3783 } else {
3784 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3785 }
3786 print "<div class=\"title_text\">\n" .
3787 "<table cellspacing=\"0\">\n";
3788 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3789 "<tr>" .
3790 "<td></td><td> $ad{'rfc2822'}";
3791 if ($ad{'hour_local'} < 6) {
3792 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3793 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3794 } else {
3795 printf(" (%02d:%02d %s)",
3796 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3797 }
3798 print "</td>" .
3799 "</tr>\n";
3800 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3801 print "<tr><td></td><td> $cd{'rfc2822'}" .
3802 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3803 "</td></tr>\n";
3804 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3805 print "<tr>" .
3806 "<td>tree</td>" .
3807 "<td class=\"sha1\">" .
3808 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3809 class => "list"}, $co{'tree'}) .
3810 "</td>" .
3811 "<td class=\"link\">" .
3812 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3813 "tree");
3814 if ($have_snapshot) {
3815 print " | " .
3816 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3817 }
3818 print "</td>" .
3819 "</tr>\n";
3820
3821 foreach my $par (@$parents) {
3822 print "<tr>" .
3823 "<td>parent</td>" .
3824 "<td class=\"sha1\">" .
3825 $cgi->a({-href => href(action=>"commit", hash=>$par),
3826 class => "list"}, $par) .
3827 "</td>" .
3828 "<td class=\"link\">" .
3829 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3830 " | " .
3831 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3832 "</td>" .
3833 "</tr>\n";
3834 }
3835 print "</table>".
3836 "</div>\n";
3837
3838 print "<div class=\"page_body\">\n";
3839 git_print_log($co{'comment'});
3840 print "</div>\n";
3841
3842 if (@$parents <= 1) {
3843 # do not output difftree/whatchanged for merges
3844 git_difftree_body(\@difftree, $hash, $parent);
3845 }
3846
3847 git_footer_html();
3848 }
3849
3850 sub git_object {
3851 # object is defined by:
3852 # - hash or hash_base alone
3853 # - hash_base and file_name
3854 my $type;
3855
3856 # - hash or hash_base alone
3857 if ($hash || ($hash_base && !defined $file_name)) {
3858 my $object_id = $hash || $hash_base;
3859
3860 my $git_command = git_cmd_str();
3861 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
3862 or die_error('404 Not Found', "Object does not exist");
3863 $type = <$fd>;
3864 chomp $type;
3865 close $fd
3866 or die_error('404 Not Found', "Object does not exist");
3867
3868 # - hash_base and file_name
3869 } elsif ($hash_base && defined $file_name) {
3870 $file_name =~ s,/+$,,;
3871
3872 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
3873 or die_error('404 Not Found', "Base object does not exist");
3874
3875 # here errors should not hapen
3876 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
3877 or die_error(undef, "Open git-ls-tree failed");
3878 my $line = <$fd>;
3879 close $fd;
3880
3881 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3882 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
3883 die_error('404 Not Found', "File or directory for given base does not exist");
3884 }
3885 $type = $2;
3886 $hash = $3;
3887 } else {
3888 die_error('404 Not Found', "Not enough information to find object");
3889 }
3890
3891 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
3892 hash=>$hash, hash_base=>$hash_base,
3893 file_name=>$file_name),
3894 -status => '302 Found');
3895 }
3896
3897 sub git_blobdiff {
3898 my $format = shift || 'html';
3899
3900 my $fd;
3901 my @difftree;
3902 my %diffinfo;
3903 my $expires;
3904
3905 # preparing $fd and %diffinfo for git_patchset_body
3906 # new style URI
3907 if (defined $hash_base && defined $hash_parent_base) {
3908 if (defined $file_name) {
3909 # read raw output
3910 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3911 $hash_parent_base, $hash_base,
3912 "--", (defined $file_parent ? $file_parent : ()), $file_name
3913 or die_error(undef, "Open git-diff-tree failed");
3914 @difftree = map { chomp; $_ } <$fd>;
3915 close $fd
3916 or die_error(undef, "Reading git-diff-tree failed");
3917 @difftree
3918 or die_error('404 Not Found', "Blob diff not found");
3919
3920 } elsif (defined $hash &&
3921 $hash =~ /[0-9a-fA-F]{40}/) {
3922 # try to find filename from $hash
3923
3924 # read filtered raw output
3925 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3926 $hash_parent_base, $hash_base, "--"
3927 or die_error(undef, "Open git-diff-tree failed");
3928 @difftree =
3929 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3930 # $hash == to_id
3931 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3932 map { chomp; $_ } <$fd>;
3933 close $fd
3934 or die_error(undef, "Reading git-diff-tree failed");
3935 @difftree
3936 or die_error('404 Not Found', "Blob diff not found");
3937
3938 } else {
3939 die_error('404 Not Found', "Missing one of the blob diff parameters");
3940 }
3941
3942 if (@difftree > 1) {
3943 die_error('404 Not Found', "Ambiguous blob diff specification");
3944 }
3945
3946 %diffinfo = parse_difftree_raw_line($difftree[0]);
3947 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3948 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3949
3950 $hash_parent ||= $diffinfo{'from_id'};
3951 $hash ||= $diffinfo{'to_id'};
3952
3953 # non-textual hash id's can be cached
3954 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3955 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3956 $expires = '+1d';
3957 }
3958
3959 # open patch output
3960 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3961 '-p', ($format eq 'html' ? "--full-index" : ()),
3962 $hash_parent_base, $hash_base,
3963 "--", (defined $file_parent ? $file_parent : ()), $file_name
3964 or die_error(undef, "Open git-diff-tree failed");
3965 }
3966
3967 # old/legacy style URI
3968 if (!%diffinfo && # if new style URI failed
3969 defined $hash && defined $hash_parent) {
3970 # fake git-diff-tree raw output
3971 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3972 $diffinfo{'from_id'} = $hash_parent;
3973 $diffinfo{'to_id'} = $hash;
3974 if (defined $file_name) {
3975 if (defined $file_parent) {
3976 $diffinfo{'status'} = '2';
3977 $diffinfo{'from_file'} = $file_parent;
3978 $diffinfo{'to_file'} = $file_name;
3979 } else { # assume not renamed
3980 $diffinfo{'status'} = '1';
3981 $diffinfo{'from_file'} = $file_name;
3982 $diffinfo{'to_file'} = $file_name;
3983 }
3984 } else { # no filename given
3985 $diffinfo{'status'} = '2';
3986 $diffinfo{'from_file'} = $hash_parent;
3987 $diffinfo{'to_file'} = $hash;
3988 }
3989
3990 # non-textual hash id's can be cached
3991 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3992 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3993 $expires = '+1d';
3994 }
3995
3996 # open patch output
3997 open $fd, "-|", git_cmd(), "diff", @diff_opts,
3998 '-p', ($format eq 'html' ? "--full-index" : ()),
3999 $hash_parent, $hash, "--"
4000 or die_error(undef, "Open git-diff failed");
4001 } else {
4002 die_error('404 Not Found', "Missing one of the blob diff parameters")
4003 unless %diffinfo;
4004 }
4005
4006 # header
4007 if ($format eq 'html') {
4008 my $formats_nav =
4009 $cgi->a({-href => href(action=>"blobdiff_plain",
4010 hash=>$hash, hash_parent=>$hash_parent,
4011 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4012 file_name=>$file_name, file_parent=>$file_parent)},
4013 "raw");
4014 git_header_html(undef, $expires);
4015 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4016 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4017 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4018 } else {
4019 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4020 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4021 }
4022 if (defined $file_name) {
4023 git_print_page_path($file_name, "blob", $hash_base);
4024 } else {
4025 print "<div class=\"page_path\"></div>\n";
4026 }
4027
4028 } elsif ($format eq 'plain') {
4029 print $cgi->header(
4030 -type => 'text/plain',
4031 -charset => 'utf-8',
4032 -expires => $expires,
4033 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4034
4035 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4036
4037 } else {
4038 die_error(undef, "Unknown blobdiff format");
4039 }
4040
4041 # patch
4042 if ($format eq 'html') {
4043 print "<div class=\"page_body\">\n";
4044
4045 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4046 close $fd;
4047
4048 print "</div>\n"; # class="page_body"
4049 git_footer_html();
4050
4051 } else {
4052 while (my $line = <$fd>) {
4053 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4054 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4055
4056 print $line;
4057
4058 last if $line =~ m!^\+\+\+!;
4059 }
4060 local $/ = undef;
4061 print <$fd>;
4062 close $fd;
4063 }
4064 }
4065
4066 sub git_blobdiff_plain {
4067 git_blobdiff('plain');
4068 }
4069
4070 sub git_commitdiff {
4071 my $format = shift || 'html';
4072 $hash ||= $hash_base || "HEAD";
4073 my %co = parse_commit($hash);
4074 if (!%co) {
4075 die_error(undef, "Unknown commit object");
4076 }
4077
4078 # we need to prepare $formats_nav before any parameter munging
4079 my $formats_nav;
4080 if ($format eq 'html') {
4081 $formats_nav =
4082 $cgi->a({-href => href(action=>"commitdiff_plain",
4083 hash=>$hash, hash_parent=>$hash_parent)},
4084 "raw");
4085
4086 if (defined $hash_parent) {
4087 # commitdiff with two commits given
4088 my $hash_parent_short = $hash_parent;
4089 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4090 $hash_parent_short = substr($hash_parent, 0, 7);
4091 }
4092 $formats_nav .=
4093 ' (from: ' .
4094 $cgi->a({-href => href(action=>"commitdiff",
4095 hash=>$hash_parent)},
4096 esc_html($hash_parent_short)) .
4097 ')';
4098 } elsif (!$co{'parent'}) {
4099 # --root commitdiff
4100 $formats_nav .= ' (initial)';
4101 } elsif (scalar @{$co{'parents'}} == 1) {
4102 # single parent commit
4103 $formats_nav .=
4104 ' (parent: ' .
4105 $cgi->a({-href => href(action=>"commitdiff",
4106 hash=>$co{'parent'})},
4107 esc_html(substr($co{'parent'}, 0, 7))) .
4108 ')';
4109 } else {
4110 # merge commit
4111 $formats_nav .=
4112 ' (merge: ' .
4113 join(' ', map {
4114 $cgi->a({-href => href(action=>"commitdiff",
4115 hash=>$_)},
4116 esc_html(substr($_, 0, 7)));
4117 } @{$co{'parents'}} ) .
4118 ')';
4119 }
4120 }
4121
4122 if (!defined $hash_parent) {
4123 $hash_parent = $co{'parent'} || '--root';
4124 }
4125
4126 # read commitdiff
4127 my $fd;
4128 my @difftree;
4129 if ($format eq 'html') {
4130 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4131 "--no-commit-id", "--patch-with-raw", "--full-index",
4132 $hash_parent, $hash, "--"
4133 or die_error(undef, "Open git-diff-tree failed");
4134
4135 while (my $line = <$fd>) {
4136 chomp $line;
4137 # empty line ends raw part of diff-tree output
4138 last unless $line;
4139 push @difftree, $line;
4140 }
4141
4142 } elsif ($format eq 'plain') {
4143 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4144 '-p', $hash_parent, $hash, "--"
4145 or die_error(undef, "Open git-diff-tree failed");
4146
4147 } else {
4148 die_error(undef, "Unknown commitdiff format");
4149 }
4150
4151 # non-textual hash id's can be cached
4152 my $expires;
4153 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4154 $expires = "+1d";
4155 }
4156
4157 # write commit message
4158 if ($format eq 'html') {
4159 my $refs = git_get_references();
4160 my $ref = format_ref_marker($refs, $co{'id'});
4161
4162 git_header_html(undef, $expires);
4163 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4164 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4165 git_print_authorship(\%co);
4166 print "<div class=\"page_body\">\n";
4167 if (@{$co{'comment'}} > 1) {
4168 print "<div class=\"log\">\n";
4169 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4170 print "</div>\n"; # class="log"
4171 }
4172
4173 } elsif ($format eq 'plain') {
4174 my $refs = git_get_references("tags");
4175 my $tagname = git_get_rev_name_tags($hash);
4176 my $filename = basename($project) . "-$hash.patch";
4177
4178 print $cgi->header(
4179 -type => 'text/plain',
4180 -charset => 'utf-8',
4181 -expires => $expires,
4182 -content_disposition => 'inline; filename="' . "$filename" . '"');
4183 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4184 print <<TEXT;
4185 From: $co{'author'}
4186 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4187 Subject: $co{'title'}
4188 TEXT
4189 print "X-Git-Tag: $tagname\n" if $tagname;
4190 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4191
4192 foreach my $line (@{$co{'comment'}}) {
4193 print "$line\n";
4194 }
4195 print "---\n\n";
4196 }
4197
4198 # write patch
4199 if ($format eq 'html') {
4200 git_difftree_body(\@difftree, $hash, $hash_parent);
4201 print "<br/>\n";
4202
4203 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
4204 close $fd;
4205 print "</div>\n"; # class="page_body"
4206 git_footer_html();
4207
4208 } elsif ($format eq 'plain') {
4209 local $/ = undef;
4210 print <$fd>;
4211 close $fd
4212 or print "Reading git-diff-tree failed\n";
4213 }
4214 }
4215
4216 sub git_commitdiff_plain {
4217 git_commitdiff('plain');
4218 }
4219
4220 sub git_history {
4221 if (!defined $hash_base) {
4222 $hash_base = git_get_head_hash($project);
4223 }
4224 if (!defined $page) {
4225 $page = 0;
4226 }
4227 my $ftype;
4228 my %co = parse_commit($hash_base);
4229 if (!%co) {
4230 die_error(undef, "Unknown commit object");
4231 }
4232
4233 my $refs = git_get_references();
4234 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4235
4236 if (!defined $hash && defined $file_name) {
4237 $hash = git_get_hash_by_path($hash_base, $file_name);
4238 }
4239 if (defined $hash) {
4240 $ftype = git_get_type($hash);
4241 }
4242
4243 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4244
4245 my $paging_nav = '';
4246 if ($page > 0) {
4247 $paging_nav .=
4248 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4249 file_name=>$file_name)},
4250 "first");
4251 $paging_nav .= " &sdot; " .
4252 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4253 file_name=>$file_name, page=>$page-1),
4254 -accesskey => "p", -title => "Alt-p"}, "prev");
4255 } else {
4256 $paging_nav .= "first";
4257 $paging_nav .= " &sdot; prev";
4258 }
4259 if ($#commitlist >= 100) {
4260 $paging_nav .= " &sdot; " .
4261 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4262 file_name=>$file_name, page=>$page+1),
4263 -accesskey => "n", -title => "Alt-n"}, "next");
4264 } else {
4265 $paging_nav .= " &sdot; next";
4266 }
4267 my $next_link = '';
4268 if ($#commitlist >= 100) {
4269 $next_link =
4270 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4271 file_name=>$file_name, page=>$page+1),
4272 -accesskey => "n", -title => "Alt-n"}, "next");
4273 }
4274
4275 git_header_html();
4276 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4277 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4278 git_print_page_path($file_name, $ftype, $hash_base);
4279
4280 git_history_body(\@commitlist, 0, 99,
4281 $refs, $hash_base, $ftype, $next_link);
4282
4283 git_footer_html();
4284 }
4285
4286 sub git_search {
4287 my ($have_search) = gitweb_check_feature('search');
4288 if (!$have_search) {
4289 die_error('403 Permission denied', "Permission denied");
4290 }
4291 if (!defined $searchtext) {
4292 die_error(undef, "Text field empty");
4293 }
4294 if (!defined $hash) {
4295 $hash = git_get_head_hash($project);
4296 }
4297 my %co = parse_commit($hash);
4298 if (!%co) {
4299 die_error(undef, "Unknown commit object");
4300 }
4301 if (!defined $page) {
4302 $page = 0;
4303 }
4304
4305 $searchtype ||= 'commit';
4306 if ($searchtype eq 'pickaxe') {
4307 # pickaxe may take all resources of your box and run for several minutes
4308 # with every query - so decide by yourself how public you make this feature
4309 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4310 if (!$have_pickaxe) {
4311 die_error('403 Permission denied', "Permission denied");
4312 }
4313 }
4314
4315 git_header_html();
4316
4317 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4318 my $greptype;
4319 if ($searchtype eq 'commit') {
4320 $greptype = "--grep=";
4321 } elsif ($searchtype eq 'author') {
4322 $greptype = "--author=";
4323 } elsif ($searchtype eq 'committer') {
4324 $greptype = "--committer=";
4325 }
4326 $greptype .= $searchtext;
4327 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4328
4329 my $paging_nav = '';
4330 if ($page > 0) {
4331 $paging_nav .=
4332 $cgi->a({-href => href(action=>"search", hash=>$hash,
4333 searchtext=>$searchtext, searchtype=>$searchtype)},
4334 "first");
4335 $paging_nav .= " &sdot; " .
4336 $cgi->a({-href => href(action=>"search", hash=>$hash,
4337 searchtext=>$searchtext, searchtype=>$searchtype,
4338 page=>$page-1),
4339 -accesskey => "p", -title => "Alt-p"}, "prev");
4340 } else {
4341 $paging_nav .= "first";
4342 $paging_nav .= " &sdot; prev";
4343 }
4344 if ($#commitlist >= 100) {
4345 $paging_nav .= " &sdot; " .
4346 $cgi->a({-href => href(action=>"search", hash=>$hash,
4347 searchtext=>$searchtext, searchtype=>$searchtype,
4348 page=>$page+1),
4349 -accesskey => "n", -title => "Alt-n"}, "next");
4350 } else {
4351 $paging_nav .= " &sdot; next";
4352 }
4353 my $next_link = '';
4354 if ($#commitlist >= 100) {
4355 $next_link =
4356 $cgi->a({-href => href(action=>"search", hash=>$hash,
4357 searchtext=>$searchtext, searchtype=>$searchtype,
4358 page=>$page+1),
4359 -accesskey => "n", -title => "Alt-n"}, "next");
4360 }
4361
4362 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4363 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4364 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4365 }
4366
4367 if ($searchtype eq 'pickaxe') {
4368 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4369 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4370
4371 print "<table cellspacing=\"0\">\n";
4372 my $alternate = 1;
4373 $/ = "\n";
4374 my $git_command = git_cmd_str();
4375 open my $fd, "-|", "$git_command rev-list $hash | " .
4376 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4377 undef %co;
4378 my @files;
4379 while (my $line = <$fd>) {
4380 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4381 my %set;
4382 $set{'file'} = $6;
4383 $set{'from_id'} = $3;
4384 $set{'to_id'} = $4;
4385 $set{'id'} = $set{'to_id'};
4386 if ($set{'id'} =~ m/0{40}/) {
4387 $set{'id'} = $set{'from_id'};
4388 }
4389 if ($set{'id'} =~ m/0{40}/) {
4390 next;
4391 }
4392 push @files, \%set;
4393 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4394 if (%co) {
4395 if ($alternate) {
4396 print "<tr class=\"dark\">\n";
4397 } else {
4398 print "<tr class=\"light\">\n";
4399 }
4400 $alternate ^= 1;
4401 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4402 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4403 "<td>" .
4404 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4405 -class => "list subject"},
4406 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4407 while (my $setref = shift @files) {
4408 my %set = %$setref;
4409 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4410 hash=>$set{'id'}, file_name=>$set{'file'}),
4411 -class => "list"},
4412 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4413 "<br/>\n";
4414 }
4415 print "</td>\n" .
4416 "<td class=\"link\">" .
4417 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4418 " | " .
4419 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4420 print "</td>\n" .
4421 "</tr>\n";
4422 }
4423 %co = parse_commit($1);
4424 }
4425 }
4426 close $fd;
4427
4428 print "</table>\n";
4429 }
4430 git_footer_html();
4431 }
4432
4433 sub git_search_help {
4434 git_header_html();
4435 git_print_page_nav('','', $hash,$hash,$hash);
4436 print <<EOT;
4437 <dl>
4438 <dt><b>commit</b></dt>
4439 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4440 <dt><b>author</b></dt>
4441 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4442 <dt><b>committer</b></dt>
4443 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4444 EOT
4445 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4446 if ($have_pickaxe) {
4447 print <<EOT;
4448 <dt><b>pickaxe</b></dt>
4449 <dd>All commits that caused the string to appear or disappear from any file (changes that
4450 added, removed or "modified" the string) will be listed. This search can take a while and
4451 takes a lot of strain on the server, so please use it wisely.</dd>
4452 EOT
4453 }
4454 print "</dl>\n";
4455 git_footer_html();
4456 }
4457
4458 sub git_shortlog {
4459 my $head = git_get_head_hash($project);
4460 if (!defined $hash) {
4461 $hash = $head;
4462 }
4463 if (!defined $page) {
4464 $page = 0;
4465 }
4466 my $refs = git_get_references();
4467
4468 my @commitlist = parse_commits($hash, 101, (100 * $page));
4469
4470 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4471 my $next_link = '';
4472 if ($#commitlist >= 100) {
4473 $next_link =
4474 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4475 -accesskey => "n", -title => "Alt-n"}, "next");
4476 }
4477
4478 git_header_html();
4479 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4480 git_print_header_div('summary', $project);
4481
4482 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4483
4484 git_footer_html();
4485 }
4486
4487 ## ......................................................................
4488 ## feeds (RSS, Atom; OPML)
4489
4490 sub git_feed {
4491 my $format = shift || 'atom';
4492 my ($have_blame) = gitweb_check_feature('blame');
4493
4494 # Atom: http://www.atomenabled.org/developers/syndication/
4495 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4496 if ($format ne 'rss' && $format ne 'atom') {
4497 die_error(undef, "Unknown web feed format");
4498 }
4499
4500 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4501 my $head = $hash || 'HEAD';
4502 my @commitlist = parse_commits($head, 150);
4503
4504 my %latest_commit;
4505 my %latest_date;
4506 my $content_type = "application/$format+xml";
4507 if (defined $cgi->http('HTTP_ACCEPT') &&
4508 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4509 # browser (feed reader) prefers text/xml
4510 $content_type = 'text/xml';
4511 }
4512 if (defined($commitlist[0])) {
4513 %latest_commit = %{$commitlist[0]};
4514 %latest_date = parse_date($latest_commit{'author_epoch'});
4515 print $cgi->header(
4516 -type => $content_type,
4517 -charset => 'utf-8',
4518 -last_modified => $latest_date{'rfc2822'});
4519 } else {
4520 print $cgi->header(
4521 -type => $content_type,
4522 -charset => 'utf-8');
4523 }
4524
4525 # Optimization: skip generating the body if client asks only
4526 # for Last-Modified date.
4527 return if ($cgi->request_method() eq 'HEAD');
4528
4529 # header variables
4530 my $title = "$site_name - $project/$action";
4531 my $feed_type = 'log';
4532 if (defined $hash) {
4533 $title .= " - '$hash'";
4534 $feed_type = 'branch log';
4535 if (defined $file_name) {
4536 $title .= " :: $file_name";
4537 $feed_type = 'history';
4538 }
4539 } elsif (defined $file_name) {
4540 $title .= " - $file_name";
4541 $feed_type = 'history';
4542 }
4543 $title .= " $feed_type";
4544 my $descr = git_get_project_description($project);
4545 if (defined $descr) {
4546 $descr = esc_html($descr);
4547 } else {
4548 $descr = "$project " .
4549 ($format eq 'rss' ? 'RSS' : 'Atom') .
4550 " feed";
4551 }
4552 my $owner = git_get_project_owner($project);
4553 $owner = esc_html($owner);
4554
4555 #header
4556 my $alt_url;
4557 if (defined $file_name) {
4558 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4559 } elsif (defined $hash) {
4560 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4561 } else {
4562 $alt_url = href(-full=>1, action=>"summary");
4563 }
4564 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4565 if ($format eq 'rss') {
4566 print <<XML;
4567 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4568 <channel>
4569 XML
4570 print "<title>$title</title>\n" .
4571 "<link>$alt_url</link>\n" .
4572 "<description>$descr</description>\n" .
4573 "<language>en</language>\n";
4574 } elsif ($format eq 'atom') {
4575 print <<XML;
4576 <feed xmlns="http://www.w3.org/2005/Atom">
4577 XML
4578 print "<title>$title</title>\n" .
4579 "<subtitle>$descr</subtitle>\n" .
4580 '<link rel="alternate" type="text/html" href="' .
4581 $alt_url . '" />' . "\n" .
4582 '<link rel="self" type="' . $content_type . '" href="' .
4583 $cgi->self_url() . '" />' . "\n" .
4584 "<id>" . href(-full=>1) . "</id>\n" .
4585 # use project owner for feed author
4586 "<author><name>$owner</name></author>\n";
4587 if (defined $favicon) {
4588 print "<icon>" . esc_url($favicon) . "</icon>\n";
4589 }
4590 if (defined $logo_url) {
4591 # not twice as wide as tall: 72 x 27 pixels
4592 print "<logo>" . esc_url($logo) . "</logo>\n";
4593 }
4594 if (! %latest_date) {
4595 # dummy date to keep the feed valid until commits trickle in:
4596 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4597 } else {
4598 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4599 }
4600 }
4601
4602 # contents
4603 for (my $i = 0; $i <= $#commitlist; $i++) {
4604 my %co = %{$commitlist[$i]};
4605 my $commit = $co{'id'};
4606 # we read 150, we always show 30 and the ones more recent than 48 hours
4607 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4608 last;
4609 }
4610 my %cd = parse_date($co{'author_epoch'});
4611
4612 # get list of changed files
4613 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4614 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4615 or next;
4616 my @difftree = map { chomp; $_ } <$fd>;
4617 close $fd
4618 or next;
4619
4620 # print element (entry, item)
4621 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4622 if ($format eq 'rss') {
4623 print "<item>\n" .
4624 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4625 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4626 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4627 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4628 "<link>$co_url</link>\n" .
4629 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4630 "<content:encoded>" .
4631 "<![CDATA[\n";
4632 } elsif ($format eq 'atom') {
4633 print "<entry>\n" .
4634 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4635 "<updated>$cd{'iso-8601'}</updated>\n" .
4636 "<author>\n" .
4637 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4638 if ($co{'author_email'}) {
4639 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4640 }
4641 print "</author>\n" .
4642 # use committer for contributor
4643 "<contributor>\n" .
4644 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4645 if ($co{'committer_email'}) {
4646 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4647 }
4648 print "</contributor>\n" .
4649 "<published>$cd{'iso-8601'}</published>\n" .
4650 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4651 "<id>$co_url</id>\n" .
4652 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4653 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4654 }
4655 my $comment = $co{'comment'};
4656 print "<pre>\n";
4657 foreach my $line (@$comment) {
4658 $line = esc_html($line);
4659 print "$line\n";
4660 }
4661 print "</pre><ul>\n";
4662 foreach my $difftree_line (@difftree) {
4663 my %difftree = parse_difftree_raw_line($difftree_line);
4664 next if !$difftree{'from_id'};
4665
4666 my $file = $difftree{'file'} || $difftree{'to_file'};
4667
4668 print "<li>" .
4669 "[" .
4670 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4671 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4672 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4673 file_name=>$file, file_parent=>$difftree{'from_file'}),
4674 -title => "diff"}, 'D');
4675 if ($have_blame) {
4676 print $cgi->a({-href => href(-full=>1, action=>"blame",
4677 file_name=>$file, hash_base=>$commit),
4678 -title => "blame"}, 'B');
4679 }
4680 # if this is not a feed of a file history
4681 if (!defined $file_name || $file_name ne $file) {
4682 print $cgi->a({-href => href(-full=>1, action=>"history",
4683 file_name=>$file, hash=>$commit),
4684 -title => "history"}, 'H');
4685 }
4686 $file = esc_path($file);
4687 print "] ".
4688 "$file</li>\n";
4689 }
4690 if ($format eq 'rss') {
4691 print "</ul>]]>\n" .
4692 "</content:encoded>\n" .
4693 "</item>\n";
4694 } elsif ($format eq 'atom') {
4695 print "</ul>\n</div>\n" .
4696 "</content>\n" .
4697 "</entry>\n";
4698 }
4699 }
4700
4701 # end of feed
4702 if ($format eq 'rss') {
4703 print "</channel>\n</rss>\n";
4704 } elsif ($format eq 'atom') {
4705 print "</feed>\n";
4706 }
4707 }
4708
4709 sub git_rss {
4710 git_feed('rss');
4711 }
4712
4713 sub git_atom {
4714 git_feed('atom');
4715 }
4716
4717 sub git_opml {
4718 my @list = git_get_projects_list();
4719
4720 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4721 print <<XML;
4722 <?xml version="1.0" encoding="utf-8"?>
4723 <opml version="1.0">
4724 <head>
4725 <title>$site_name OPML Export</title>
4726 </head>
4727 <body>
4728 <outline text="git RSS feeds">
4729 XML
4730
4731 foreach my $pr (@list) {
4732 my %proj = %$pr;
4733 my $head = git_get_head_hash($proj{'path'});
4734 if (!defined $head) {
4735 next;
4736 }
4737 $git_dir = "$projectroot/$proj{'path'}";
4738 my %co = parse_commit($head);
4739 if (!%co) {
4740 next;
4741 }
4742
4743 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
4744 my $rss = "$my_url?p=$proj{'path'};a=rss";
4745 my $html = "$my_url?p=$proj{'path'};a=summary";
4746 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
4747 }
4748 print <<XML;
4749 </outline>
4750 </body>
4751 </opml>
4752 XML
4753 }
This page took 3.356104 seconds and 5 git commands to generate.