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