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