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