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