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