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