]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Empty patch for merge means trivial merge, not no differences
[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 chomp $descr;
1119 return $descr;
1120 }
1121
1122 sub git_get_project_url_list {
1123 my $path = shift;
1124
1125 open my $fd, "$projectroot/$path/cloneurl" or return;
1126 my @git_project_url_list = map { chomp; $_ } <$fd>;
1127 close $fd;
1128
1129 return wantarray ? @git_project_url_list : \@git_project_url_list;
1130 }
1131
1132 sub git_get_projects_list {
1133 my ($filter) = @_;
1134 my @list;
1135
1136 $filter ||= '';
1137 $filter =~ s/\.git$//;
1138
1139 my ($check_forks) = gitweb_check_feature('forks');
1140
1141 if (-d $projects_list) {
1142 # search in directory
1143 my $dir = $projects_list . ($filter ? "/$filter" : '');
1144 # remove the trailing "/"
1145 $dir =~ s!/+$!!;
1146 my $pfxlen = length("$dir");
1147
1148 File::Find::find({
1149 follow_fast => 1, # follow symbolic links
1150 dangling_symlinks => 0, # ignore dangling symlinks, silently
1151 wanted => sub {
1152 # skip project-list toplevel, if we get it.
1153 return if (m!^[/.]$!);
1154 # only directories can be git repositories
1155 return unless (-d $_);
1156
1157 my $subdir = substr($File::Find::name, $pfxlen + 1);
1158 # we check related file in $projectroot
1159 if ($check_forks and $subdir =~ m#/.#) {
1160 $File::Find::prune = 1;
1161 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1162 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1163 $File::Find::prune = 1;
1164 }
1165 },
1166 }, "$dir");
1167
1168 } elsif (-f $projects_list) {
1169 # read from file(url-encoded):
1170 # 'git%2Fgit.git Linus+Torvalds'
1171 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1172 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1173 my %paths;
1174 open my ($fd), $projects_list or return;
1175 PROJECT:
1176 while (my $line = <$fd>) {
1177 chomp $line;
1178 my ($path, $owner) = split ' ', $line;
1179 $path = unescape($path);
1180 $owner = unescape($owner);
1181 if (!defined $path) {
1182 next;
1183 }
1184 if ($filter ne '') {
1185 # looking for forks;
1186 my $pfx = substr($path, 0, length($filter));
1187 if ($pfx ne $filter) {
1188 next PROJECT;
1189 }
1190 my $sfx = substr($path, length($filter));
1191 if ($sfx !~ /^\/.*\.git$/) {
1192 next PROJECT;
1193 }
1194 } elsif ($check_forks) {
1195 PATH:
1196 foreach my $filter (keys %paths) {
1197 # looking for forks;
1198 my $pfx = substr($path, 0, length($filter));
1199 if ($pfx ne $filter) {
1200 next PATH;
1201 }
1202 my $sfx = substr($path, length($filter));
1203 if ($sfx !~ /^\/.*\.git$/) {
1204 next PATH;
1205 }
1206 # is a fork, don't include it in
1207 # the list
1208 next PROJECT;
1209 }
1210 }
1211 if (check_export_ok("$projectroot/$path")) {
1212 my $pr = {
1213 path => $path,
1214 owner => decode_utf8($owner),
1215 };
1216 push @list, $pr;
1217 (my $forks_path = $path) =~ s/\.git$//;
1218 $paths{$forks_path}++;
1219 }
1220 }
1221 close $fd;
1222 }
1223 return @list;
1224 }
1225
1226 sub git_get_project_owner {
1227 my $project = shift;
1228 my $owner;
1229
1230 return undef unless $project;
1231
1232 # read from file (url-encoded):
1233 # 'git%2Fgit.git Linus+Torvalds'
1234 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1235 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1236 if (-f $projects_list) {
1237 open (my $fd , $projects_list);
1238 while (my $line = <$fd>) {
1239 chomp $line;
1240 my ($pr, $ow) = split ' ', $line;
1241 $pr = unescape($pr);
1242 $ow = unescape($ow);
1243 if ($pr eq $project) {
1244 $owner = decode_utf8($ow);
1245 last;
1246 }
1247 }
1248 close $fd;
1249 }
1250 if (!defined $owner) {
1251 $owner = get_file_owner("$projectroot/$project");
1252 }
1253
1254 return $owner;
1255 }
1256
1257 sub git_get_last_activity {
1258 my ($path) = @_;
1259 my $fd;
1260
1261 $git_dir = "$projectroot/$path";
1262 open($fd, "-|", git_cmd(), 'for-each-ref',
1263 '--format=%(committer)',
1264 '--sort=-committerdate',
1265 '--count=1',
1266 'refs/heads') or return;
1267 my $most_recent = <$fd>;
1268 close $fd or return;
1269 if (defined $most_recent &&
1270 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1271 my $timestamp = $1;
1272 my $age = time - $timestamp;
1273 return ($age, age_string($age));
1274 }
1275 }
1276
1277 sub git_get_references {
1278 my $type = shift || "";
1279 my %refs;
1280 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1281 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1282 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1283 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1284 or return;
1285
1286 while (my $line = <$fd>) {
1287 chomp $line;
1288 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1289 if (defined $refs{$1}) {
1290 push @{$refs{$1}}, $2;
1291 } else {
1292 $refs{$1} = [ $2 ];
1293 }
1294 }
1295 }
1296 close $fd or return;
1297 return \%refs;
1298 }
1299
1300 sub git_get_rev_name_tags {
1301 my $hash = shift || return undef;
1302
1303 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1304 or return;
1305 my $name_rev = <$fd>;
1306 close $fd;
1307
1308 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1309 return $1;
1310 } else {
1311 # catches also '$hash undefined' output
1312 return undef;
1313 }
1314 }
1315
1316 ## ----------------------------------------------------------------------
1317 ## parse to hash functions
1318
1319 sub parse_date {
1320 my $epoch = shift;
1321 my $tz = shift || "-0000";
1322
1323 my %date;
1324 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1325 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1326 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1327 $date{'hour'} = $hour;
1328 $date{'minute'} = $min;
1329 $date{'mday'} = $mday;
1330 $date{'day'} = $days[$wday];
1331 $date{'month'} = $months[$mon];
1332 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1333 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1334 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1335 $mday, $months[$mon], $hour ,$min;
1336 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1337 1900+$year, $mon, $mday, $hour ,$min, $sec;
1338
1339 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1340 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1341 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1342 $date{'hour_local'} = $hour;
1343 $date{'minute_local'} = $min;
1344 $date{'tz_local'} = $tz;
1345 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1346 1900+$year, $mon+1, $mday,
1347 $hour, $min, $sec, $tz);
1348 return %date;
1349 }
1350
1351 sub parse_tag {
1352 my $tag_id = shift;
1353 my %tag;
1354 my @comment;
1355
1356 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1357 $tag{'id'} = $tag_id;
1358 while (my $line = <$fd>) {
1359 chomp $line;
1360 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1361 $tag{'object'} = $1;
1362 } elsif ($line =~ m/^type (.+)$/) {
1363 $tag{'type'} = $1;
1364 } elsif ($line =~ m/^tag (.+)$/) {
1365 $tag{'name'} = $1;
1366 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1367 $tag{'author'} = $1;
1368 $tag{'epoch'} = $2;
1369 $tag{'tz'} = $3;
1370 } elsif ($line =~ m/--BEGIN/) {
1371 push @comment, $line;
1372 last;
1373 } elsif ($line eq "") {
1374 last;
1375 }
1376 }
1377 push @comment, <$fd>;
1378 $tag{'comment'} = \@comment;
1379 close $fd or return;
1380 if (!defined $tag{'name'}) {
1381 return
1382 };
1383 return %tag
1384 }
1385
1386 sub parse_commit_text {
1387 my ($commit_text, $withparents) = @_;
1388 my @commit_lines = split '\n', $commit_text;
1389 my %co;
1390
1391 pop @commit_lines; # Remove '\0'
1392
1393 if (! @commit_lines) {
1394 return;
1395 }
1396
1397 my $header = shift @commit_lines;
1398 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1399 return;
1400 }
1401 ($co{'id'}, my @parents) = split ' ', $header;
1402 while (my $line = shift @commit_lines) {
1403 last if $line eq "\n";
1404 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1405 $co{'tree'} = $1;
1406 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1407 push @parents, $1;
1408 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1409 $co{'author'} = $1;
1410 $co{'author_epoch'} = $2;
1411 $co{'author_tz'} = $3;
1412 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1413 $co{'author_name'} = $1;
1414 $co{'author_email'} = $2;
1415 } else {
1416 $co{'author_name'} = $co{'author'};
1417 }
1418 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1419 $co{'committer'} = $1;
1420 $co{'committer_epoch'} = $2;
1421 $co{'committer_tz'} = $3;
1422 $co{'committer_name'} = $co{'committer'};
1423 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1424 $co{'committer_name'} = $1;
1425 $co{'committer_email'} = $2;
1426 } else {
1427 $co{'committer_name'} = $co{'committer'};
1428 }
1429 }
1430 }
1431 if (!defined $co{'tree'}) {
1432 return;
1433 };
1434 $co{'parents'} = \@parents;
1435 $co{'parent'} = $parents[0];
1436
1437 foreach my $title (@commit_lines) {
1438 $title =~ s/^ //;
1439 if ($title ne "") {
1440 $co{'title'} = chop_str($title, 80, 5);
1441 # remove leading stuff of merges to make the interesting part visible
1442 if (length($title) > 50) {
1443 $title =~ s/^Automatic //;
1444 $title =~ s/^merge (of|with) /Merge ... /i;
1445 if (length($title) > 50) {
1446 $title =~ s/(http|rsync):\/\///;
1447 }
1448 if (length($title) > 50) {
1449 $title =~ s/(master|www|rsync)\.//;
1450 }
1451 if (length($title) > 50) {
1452 $title =~ s/kernel.org:?//;
1453 }
1454 if (length($title) > 50) {
1455 $title =~ s/\/pub\/scm//;
1456 }
1457 }
1458 $co{'title_short'} = chop_str($title, 50, 5);
1459 last;
1460 }
1461 }
1462 if ($co{'title'} eq "") {
1463 $co{'title'} = $co{'title_short'} = '(no commit message)';
1464 }
1465 # remove added spaces
1466 foreach my $line (@commit_lines) {
1467 $line =~ s/^ //;
1468 }
1469 $co{'comment'} = \@commit_lines;
1470
1471 my $age = time - $co{'committer_epoch'};
1472 $co{'age'} = $age;
1473 $co{'age_string'} = age_string($age);
1474 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1475 if ($age > 60*60*24*7*2) {
1476 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1477 $co{'age_string_age'} = $co{'age_string'};
1478 } else {
1479 $co{'age_string_date'} = $co{'age_string'};
1480 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1481 }
1482 return %co;
1483 }
1484
1485 sub parse_commit {
1486 my ($commit_id) = @_;
1487 my %co;
1488
1489 local $/ = "\0";
1490
1491 open my $fd, "-|", git_cmd(), "rev-list",
1492 "--parents",
1493 "--header",
1494 "--max-count=1",
1495 $commit_id,
1496 "--",
1497 or die_error(undef, "Open git-rev-list failed");
1498 %co = parse_commit_text(<$fd>, 1);
1499 close $fd;
1500
1501 return %co;
1502 }
1503
1504 sub parse_commits {
1505 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1506 my @cos;
1507
1508 $maxcount ||= 1;
1509 $skip ||= 0;
1510
1511 local $/ = "\0";
1512
1513 open my $fd, "-|", git_cmd(), "rev-list",
1514 "--header",
1515 ($arg ? ($arg) : ()),
1516 ("--max-count=" . $maxcount),
1517 ("--skip=" . $skip),
1518 $commit_id,
1519 "--",
1520 ($filename ? ($filename) : ())
1521 or die_error(undef, "Open git-rev-list failed");
1522 while (my $line = <$fd>) {
1523 my %co = parse_commit_text($line);
1524 push @cos, \%co;
1525 }
1526 close $fd;
1527
1528 return wantarray ? @cos : \@cos;
1529 }
1530
1531 # parse ref from ref_file, given by ref_id, with given type
1532 sub parse_ref {
1533 my $ref_file = shift;
1534 my $ref_id = shift;
1535 my $type = shift || git_get_type($ref_id);
1536 my %ref_item;
1537
1538 $ref_item{'type'} = $type;
1539 $ref_item{'id'} = $ref_id;
1540 $ref_item{'epoch'} = 0;
1541 $ref_item{'age'} = "unknown";
1542 if ($type eq "tag") {
1543 my %tag = parse_tag($ref_id);
1544 $ref_item{'comment'} = $tag{'comment'};
1545 if ($tag{'type'} eq "commit") {
1546 my %co = parse_commit($tag{'object'});
1547 $ref_item{'epoch'} = $co{'committer_epoch'};
1548 $ref_item{'age'} = $co{'age_string'};
1549 } elsif (defined($tag{'epoch'})) {
1550 my $age = time - $tag{'epoch'};
1551 $ref_item{'epoch'} = $tag{'epoch'};
1552 $ref_item{'age'} = age_string($age);
1553 }
1554 $ref_item{'reftype'} = $tag{'type'};
1555 $ref_item{'name'} = $tag{'name'};
1556 $ref_item{'refid'} = $tag{'object'};
1557 } elsif ($type eq "commit"){
1558 my %co = parse_commit($ref_id);
1559 $ref_item{'reftype'} = "commit";
1560 $ref_item{'name'} = $ref_file;
1561 $ref_item{'title'} = $co{'title'};
1562 $ref_item{'refid'} = $ref_id;
1563 $ref_item{'epoch'} = $co{'committer_epoch'};
1564 $ref_item{'age'} = $co{'age_string'};
1565 } else {
1566 $ref_item{'reftype'} = $type;
1567 $ref_item{'name'} = $ref_file;
1568 $ref_item{'refid'} = $ref_id;
1569 }
1570
1571 return %ref_item;
1572 }
1573
1574 # parse line of git-diff-tree "raw" output
1575 sub parse_difftree_raw_line {
1576 my $line = shift;
1577 my %res;
1578
1579 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1580 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1581 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1582 $res{'from_mode'} = $1;
1583 $res{'to_mode'} = $2;
1584 $res{'from_id'} = $3;
1585 $res{'to_id'} = $4;
1586 $res{'status'} = $5;
1587 $res{'similarity'} = $6;
1588 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1589 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1590 } else {
1591 $res{'file'} = unquote($7);
1592 }
1593 }
1594 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1595 # combined diff (for merge commit)
1596 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1597 $res{'nparents'} = length($1);
1598 $res{'from_mode'} = [ split(' ', $2) ];
1599 $res{'to_mode'} = pop @{$res{'from_mode'}};
1600 $res{'from_id'} = [ split(' ', $3) ];
1601 $res{'to_id'} = pop @{$res{'from_id'}};
1602 $res{'status'} = [ split('', $4) ];
1603 $res{'to_file'} = unquote($5);
1604 }
1605 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1606 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1607 $res{'commit'} = $1;
1608 }
1609
1610 return wantarray ? %res : \%res;
1611 }
1612
1613 # parse line of git-ls-tree output
1614 sub parse_ls_tree_line ($;%) {
1615 my $line = shift;
1616 my %opts = @_;
1617 my %res;
1618
1619 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1620 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1621
1622 $res{'mode'} = $1;
1623 $res{'type'} = $2;
1624 $res{'hash'} = $3;
1625 if ($opts{'-z'}) {
1626 $res{'name'} = $4;
1627 } else {
1628 $res{'name'} = unquote($4);
1629 }
1630
1631 return wantarray ? %res : \%res;
1632 }
1633
1634 ## ......................................................................
1635 ## parse to array of hashes functions
1636
1637 sub git_get_heads_list {
1638 my $limit = shift;
1639 my @headslist;
1640
1641 open my $fd, '-|', git_cmd(), 'for-each-ref',
1642 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1643 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1644 'refs/heads'
1645 or return;
1646 while (my $line = <$fd>) {
1647 my %ref_item;
1648
1649 chomp $line;
1650 my ($refinfo, $committerinfo) = split(/\0/, $line);
1651 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1652 my ($committer, $epoch, $tz) =
1653 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1654 $name =~ s!^refs/heads/!!;
1655
1656 $ref_item{'name'} = $name;
1657 $ref_item{'id'} = $hash;
1658 $ref_item{'title'} = $title || '(no commit message)';
1659 $ref_item{'epoch'} = $epoch;
1660 if ($epoch) {
1661 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1662 } else {
1663 $ref_item{'age'} = "unknown";
1664 }
1665
1666 push @headslist, \%ref_item;
1667 }
1668 close $fd;
1669
1670 return wantarray ? @headslist : \@headslist;
1671 }
1672
1673 sub git_get_tags_list {
1674 my $limit = shift;
1675 my @tagslist;
1676
1677 open my $fd, '-|', git_cmd(), 'for-each-ref',
1678 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1679 '--format=%(objectname) %(objecttype) %(refname) '.
1680 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1681 'refs/tags'
1682 or return;
1683 while (my $line = <$fd>) {
1684 my %ref_item;
1685
1686 chomp $line;
1687 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1688 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1689 my ($creator, $epoch, $tz) =
1690 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1691 $name =~ s!^refs/tags/!!;
1692
1693 $ref_item{'type'} = $type;
1694 $ref_item{'id'} = $id;
1695 $ref_item{'name'} = $name;
1696 if ($type eq "tag") {
1697 $ref_item{'subject'} = $title;
1698 $ref_item{'reftype'} = $reftype;
1699 $ref_item{'refid'} = $refid;
1700 } else {
1701 $ref_item{'reftype'} = $type;
1702 $ref_item{'refid'} = $id;
1703 }
1704
1705 if ($type eq "tag" || $type eq "commit") {
1706 $ref_item{'epoch'} = $epoch;
1707 if ($epoch) {
1708 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1709 } else {
1710 $ref_item{'age'} = "unknown";
1711 }
1712 }
1713
1714 push @tagslist, \%ref_item;
1715 }
1716 close $fd;
1717
1718 return wantarray ? @tagslist : \@tagslist;
1719 }
1720
1721 ## ----------------------------------------------------------------------
1722 ## filesystem-related functions
1723
1724 sub get_file_owner {
1725 my $path = shift;
1726
1727 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1728 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1729 if (!defined $gcos) {
1730 return undef;
1731 }
1732 my $owner = $gcos;
1733 $owner =~ s/[,;].*$//;
1734 return decode_utf8($owner);
1735 }
1736
1737 ## ......................................................................
1738 ## mimetype related functions
1739
1740 sub mimetype_guess_file {
1741 my $filename = shift;
1742 my $mimemap = shift;
1743 -r $mimemap or return undef;
1744
1745 my %mimemap;
1746 open(MIME, $mimemap) or return undef;
1747 while (<MIME>) {
1748 next if m/^#/; # skip comments
1749 my ($mime, $exts) = split(/\t+/);
1750 if (defined $exts) {
1751 my @exts = split(/\s+/, $exts);
1752 foreach my $ext (@exts) {
1753 $mimemap{$ext} = $mime;
1754 }
1755 }
1756 }
1757 close(MIME);
1758
1759 $filename =~ /\.([^.]*)$/;
1760 return $mimemap{$1};
1761 }
1762
1763 sub mimetype_guess {
1764 my $filename = shift;
1765 my $mime;
1766 $filename =~ /\./ or return undef;
1767
1768 if ($mimetypes_file) {
1769 my $file = $mimetypes_file;
1770 if ($file !~ m!^/!) { # if it is relative path
1771 # it is relative to project
1772 $file = "$projectroot/$project/$file";
1773 }
1774 $mime = mimetype_guess_file($filename, $file);
1775 }
1776 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1777 return $mime;
1778 }
1779
1780 sub blob_mimetype {
1781 my $fd = shift;
1782 my $filename = shift;
1783
1784 if ($filename) {
1785 my $mime = mimetype_guess($filename);
1786 $mime and return $mime;
1787 }
1788
1789 # just in case
1790 return $default_blob_plain_mimetype unless $fd;
1791
1792 if (-T $fd) {
1793 return 'text/plain' .
1794 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1795 } elsif (! $filename) {
1796 return 'application/octet-stream';
1797 } elsif ($filename =~ m/\.png$/i) {
1798 return 'image/png';
1799 } elsif ($filename =~ m/\.gif$/i) {
1800 return 'image/gif';
1801 } elsif ($filename =~ m/\.jpe?g$/i) {
1802 return 'image/jpeg';
1803 } else {
1804 return 'application/octet-stream';
1805 }
1806 }
1807
1808 ## ======================================================================
1809 ## functions printing HTML: header, footer, error page
1810
1811 sub git_header_html {
1812 my $status = shift || "200 OK";
1813 my $expires = shift;
1814
1815 my $title = "$site_name";
1816 if (defined $project) {
1817 $title .= " - " . decode_utf8($project);
1818 if (defined $action) {
1819 $title .= "/$action";
1820 if (defined $file_name) {
1821 $title .= " - " . esc_path($file_name);
1822 if ($action eq "tree" && $file_name !~ m|/$|) {
1823 $title .= "/";
1824 }
1825 }
1826 }
1827 }
1828 my $content_type;
1829 # require explicit support from the UA if we are to send the page as
1830 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1831 # we have to do this because MSIE sometimes globs '*/*', pretending to
1832 # support xhtml+xml but choking when it gets what it asked for.
1833 if (defined $cgi->http('HTTP_ACCEPT') &&
1834 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1835 $cgi->Accept('application/xhtml+xml') != 0) {
1836 $content_type = 'application/xhtml+xml';
1837 } else {
1838 $content_type = 'text/html';
1839 }
1840 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1841 -status=> $status, -expires => $expires);
1842 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1843 print <<EOF;
1844 <?xml version="1.0" encoding="utf-8"?>
1845 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1846 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1847 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1848 <!-- git core binaries version $git_version -->
1849 <head>
1850 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1851 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1852 <meta name="robots" content="index, nofollow"/>
1853 <title>$title</title>
1854 EOF
1855 # print out each stylesheet that exist
1856 if (defined $stylesheet) {
1857 #provides backwards capability for those people who define style sheet in a config file
1858 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1859 } else {
1860 foreach my $stylesheet (@stylesheets) {
1861 next unless $stylesheet;
1862 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1863 }
1864 }
1865 if (defined $project) {
1866 printf('<link rel="alternate" title="%s log RSS feed" '.
1867 'href="%s" type="application/rss+xml" />'."\n",
1868 esc_param($project), href(action=>"rss"));
1869 printf('<link rel="alternate" title="%s log Atom feed" '.
1870 'href="%s" type="application/atom+xml" />'."\n",
1871 esc_param($project), href(action=>"atom"));
1872 } else {
1873 printf('<link rel="alternate" title="%s projects list" '.
1874 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1875 $site_name, href(project=>undef, action=>"project_index"));
1876 printf('<link rel="alternate" title="%s projects feeds" '.
1877 'href="%s" type="text/x-opml"/>'."\n",
1878 $site_name, href(project=>undef, action=>"opml"));
1879 }
1880 if (defined $favicon) {
1881 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1882 }
1883
1884 print "</head>\n" .
1885 "<body>\n";
1886
1887 if (-f $site_header) {
1888 open (my $fd, $site_header);
1889 print <$fd>;
1890 close $fd;
1891 }
1892
1893 print "<div class=\"page_header\">\n" .
1894 $cgi->a({-href => esc_url($logo_url),
1895 -title => $logo_label},
1896 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1897 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1898 if (defined $project) {
1899 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1900 if (defined $action) {
1901 print " / $action";
1902 }
1903 print "\n";
1904 }
1905 my ($have_search) = gitweb_check_feature('search');
1906 if ((defined $project) && ($have_search)) {
1907 if (!defined $searchtext) {
1908 $searchtext = "";
1909 }
1910 my $search_hash;
1911 if (defined $hash_base) {
1912 $search_hash = $hash_base;
1913 } elsif (defined $hash) {
1914 $search_hash = $hash;
1915 } else {
1916 $search_hash = "HEAD";
1917 }
1918 $cgi->param("a", "search");
1919 $cgi->param("h", $search_hash);
1920 $cgi->param("p", $project);
1921 print $cgi->startform(-method => "get", -action => $my_uri) .
1922 "<div class=\"search\">\n" .
1923 $cgi->hidden(-name => "p") . "\n" .
1924 $cgi->hidden(-name => "a") . "\n" .
1925 $cgi->hidden(-name => "h") . "\n" .
1926 $cgi->popup_menu(-name => 'st', -default => 'commit',
1927 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1928 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1929 " search:\n",
1930 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1931 "</div>" .
1932 $cgi->end_form() . "\n";
1933 }
1934 print "</div>\n";
1935 }
1936
1937 sub git_footer_html {
1938 print "<div class=\"page_footer\">\n";
1939 if (defined $project) {
1940 my $descr = git_get_project_description($project);
1941 if (defined $descr) {
1942 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1943 }
1944 print $cgi->a({-href => href(action=>"rss"),
1945 -class => "rss_logo"}, "RSS") . " ";
1946 print $cgi->a({-href => href(action=>"atom"),
1947 -class => "rss_logo"}, "Atom") . "\n";
1948 } else {
1949 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1950 -class => "rss_logo"}, "OPML") . " ";
1951 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1952 -class => "rss_logo"}, "TXT") . "\n";
1953 }
1954 print "</div>\n" ;
1955
1956 if (-f $site_footer) {
1957 open (my $fd, $site_footer);
1958 print <$fd>;
1959 close $fd;
1960 }
1961
1962 print "</body>\n" .
1963 "</html>";
1964 }
1965
1966 sub die_error {
1967 my $status = shift || "403 Forbidden";
1968 my $error = shift || "Malformed query, file missing or permission denied";
1969
1970 git_header_html($status);
1971 print <<EOF;
1972 <div class="page_body">
1973 <br /><br />
1974 $status - $error
1975 <br />
1976 </div>
1977 EOF
1978 git_footer_html();
1979 exit;
1980 }
1981
1982 ## ----------------------------------------------------------------------
1983 ## functions printing or outputting HTML: navigation
1984
1985 sub git_print_page_nav {
1986 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1987 $extra = '' if !defined $extra; # pager or formats
1988
1989 my @navs = qw(summary shortlog log commit commitdiff tree);
1990 if ($suppress) {
1991 @navs = grep { $_ ne $suppress } @navs;
1992 }
1993
1994 my %arg = map { $_ => {action=>$_} } @navs;
1995 if (defined $head) {
1996 for (qw(commit commitdiff)) {
1997 $arg{$_}{'hash'} = $head;
1998 }
1999 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2000 for (qw(shortlog log)) {
2001 $arg{$_}{'hash'} = $head;
2002 }
2003 }
2004 }
2005 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2006 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2007
2008 print "<div class=\"page_nav\">\n" .
2009 (join " | ",
2010 map { $_ eq $current ?
2011 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2012 } @navs);
2013 print "<br/>\n$extra<br/>\n" .
2014 "</div>\n";
2015 }
2016
2017 sub format_paging_nav {
2018 my ($action, $hash, $head, $page, $nrevs) = @_;
2019 my $paging_nav;
2020
2021
2022 if ($hash ne $head || $page) {
2023 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2024 } else {
2025 $paging_nav .= "HEAD";
2026 }
2027
2028 if ($page > 0) {
2029 $paging_nav .= " &sdot; " .
2030 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2031 -accesskey => "p", -title => "Alt-p"}, "prev");
2032 } else {
2033 $paging_nav .= " &sdot; prev";
2034 }
2035
2036 if ($nrevs >= (100 * ($page+1)-1)) {
2037 $paging_nav .= " &sdot; " .
2038 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2039 -accesskey => "n", -title => "Alt-n"}, "next");
2040 } else {
2041 $paging_nav .= " &sdot; next";
2042 }
2043
2044 return $paging_nav;
2045 }
2046
2047 ## ......................................................................
2048 ## functions printing or outputting HTML: div
2049
2050 sub git_print_header_div {
2051 my ($action, $title, $hash, $hash_base) = @_;
2052 my %args = ();
2053
2054 $args{'action'} = $action;
2055 $args{'hash'} = $hash if $hash;
2056 $args{'hash_base'} = $hash_base if $hash_base;
2057
2058 print "<div class=\"header\">\n" .
2059 $cgi->a({-href => href(%args), -class => "title"},
2060 $title ? $title : $action) .
2061 "\n</div>\n";
2062 }
2063
2064 #sub git_print_authorship (\%) {
2065 sub git_print_authorship {
2066 my $co = shift;
2067
2068 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2069 print "<div class=\"author_date\">" .
2070 esc_html($co->{'author_name'}) .
2071 " [$ad{'rfc2822'}";
2072 if ($ad{'hour_local'} < 6) {
2073 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2074 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2075 } else {
2076 printf(" (%02d:%02d %s)",
2077 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2078 }
2079 print "]</div>\n";
2080 }
2081
2082 sub git_print_page_path {
2083 my $name = shift;
2084 my $type = shift;
2085 my $hb = shift;
2086
2087
2088 print "<div class=\"page_path\">";
2089 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2090 -title => 'tree root'}, decode_utf8("[$project]"));
2091 print " / ";
2092 if (defined $name) {
2093 my @dirname = split '/', $name;
2094 my $basename = pop @dirname;
2095 my $fullname = '';
2096
2097 foreach my $dir (@dirname) {
2098 $fullname .= ($fullname ? '/' : '') . $dir;
2099 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2100 hash_base=>$hb),
2101 -title => $fullname}, esc_path($dir));
2102 print " / ";
2103 }
2104 if (defined $type && $type eq 'blob') {
2105 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2106 hash_base=>$hb),
2107 -title => $name}, esc_path($basename));
2108 } elsif (defined $type && $type eq 'tree') {
2109 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2110 hash_base=>$hb),
2111 -title => $name}, esc_path($basename));
2112 print " / ";
2113 } else {
2114 print esc_path($basename);
2115 }
2116 }
2117 print "<br/></div>\n";
2118 }
2119
2120 # sub git_print_log (\@;%) {
2121 sub git_print_log ($;%) {
2122 my $log = shift;
2123 my %opts = @_;
2124
2125 if ($opts{'-remove_title'}) {
2126 # remove title, i.e. first line of log
2127 shift @$log;
2128 }
2129 # remove leading empty lines
2130 while (defined $log->[0] && $log->[0] eq "") {
2131 shift @$log;
2132 }
2133
2134 # print log
2135 my $signoff = 0;
2136 my $empty = 0;
2137 foreach my $line (@$log) {
2138 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2139 $signoff = 1;
2140 $empty = 0;
2141 if (! $opts{'-remove_signoff'}) {
2142 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2143 next;
2144 } else {
2145 # remove signoff lines
2146 next;
2147 }
2148 } else {
2149 $signoff = 0;
2150 }
2151
2152 # print only one empty line
2153 # do not print empty line after signoff
2154 if ($line eq "") {
2155 next if ($empty || $signoff);
2156 $empty = 1;
2157 } else {
2158 $empty = 0;
2159 }
2160
2161 print format_log_line_html($line) . "<br/>\n";
2162 }
2163
2164 if ($opts{'-final_empty_line'}) {
2165 # end with single empty line
2166 print "<br/>\n" unless $empty;
2167 }
2168 }
2169
2170 # return link target (what link points to)
2171 sub git_get_link_target {
2172 my $hash = shift;
2173 my $link_target;
2174
2175 # read link
2176 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2177 or return;
2178 {
2179 local $/;
2180 $link_target = <$fd>;
2181 }
2182 close $fd
2183 or return;
2184
2185 return $link_target;
2186 }
2187
2188 # given link target, and the directory (basedir) the link is in,
2189 # return target of link relative to top directory (top tree);
2190 # return undef if it is not possible (including absolute links).
2191 sub normalize_link_target {
2192 my ($link_target, $basedir, $hash_base) = @_;
2193
2194 # we can normalize symlink target only if $hash_base is provided
2195 return unless $hash_base;
2196
2197 # absolute symlinks (beginning with '/') cannot be normalized
2198 return if (substr($link_target, 0, 1) eq '/');
2199
2200 # normalize link target to path from top (root) tree (dir)
2201 my $path;
2202 if ($basedir) {
2203 $path = $basedir . '/' . $link_target;
2204 } else {
2205 # we are in top (root) tree (dir)
2206 $path = $link_target;
2207 }
2208
2209 # remove //, /./, and /../
2210 my @path_parts;
2211 foreach my $part (split('/', $path)) {
2212 # discard '.' and ''
2213 next if (!$part || $part eq '.');
2214 # handle '..'
2215 if ($part eq '..') {
2216 if (@path_parts) {
2217 pop @path_parts;
2218 } else {
2219 # link leads outside repository (outside top dir)
2220 return;
2221 }
2222 } else {
2223 push @path_parts, $part;
2224 }
2225 }
2226 $path = join('/', @path_parts);
2227
2228 return $path;
2229 }
2230
2231 # print tree entry (row of git_tree), but without encompassing <tr> element
2232 sub git_print_tree_entry {
2233 my ($t, $basedir, $hash_base, $have_blame) = @_;
2234
2235 my %base_key = ();
2236 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2237
2238 # The format of a table row is: mode list link. Where mode is
2239 # the mode of the entry, list is the name of the entry, an href,
2240 # and link is the action links of the entry.
2241
2242 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2243 if ($t->{'type'} eq "blob") {
2244 print "<td class=\"list\">" .
2245 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2246 file_name=>"$basedir$t->{'name'}", %base_key),
2247 -class => "list"}, esc_path($t->{'name'}));
2248 if (S_ISLNK(oct $t->{'mode'})) {
2249 my $link_target = git_get_link_target($t->{'hash'});
2250 if ($link_target) {
2251 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2252 if (defined $norm_target) {
2253 print " -> " .
2254 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2255 file_name=>$norm_target),
2256 -title => $norm_target}, esc_path($link_target));
2257 } else {
2258 print " -> " . esc_path($link_target);
2259 }
2260 }
2261 }
2262 print "</td>\n";
2263 print "<td class=\"link\">";
2264 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2265 file_name=>"$basedir$t->{'name'}", %base_key)},
2266 "blob");
2267 if ($have_blame) {
2268 print " | " .
2269 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2270 file_name=>"$basedir$t->{'name'}", %base_key)},
2271 "blame");
2272 }
2273 if (defined $hash_base) {
2274 print " | " .
2275 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2276 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2277 "history");
2278 }
2279 print " | " .
2280 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2281 file_name=>"$basedir$t->{'name'}")},
2282 "raw");
2283 print "</td>\n";
2284
2285 } elsif ($t->{'type'} eq "tree") {
2286 print "<td class=\"list\">";
2287 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2288 file_name=>"$basedir$t->{'name'}", %base_key)},
2289 esc_path($t->{'name'}));
2290 print "</td>\n";
2291 print "<td class=\"link\">";
2292 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2293 file_name=>"$basedir$t->{'name'}", %base_key)},
2294 "tree");
2295 if (defined $hash_base) {
2296 print " | " .
2297 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2298 file_name=>"$basedir$t->{'name'}")},
2299 "history");
2300 }
2301 print "</td>\n";
2302 }
2303 }
2304
2305 ## ......................................................................
2306 ## functions printing large fragments of HTML
2307
2308 sub fill_from_file_info {
2309 my ($diff, @parents) = @_;
2310
2311 $diff->{'from_file'} = [ ];
2312 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2313 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2314 if ($diff->{'status'}[$i] eq 'R' ||
2315 $diff->{'status'}[$i] eq 'C') {
2316 $diff->{'from_file'}[$i] =
2317 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2318 }
2319 }
2320
2321 return $diff;
2322 }
2323
2324 # parameters can be strings, or references to arrays of strings
2325 sub from_ids_eq {
2326 my ($a, $b) = @_;
2327
2328 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2329 for (my $i = 0; $i < @$a; ++$i) {
2330 return 0 unless ($a->[$i] eq $b->[$i]);
2331 }
2332 return 1;
2333 } elsif (!ref($a) && !ref($b)) {
2334 return $a eq $b;
2335 } else {
2336 return 0;
2337 }
2338 }
2339
2340
2341 sub git_difftree_body {
2342 my ($difftree, $hash, @parents) = @_;
2343 my ($parent) = $parents[0];
2344 my ($have_blame) = gitweb_check_feature('blame');
2345 print "<div class=\"list_head\">\n";
2346 if ($#{$difftree} > 10) {
2347 print(($#{$difftree} + 1) . " files changed:\n");
2348 }
2349 print "</div>\n";
2350
2351 print "<table class=\"" .
2352 (@parents > 1 ? "combined " : "") .
2353 "diff_tree\">\n";
2354 my $alternate = 1;
2355 my $patchno = 0;
2356 foreach my $line (@{$difftree}) {
2357 my $diff;
2358 if (ref($line) eq "HASH") {
2359 # pre-parsed (or generated by hand)
2360 $diff = $line;
2361 } else {
2362 $diff = parse_difftree_raw_line($line);
2363 }
2364
2365 if ($alternate) {
2366 print "<tr class=\"dark\">\n";
2367 } else {
2368 print "<tr class=\"light\">\n";
2369 }
2370 $alternate ^= 1;
2371
2372 if (exists $diff->{'nparents'}) { # combined diff
2373
2374 fill_from_file_info($diff, @parents)
2375 unless exists $diff->{'from_file'};
2376
2377 if ($diff->{'to_id'} ne ('0' x 40)) {
2378 # file exists in the result (child) commit
2379 print "<td>" .
2380 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2381 file_name=>$diff->{'to_file'},
2382 hash_base=>$hash),
2383 -class => "list"}, esc_path($diff->{'to_file'})) .
2384 "</td>\n";
2385 } else {
2386 print "<td>" .
2387 esc_path($diff->{'to_file'}) .
2388 "</td>\n";
2389 }
2390
2391 if ($action eq 'commitdiff') {
2392 # link to patch
2393 $patchno++;
2394 print "<td class=\"link\">" .
2395 $cgi->a({-href => "#patch$patchno"}, "patch") .
2396 " | " .
2397 "</td>\n";
2398 }
2399
2400 my $has_history = 0;
2401 my $not_deleted = 0;
2402 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2403 my $hash_parent = $parents[$i];
2404 my $from_hash = $diff->{'from_id'}[$i];
2405 my $from_path = $diff->{'from_file'}[$i];
2406 my $status = $diff->{'status'}[$i];
2407
2408 $has_history ||= ($status ne 'A');
2409 $not_deleted ||= ($status ne 'D');
2410
2411 if ($status eq 'A') {
2412 print "<td class=\"link\" align=\"right\"> | </td>\n";
2413 } elsif ($status eq 'D') {
2414 print "<td class=\"link\">" .
2415 $cgi->a({-href => href(action=>"blob",
2416 hash_base=>$hash,
2417 hash=>$from_hash,
2418 file_name=>$from_path)},
2419 "blob" . ($i+1)) .
2420 " | </td>\n";
2421 } else {
2422 if ($diff->{'to_id'} eq $from_hash) {
2423 print "<td class=\"link nochange\">";
2424 } else {
2425 print "<td class=\"link\">";
2426 }
2427 print $cgi->a({-href => href(action=>"blobdiff",
2428 hash=>$diff->{'to_id'},
2429 hash_parent=>$from_hash,
2430 hash_base=>$hash,
2431 hash_parent_base=>$hash_parent,
2432 file_name=>$diff->{'to_file'},
2433 file_parent=>$from_path)},
2434 "diff" . ($i+1)) .
2435 " | </td>\n";
2436 }
2437 }
2438
2439 print "<td class=\"link\">";
2440 if ($not_deleted) {
2441 print $cgi->a({-href => href(action=>"blob",
2442 hash=>$diff->{'to_id'},
2443 file_name=>$diff->{'to_file'},
2444 hash_base=>$hash)},
2445 "blob");
2446 print " | " if ($has_history);
2447 }
2448 if ($has_history) {
2449 print $cgi->a({-href => href(action=>"history",
2450 file_name=>$diff->{'to_file'},
2451 hash_base=>$hash)},
2452 "history");
2453 }
2454 print "</td>\n";
2455
2456 print "</tr>\n";
2457 next; # instead of 'else' clause, to avoid extra indent
2458 }
2459 # else ordinary diff
2460
2461 my ($to_mode_oct, $to_mode_str, $to_file_type);
2462 my ($from_mode_oct, $from_mode_str, $from_file_type);
2463 if ($diff->{'to_mode'} ne ('0' x 6)) {
2464 $to_mode_oct = oct $diff->{'to_mode'};
2465 if (S_ISREG($to_mode_oct)) { # only for regular file
2466 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2467 }
2468 $to_file_type = file_type($diff->{'to_mode'});
2469 }
2470 if ($diff->{'from_mode'} ne ('0' x 6)) {
2471 $from_mode_oct = oct $diff->{'from_mode'};
2472 if (S_ISREG($to_mode_oct)) { # only for regular file
2473 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2474 }
2475 $from_file_type = file_type($diff->{'from_mode'});
2476 }
2477
2478 if ($diff->{'status'} eq "A") { # created
2479 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2480 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2481 $mode_chng .= "]</span>";
2482 print "<td>";
2483 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2484 hash_base=>$hash, file_name=>$diff->{'file'}),
2485 -class => "list"}, esc_path($diff->{'file'}));
2486 print "</td>\n";
2487 print "<td>$mode_chng</td>\n";
2488 print "<td class=\"link\">";
2489 if ($action eq 'commitdiff') {
2490 # link to patch
2491 $patchno++;
2492 print $cgi->a({-href => "#patch$patchno"}, "patch");
2493 print " | ";
2494 }
2495 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2496 hash_base=>$hash, file_name=>$diff->{'file'})},
2497 "blob");
2498 print "</td>\n";
2499
2500 } elsif ($diff->{'status'} eq "D") { # deleted
2501 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2502 print "<td>";
2503 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2504 hash_base=>$parent, file_name=>$diff->{'file'}),
2505 -class => "list"}, esc_path($diff->{'file'}));
2506 print "</td>\n";
2507 print "<td>$mode_chng</td>\n";
2508 print "<td class=\"link\">";
2509 if ($action eq 'commitdiff') {
2510 # link to patch
2511 $patchno++;
2512 print $cgi->a({-href => "#patch$patchno"}, "patch");
2513 print " | ";
2514 }
2515 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2516 hash_base=>$parent, file_name=>$diff->{'file'})},
2517 "blob") . " | ";
2518 if ($have_blame) {
2519 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2520 file_name=>$diff->{'file'})},
2521 "blame") . " | ";
2522 }
2523 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2524 file_name=>$diff->{'file'})},
2525 "history");
2526 print "</td>\n";
2527
2528 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2529 my $mode_chnge = "";
2530 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2531 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2532 if ($from_file_type ne $to_file_type) {
2533 $mode_chnge .= " from $from_file_type to $to_file_type";
2534 }
2535 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2536 if ($from_mode_str && $to_mode_str) {
2537 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2538 } elsif ($to_mode_str) {
2539 $mode_chnge .= " mode: $to_mode_str";
2540 }
2541 }
2542 $mode_chnge .= "]</span>\n";
2543 }
2544 print "<td>";
2545 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2546 hash_base=>$hash, file_name=>$diff->{'file'}),
2547 -class => "list"}, esc_path($diff->{'file'}));
2548 print "</td>\n";
2549 print "<td>$mode_chnge</td>\n";
2550 print "<td class=\"link\">";
2551 if ($action eq 'commitdiff') {
2552 # link to patch
2553 $patchno++;
2554 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2555 " | ";
2556 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2557 # "commit" view and modified file (not onlu mode changed)
2558 print $cgi->a({-href => href(action=>"blobdiff",
2559 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2560 hash_base=>$hash, hash_parent_base=>$parent,
2561 file_name=>$diff->{'file'})},
2562 "diff") .
2563 " | ";
2564 }
2565 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2566 hash_base=>$hash, file_name=>$diff->{'file'})},
2567 "blob") . " | ";
2568 if ($have_blame) {
2569 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2570 file_name=>$diff->{'file'})},
2571 "blame") . " | ";
2572 }
2573 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2574 file_name=>$diff->{'file'})},
2575 "history");
2576 print "</td>\n";
2577
2578 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2579 my %status_name = ('R' => 'moved', 'C' => 'copied');
2580 my $nstatus = $status_name{$diff->{'status'}};
2581 my $mode_chng = "";
2582 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2583 # mode also for directories, so we cannot use $to_mode_str
2584 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2585 }
2586 print "<td>" .
2587 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2588 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2589 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2590 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2591 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2592 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2593 -class => "list"}, esc_path($diff->{'from_file'})) .
2594 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2595 "<td class=\"link\">";
2596 if ($action eq 'commitdiff') {
2597 # link to patch
2598 $patchno++;
2599 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2600 " | ";
2601 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2602 # "commit" view and modified file (not only pure rename or copy)
2603 print $cgi->a({-href => href(action=>"blobdiff",
2604 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2605 hash_base=>$hash, hash_parent_base=>$parent,
2606 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2607 "diff") .
2608 " | ";
2609 }
2610 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2611 hash_base=>$parent, file_name=>$diff->{'to_file'})},
2612 "blob") . " | ";
2613 if ($have_blame) {
2614 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2615 file_name=>$diff->{'to_file'})},
2616 "blame") . " | ";
2617 }
2618 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2619 file_name=>$diff->{'to_file'})},
2620 "history");
2621 print "</td>\n";
2622
2623 } # we should not encounter Unmerged (U) or Unknown (X) status
2624 print "</tr>\n";
2625 }
2626 print "</table>\n";
2627 }
2628
2629 sub git_patchset_body {
2630 my ($fd, $difftree, $hash, @hash_parents) = @_;
2631 my ($hash_parent) = $hash_parents[0];
2632
2633 my $patch_idx = 0;
2634 my $patch_number = 0;
2635 my $patch_line;
2636 my $diffinfo;
2637 my (%from, %to);
2638
2639 print "<div class=\"patchset\">\n";
2640
2641 # skip to first patch
2642 while ($patch_line = <$fd>) {
2643 chomp $patch_line;
2644
2645 last if ($patch_line =~ m/^diff /);
2646 }
2647
2648 PATCH:
2649 while ($patch_line) {
2650 my @diff_header;
2651 my ($from_id, $to_id);
2652
2653 # git diff header
2654 #assert($patch_line =~ m/^diff /) if DEBUG;
2655 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2656 $patch_number++;
2657 push @diff_header, $patch_line;
2658
2659 # extended diff header
2660 EXTENDED_HEADER:
2661 while ($patch_line = <$fd>) {
2662 chomp $patch_line;
2663
2664 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2665
2666 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2667 $from_id = $1;
2668 $to_id = $2;
2669 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2670 $from_id = [ split(',', $1) ];
2671 $to_id = $2;
2672 }
2673
2674 push @diff_header, $patch_line;
2675 }
2676 my $last_patch_line = $patch_line;
2677
2678 # check if current patch belong to current raw line
2679 # and parse raw git-diff line if needed
2680 if (defined $diffinfo &&
2681 defined $from_id && defined $to_id &&
2682 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2683 $diffinfo->{'to_id'} eq $to_id) {
2684 # this is continuation of a split patch
2685 print "<div class=\"patch cont\">\n";
2686 } else {
2687 # advance raw git-diff output if needed
2688 $patch_idx++ if defined $diffinfo;
2689
2690 # read and prepare patch information
2691 if (ref($difftree->[$patch_idx]) eq "HASH") {
2692 # pre-parsed (or generated by hand)
2693 $diffinfo = $difftree->[$patch_idx];
2694 } else {
2695 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2696 }
2697 if ($diffinfo->{'nparents'}) {
2698 # combined diff
2699 $from{'file'} = [];
2700 $from{'href'} = [];
2701 fill_from_file_info($diffinfo, @hash_parents)
2702 unless exists $diffinfo->{'from_file'};
2703 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2704 $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2705 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2706 $from{'href'}[$i] = href(action=>"blob",
2707 hash_base=>$hash_parents[$i],
2708 hash=>$diffinfo->{'from_id'}[$i],
2709 file_name=>$from{'file'}[$i]);
2710 } else {
2711 $from{'href'}[$i] = undef;
2712 }
2713 }
2714 } else {
2715 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2716 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2717 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2718 hash=>$diffinfo->{'from_id'},
2719 file_name=>$from{'file'});
2720 } else {
2721 delete $from{'href'};
2722 }
2723 }
2724 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2725 if ($diffinfo->{'status'} ne "D") { # not deleted file
2726 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2727 hash=>$diffinfo->{'to_id'},
2728 file_name=>$to{'file'});
2729 } else {
2730 delete $to{'href'};
2731 }
2732 # this is first patch for raw difftree line with $patch_idx index
2733 # we index @$difftree array from 0, but number patches from 1
2734 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2735 }
2736
2737 # print "git diff" header
2738 $patch_line = shift @diff_header;
2739 if ($diffinfo->{'nparents'}) {
2740
2741 # combined diff
2742 $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2743 if ($to{'href'}) {
2744 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2745 esc_path($to{'file'}));
2746 } else { # file was deleted
2747 $patch_line .= esc_path($to{'file'});
2748 }
2749
2750 } else {
2751
2752 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2753 if ($from{'href'}) {
2754 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2755 'a/' . esc_path($from{'file'}));
2756 } else { # file was added
2757 $patch_line .= 'a/' . esc_path($from{'file'});
2758 }
2759 $patch_line .= ' ';
2760 if ($to{'href'}) {
2761 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2762 'b/' . esc_path($to{'file'}));
2763 } else { # file was deleted
2764 $patch_line .= 'b/' . esc_path($to{'file'});
2765 }
2766
2767 }
2768 print "<div class=\"diff header\">$patch_line</div>\n";
2769
2770 # print extended diff header
2771 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2772 EXTENDED_HEADER:
2773 foreach $patch_line (@diff_header) {
2774 # match <path>
2775 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2776 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2777 esc_path($from{'file'}));
2778 }
2779 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2780 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2781 esc_path($to{'file'}));
2782 }
2783 # match single <mode>
2784 if ($patch_line =~ m/\s(\d{6})$/) {
2785 $patch_line .= '<span class="info"> (' .
2786 file_type_long($1) .
2787 ')</span>';
2788 }
2789 # match <hash>
2790 if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2791 # can match only for combined diff
2792 $patch_line = 'index ';
2793 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2794 if ($from{'href'}[$i]) {
2795 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2796 -class=>"hash"},
2797 substr($diffinfo->{'from_id'}[$i],0,7));
2798 } else {
2799 $patch_line .= '0' x 7;
2800 }
2801 # separator
2802 $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2803 }
2804 $patch_line .= '..';
2805 if ($to{'href'}) {
2806 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2807 substr($diffinfo->{'to_id'},0,7));
2808 } else {
2809 $patch_line .= '0' x 7;
2810 }
2811
2812 } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2813 # can match only for ordinary diff
2814 my ($from_link, $to_link);
2815 if ($from{'href'}) {
2816 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2817 substr($diffinfo->{'from_id'},0,7));
2818 } else {
2819 $from_link = '0' x 7;
2820 }
2821 if ($to{'href'}) {
2822 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2823 substr($diffinfo->{'to_id'},0,7));
2824 } else {
2825 $to_link = '0' x 7;
2826 }
2827 #affirm {
2828 # my ($from_hash, $to_hash) =
2829 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2830 # my ($from_id, $to_id) =
2831 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2832 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2833 #} if DEBUG;
2834 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2835 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2836 }
2837 print $patch_line . "<br/>\n";
2838 }
2839 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2840
2841 # from-file/to-file diff header
2842 $patch_line = $last_patch_line;
2843 if (! $patch_line) {
2844 print "</div>\n"; # class="patch"
2845 last PATCH;
2846 }
2847 next PATCH if ($patch_line =~ m/^diff /);
2848 #assert($patch_line =~ m/^---/) if DEBUG;
2849 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2850 $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2851 $patch_line = '--- a/' .
2852 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2853 esc_path($from{'file'}));
2854 }
2855 print "<div class=\"diff from_file\">$patch_line</div>\n";
2856
2857 $patch_line = <$fd>;
2858 chomp $patch_line;
2859
2860 #assert($patch_line =~ m/^+++/) if DEBUG;
2861 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2862 $patch_line = '+++ b/' .
2863 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2864 esc_path($to{'file'}));
2865 }
2866 print "<div class=\"diff to_file\">$patch_line</div>\n";
2867
2868 # the patch itself
2869 LINE:
2870 while ($patch_line = <$fd>) {
2871 chomp $patch_line;
2872
2873 next PATCH if ($patch_line =~ m/^diff /);
2874
2875 print format_diff_line($patch_line, \%from, \%to);
2876 }
2877
2878 } continue {
2879 print "</div>\n"; # class="patch"
2880 }
2881
2882 if ($patch_number == 0) {
2883 if (@hash_parents > 1) {
2884 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
2885 } else {
2886 print "<div class=\"diff nodifferences\">No differences found</div>\n";
2887 }
2888 }
2889
2890 print "</div>\n"; # class="patchset"
2891 }
2892
2893 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2894
2895 sub git_project_list_body {
2896 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2897
2898 my ($check_forks) = gitweb_check_feature('forks');
2899
2900 my @projects;
2901 foreach my $pr (@$projlist) {
2902 my (@aa) = git_get_last_activity($pr->{'path'});
2903 unless (@aa) {
2904 next;
2905 }
2906 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2907 if (!defined $pr->{'descr'}) {
2908 my $descr = git_get_project_description($pr->{'path'}) || "";
2909 $pr->{'descr_long'} = decode_utf8($descr);
2910 $pr->{'descr'} = chop_str($descr, 25, 5);
2911 }
2912 if (!defined $pr->{'owner'}) {
2913 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2914 }
2915 if ($check_forks) {
2916 my $pname = $pr->{'path'};
2917 if (($pname =~ s/\.git$//) &&
2918 ($pname !~ /\/$/) &&
2919 (-d "$projectroot/$pname")) {
2920 $pr->{'forks'} = "-d $projectroot/$pname";
2921 }
2922 else {
2923 $pr->{'forks'} = 0;
2924 }
2925 }
2926 push @projects, $pr;
2927 }
2928
2929 $order ||= $default_projects_order;
2930 $from = 0 unless defined $from;
2931 $to = $#projects if (!defined $to || $#projects < $to);
2932
2933 print "<table class=\"project_list\">\n";
2934 unless ($no_header) {
2935 print "<tr>\n";
2936 if ($check_forks) {
2937 print "<th></th>\n";
2938 }
2939 if ($order eq "project") {
2940 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2941 print "<th>Project</th>\n";
2942 } else {
2943 print "<th>" .
2944 $cgi->a({-href => href(project=>undef, order=>'project'),
2945 -class => "header"}, "Project") .
2946 "</th>\n";
2947 }
2948 if ($order eq "descr") {
2949 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2950 print "<th>Description</th>\n";
2951 } else {
2952 print "<th>" .
2953 $cgi->a({-href => href(project=>undef, order=>'descr'),
2954 -class => "header"}, "Description") .
2955 "</th>\n";
2956 }
2957 if ($order eq "owner") {
2958 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2959 print "<th>Owner</th>\n";
2960 } else {
2961 print "<th>" .
2962 $cgi->a({-href => href(project=>undef, order=>'owner'),
2963 -class => "header"}, "Owner") .
2964 "</th>\n";
2965 }
2966 if ($order eq "age") {
2967 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2968 print "<th>Last Change</th>\n";
2969 } else {
2970 print "<th>" .
2971 $cgi->a({-href => href(project=>undef, order=>'age'),
2972 -class => "header"}, "Last Change") .
2973 "</th>\n";
2974 }
2975 print "<th></th>\n" .
2976 "</tr>\n";
2977 }
2978 my $alternate = 1;
2979 for (my $i = $from; $i <= $to; $i++) {
2980 my $pr = $projects[$i];
2981 if ($alternate) {
2982 print "<tr class=\"dark\">\n";
2983 } else {
2984 print "<tr class=\"light\">\n";
2985 }
2986 $alternate ^= 1;
2987 if ($check_forks) {
2988 print "<td>";
2989 if ($pr->{'forks'}) {
2990 print "<!-- $pr->{'forks'} -->\n";
2991 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2992 }
2993 print "</td>\n";
2994 }
2995 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2996 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2997 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2998 -class => "list", -title => $pr->{'descr_long'}},
2999 esc_html($pr->{'descr'})) . "</td>\n" .
3000 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
3001 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3002 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3003 "<td class=\"link\">" .
3004 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3005 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3006 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3007 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3008 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3009 "</td>\n" .
3010 "</tr>\n";
3011 }
3012 if (defined $extra) {
3013 print "<tr>\n";
3014 if ($check_forks) {
3015 print "<td></td>\n";
3016 }
3017 print "<td colspan=\"5\">$extra</td>\n" .
3018 "</tr>\n";
3019 }
3020 print "</table>\n";
3021 }
3022
3023 sub git_shortlog_body {
3024 # uses global variable $project
3025 my ($commitlist, $from, $to, $refs, $extra) = @_;
3026
3027 my $have_snapshot = gitweb_have_snapshot();
3028
3029 $from = 0 unless defined $from;
3030 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3031
3032 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3033 my $alternate = 1;
3034 for (my $i = $from; $i <= $to; $i++) {
3035 my %co = %{$commitlist->[$i]};
3036 my $commit = $co{'id'};
3037 my $ref = format_ref_marker($refs, $commit);
3038 if ($alternate) {
3039 print "<tr class=\"dark\">\n";
3040 } else {
3041 print "<tr class=\"light\">\n";
3042 }
3043 $alternate ^= 1;
3044 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3045 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3046 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3047 "<td>";
3048 print format_subject_html($co{'title'}, $co{'title_short'},
3049 href(action=>"commit", hash=>$commit), $ref);
3050 print "</td>\n" .
3051 "<td class=\"link\">" .
3052 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3053 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3054 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3055 if ($have_snapshot) {
3056 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3057 }
3058 print "</td>\n" .
3059 "</tr>\n";
3060 }
3061 if (defined $extra) {
3062 print "<tr>\n" .
3063 "<td colspan=\"4\">$extra</td>\n" .
3064 "</tr>\n";
3065 }
3066 print "</table>\n";
3067 }
3068
3069 sub git_history_body {
3070 # Warning: assumes constant type (blob or tree) during history
3071 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3072
3073 $from = 0 unless defined $from;
3074 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3075
3076 print "<table class=\"history\" cellspacing=\"0\">\n";
3077 my $alternate = 1;
3078 for (my $i = $from; $i <= $to; $i++) {
3079 my %co = %{$commitlist->[$i]};
3080 if (!%co) {
3081 next;
3082 }
3083 my $commit = $co{'id'};
3084
3085 my $ref = format_ref_marker($refs, $commit);
3086
3087 if ($alternate) {
3088 print "<tr class=\"dark\">\n";
3089 } else {
3090 print "<tr class=\"light\">\n";
3091 }
3092 $alternate ^= 1;
3093 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3094 # shortlog uses chop_str($co{'author_name'}, 10)
3095 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3096 "<td>";
3097 # originally git_history used chop_str($co{'title'}, 50)
3098 print format_subject_html($co{'title'}, $co{'title_short'},
3099 href(action=>"commit", hash=>$commit), $ref);
3100 print "</td>\n" .
3101 "<td class=\"link\">" .
3102 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3103 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3104
3105 if ($ftype eq 'blob') {
3106 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3107 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3108 if (defined $blob_current && defined $blob_parent &&
3109 $blob_current ne $blob_parent) {
3110 print " | " .
3111 $cgi->a({-href => href(action=>"blobdiff",
3112 hash=>$blob_current, hash_parent=>$blob_parent,
3113 hash_base=>$hash_base, hash_parent_base=>$commit,
3114 file_name=>$file_name)},
3115 "diff to current");
3116 }
3117 }
3118 print "</td>\n" .
3119 "</tr>\n";
3120 }
3121 if (defined $extra) {
3122 print "<tr>\n" .
3123 "<td colspan=\"4\">$extra</td>\n" .
3124 "</tr>\n";
3125 }
3126 print "</table>\n";
3127 }
3128
3129 sub git_tags_body {
3130 # uses global variable $project
3131 my ($taglist, $from, $to, $extra) = @_;
3132 $from = 0 unless defined $from;
3133 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3134
3135 print "<table class=\"tags\" cellspacing=\"0\">\n";
3136 my $alternate = 1;
3137 for (my $i = $from; $i <= $to; $i++) {
3138 my $entry = $taglist->[$i];
3139 my %tag = %$entry;
3140 my $comment = $tag{'subject'};
3141 my $comment_short;
3142 if (defined $comment) {
3143 $comment_short = chop_str($comment, 30, 5);
3144 }
3145 if ($alternate) {
3146 print "<tr class=\"dark\">\n";
3147 } else {
3148 print "<tr class=\"light\">\n";
3149 }
3150 $alternate ^= 1;
3151 if (defined $tag{'age'}) {
3152 print "<td><i>$tag{'age'}</i></td>\n";
3153 } else {
3154 print "<td></td>\n";
3155 }
3156 print "<td>" .
3157 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3158 -class => "list name"}, esc_html($tag{'name'})) .
3159 "</td>\n" .
3160 "<td>";
3161 if (defined $comment) {
3162 print format_subject_html($comment, $comment_short,
3163 href(action=>"tag", hash=>$tag{'id'}));
3164 }
3165 print "</td>\n" .
3166 "<td class=\"selflink\">";
3167 if ($tag{'type'} eq "tag") {
3168 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3169 } else {
3170 print "&nbsp;";
3171 }
3172 print "</td>\n" .
3173 "<td class=\"link\">" . " | " .
3174 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3175 if ($tag{'reftype'} eq "commit") {
3176 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3177 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3178 } elsif ($tag{'reftype'} eq "blob") {
3179 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3180 }
3181 print "</td>\n" .
3182 "</tr>";
3183 }
3184 if (defined $extra) {
3185 print "<tr>\n" .
3186 "<td colspan=\"5\">$extra</td>\n" .
3187 "</tr>\n";
3188 }
3189 print "</table>\n";
3190 }
3191
3192 sub git_heads_body {
3193 # uses global variable $project
3194 my ($headlist, $head, $from, $to, $extra) = @_;
3195 $from = 0 unless defined $from;
3196 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3197
3198 print "<table class=\"heads\" cellspacing=\"0\">\n";
3199 my $alternate = 1;
3200 for (my $i = $from; $i <= $to; $i++) {
3201 my $entry = $headlist->[$i];
3202 my %ref = %$entry;
3203 my $curr = $ref{'id'} eq $head;
3204 if ($alternate) {
3205 print "<tr class=\"dark\">\n";
3206 } else {
3207 print "<tr class=\"light\">\n";
3208 }
3209 $alternate ^= 1;
3210 print "<td><i>$ref{'age'}</i></td>\n" .
3211 ($curr ? "<td class=\"current_head\">" : "<td>") .
3212 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3213 -class => "list name"},esc_html($ref{'name'})) .
3214 "</td>\n" .
3215 "<td class=\"link\">" .
3216 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3217 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3218 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3219 "</td>\n" .
3220 "</tr>";
3221 }
3222 if (defined $extra) {
3223 print "<tr>\n" .
3224 "<td colspan=\"3\">$extra</td>\n" .
3225 "</tr>\n";
3226 }
3227 print "</table>\n";
3228 }
3229
3230 sub git_search_grep_body {
3231 my ($commitlist, $from, $to, $extra) = @_;
3232 $from = 0 unless defined $from;
3233 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3234
3235 print "<table class=\"grep\" cellspacing=\"0\">\n";
3236 my $alternate = 1;
3237 for (my $i = $from; $i <= $to; $i++) {
3238 my %co = %{$commitlist->[$i]};
3239 if (!%co) {
3240 next;
3241 }
3242 my $commit = $co{'id'};
3243 if ($alternate) {
3244 print "<tr class=\"dark\">\n";
3245 } else {
3246 print "<tr class=\"light\">\n";
3247 }
3248 $alternate ^= 1;
3249 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3250 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3251 "<td>" .
3252 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3253 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3254 my $comment = $co{'comment'};
3255 foreach my $line (@$comment) {
3256 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3257 my $lead = esc_html($1) || "";
3258 $lead = chop_str($lead, 30, 10);
3259 my $match = esc_html($2) || "";
3260 my $trail = esc_html($3) || "";
3261 $trail = chop_str($trail, 30, 10);
3262 my $text = "$lead<span class=\"match\">$match</span>$trail";
3263 print chop_str($text, 80, 5) . "<br/>\n";
3264 }
3265 }
3266 print "</td>\n" .
3267 "<td class=\"link\">" .
3268 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3269 " | " .
3270 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3271 print "</td>\n" .
3272 "</tr>\n";
3273 }
3274 if (defined $extra) {
3275 print "<tr>\n" .
3276 "<td colspan=\"3\">$extra</td>\n" .
3277 "</tr>\n";
3278 }
3279 print "</table>\n";
3280 }
3281
3282 ## ======================================================================
3283 ## ======================================================================
3284 ## actions
3285
3286 sub git_project_list {
3287 my $order = $cgi->param('o');
3288 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3289 die_error(undef, "Unknown order parameter");
3290 }
3291
3292 my @list = git_get_projects_list();
3293 if (!@list) {
3294 die_error(undef, "No projects found");
3295 }
3296
3297 git_header_html();
3298 if (-f $home_text) {
3299 print "<div class=\"index_include\">\n";
3300 open (my $fd, $home_text);
3301 print <$fd>;
3302 close $fd;
3303 print "</div>\n";
3304 }
3305 git_project_list_body(\@list, $order);
3306 git_footer_html();
3307 }
3308
3309 sub git_forks {
3310 my $order = $cgi->param('o');
3311 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3312 die_error(undef, "Unknown order parameter");
3313 }
3314
3315 my @list = git_get_projects_list($project);
3316 if (!@list) {
3317 die_error(undef, "No forks found");
3318 }
3319
3320 git_header_html();
3321 git_print_page_nav('','');
3322 git_print_header_div('summary', "$project forks");
3323 git_project_list_body(\@list, $order);
3324 git_footer_html();
3325 }
3326
3327 sub git_project_index {
3328 my @projects = git_get_projects_list($project);
3329
3330 print $cgi->header(
3331 -type => 'text/plain',
3332 -charset => 'utf-8',
3333 -content_disposition => 'inline; filename="index.aux"');
3334
3335 foreach my $pr (@projects) {
3336 if (!exists $pr->{'owner'}) {
3337 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3338 }
3339
3340 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3341 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3342 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3343 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3344 $path =~ s/ /\+/g;
3345 $owner =~ s/ /\+/g;
3346
3347 print "$path $owner\n";
3348 }
3349 }
3350
3351 sub git_summary {
3352 my $descr = git_get_project_description($project) || "none";
3353 my %co = parse_commit("HEAD");
3354 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3355 my $head = $co{'id'};
3356
3357 my $owner = git_get_project_owner($project);
3358
3359 my $refs = git_get_references();
3360 # These get_*_list functions return one more to allow us to see if
3361 # there are more ...
3362 my @taglist = git_get_tags_list(16);
3363 my @headlist = git_get_heads_list(16);
3364 my @forklist;
3365 my ($check_forks) = gitweb_check_feature('forks');
3366
3367 if ($check_forks) {
3368 @forklist = git_get_projects_list($project);
3369 }
3370
3371 git_header_html();
3372 git_print_page_nav('summary','', $head);
3373
3374 print "<div class=\"title\">&nbsp;</div>\n";
3375 print "<table cellspacing=\"0\">\n" .
3376 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3377 "<tr><td>owner</td><td>$owner</td></tr>\n";
3378 if (defined $cd{'rfc2822'}) {
3379 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3380 }
3381
3382 # use per project git URL list in $projectroot/$project/cloneurl
3383 # or make project git URL from git base URL and project name
3384 my $url_tag = "URL";
3385 my @url_list = git_get_project_url_list($project);
3386 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3387 foreach my $git_url (@url_list) {
3388 next unless $git_url;
3389 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3390 $url_tag = "";
3391 }
3392 print "</table>\n";
3393
3394 if (-s "$projectroot/$project/README.html") {
3395 if (open my $fd, "$projectroot/$project/README.html") {
3396 print "<div class=\"title\">readme</div>\n";
3397 print $_ while (<$fd>);
3398 close $fd;
3399 }
3400 }
3401
3402 # we need to request one more than 16 (0..15) to check if
3403 # those 16 are all
3404 my @commitlist = $head ? parse_commits($head, 17) : ();
3405 if (@commitlist) {
3406 git_print_header_div('shortlog');
3407 git_shortlog_body(\@commitlist, 0, 15, $refs,
3408 $#commitlist <= 15 ? undef :
3409 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3410 }
3411
3412 if (@taglist) {
3413 git_print_header_div('tags');
3414 git_tags_body(\@taglist, 0, 15,
3415 $#taglist <= 15 ? undef :
3416 $cgi->a({-href => href(action=>"tags")}, "..."));
3417 }
3418
3419 if (@headlist) {
3420 git_print_header_div('heads');
3421 git_heads_body(\@headlist, $head, 0, 15,
3422 $#headlist <= 15 ? undef :
3423 $cgi->a({-href => href(action=>"heads")}, "..."));
3424 }
3425
3426 if (@forklist) {
3427 git_print_header_div('forks');
3428 git_project_list_body(\@forklist, undef, 0, 15,
3429 $#forklist <= 15 ? undef :
3430 $cgi->a({-href => href(action=>"forks")}, "..."),
3431 'noheader');
3432 }
3433
3434 git_footer_html();
3435 }
3436
3437 sub git_tag {
3438 my $head = git_get_head_hash($project);
3439 git_header_html();
3440 git_print_page_nav('','', $head,undef,$head);
3441 my %tag = parse_tag($hash);
3442
3443 if (! %tag) {
3444 die_error(undef, "Unknown tag object");
3445 }
3446
3447 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3448 print "<div class=\"title_text\">\n" .
3449 "<table cellspacing=\"0\">\n" .
3450 "<tr>\n" .
3451 "<td>object</td>\n" .
3452 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3453 $tag{'object'}) . "</td>\n" .
3454 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3455 $tag{'type'}) . "</td>\n" .
3456 "</tr>\n";
3457 if (defined($tag{'author'})) {
3458 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3459 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3460 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3461 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3462 "</td></tr>\n";
3463 }
3464 print "</table>\n\n" .
3465 "</div>\n";
3466 print "<div class=\"page_body\">";
3467 my $comment = $tag{'comment'};
3468 foreach my $line (@$comment) {
3469 chomp $line;
3470 print esc_html($line, -nbsp=>1) . "<br/>\n";
3471 }
3472 print "</div>\n";
3473 git_footer_html();
3474 }
3475
3476 sub git_blame2 {
3477 my $fd;
3478 my $ftype;
3479
3480 my ($have_blame) = gitweb_check_feature('blame');
3481 if (!$have_blame) {
3482 die_error('403 Permission denied', "Permission denied");
3483 }
3484 die_error('404 Not Found', "File name not defined") if (!$file_name);
3485 $hash_base ||= git_get_head_hash($project);
3486 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3487 my %co = parse_commit($hash_base)
3488 or die_error(undef, "Reading commit failed");
3489 if (!defined $hash) {
3490 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3491 or die_error(undef, "Error looking up file");
3492 }
3493 $ftype = git_get_type($hash);
3494 if ($ftype !~ "blob") {
3495 die_error('400 Bad Request', "Object is not a blob");
3496 }
3497 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3498 $file_name, $hash_base)
3499 or die_error(undef, "Open git-blame failed");
3500 git_header_html();
3501 my $formats_nav =
3502 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3503 "blob") .
3504 " | " .
3505 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3506 "history") .
3507 " | " .
3508 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3509 "HEAD");
3510 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3511 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3512 git_print_page_path($file_name, $ftype, $hash_base);
3513 my @rev_color = (qw(light2 dark2));
3514 my $num_colors = scalar(@rev_color);
3515 my $current_color = 0;
3516 my $last_rev;
3517 print <<HTML;
3518 <div class="page_body">
3519 <table class="blame">
3520 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3521 HTML
3522 my %metainfo = ();
3523 while (1) {
3524 $_ = <$fd>;
3525 last unless defined $_;
3526 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3527 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3528 if (!exists $metainfo{$full_rev}) {
3529 $metainfo{$full_rev} = {};
3530 }
3531 my $meta = $metainfo{$full_rev};
3532 while (<$fd>) {
3533 last if (s/^\t//);
3534 if (/^(\S+) (.*)$/) {
3535 $meta->{$1} = $2;
3536 }
3537 }
3538 my $data = $_;
3539 chomp $data;
3540 my $rev = substr($full_rev, 0, 8);
3541 my $author = $meta->{'author'};
3542 my %date = parse_date($meta->{'author-time'},
3543 $meta->{'author-tz'});
3544 my $date = $date{'iso-tz'};
3545 if ($group_size) {
3546 $current_color = ++$current_color % $num_colors;
3547 }
3548 print "<tr class=\"$rev_color[$current_color]\">\n";
3549 if ($group_size) {
3550 print "<td class=\"sha1\"";
3551 print " title=\"". esc_html($author) . ", $date\"";
3552 print " rowspan=\"$group_size\"" if ($group_size > 1);
3553 print ">";
3554 print $cgi->a({-href => href(action=>"commit",
3555 hash=>$full_rev,
3556 file_name=>$file_name)},
3557 esc_html($rev));
3558 print "</td>\n";
3559 }
3560 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3561 or die_error(undef, "Open git-rev-parse failed");
3562 my $parent_commit = <$dd>;
3563 close $dd;
3564 chomp($parent_commit);
3565 my $blamed = href(action => 'blame',
3566 file_name => $meta->{'filename'},
3567 hash_base => $parent_commit);
3568 print "<td class=\"linenr\">";
3569 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3570 -id => "l$lineno",
3571 -class => "linenr" },
3572 esc_html($lineno));
3573 print "</td>";
3574 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3575 print "</tr>\n";
3576 }
3577 print "</table>\n";
3578 print "</div>";
3579 close $fd
3580 or print "Reading blob failed\n";
3581 git_footer_html();
3582 }
3583
3584 sub git_blame {
3585 my $fd;
3586
3587 my ($have_blame) = gitweb_check_feature('blame');
3588 if (!$have_blame) {
3589 die_error('403 Permission denied', "Permission denied");
3590 }
3591 die_error('404 Not Found', "File name not defined") if (!$file_name);
3592 $hash_base ||= git_get_head_hash($project);
3593 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3594 my %co = parse_commit($hash_base)
3595 or die_error(undef, "Reading commit failed");
3596 if (!defined $hash) {
3597 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3598 or die_error(undef, "Error lookup file");
3599 }
3600 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3601 or die_error(undef, "Open git-annotate failed");
3602 git_header_html();
3603 my $formats_nav =
3604 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3605 "blob") .
3606 " | " .
3607 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3608 "history") .
3609 " | " .
3610 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3611 "HEAD");
3612 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3613 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3614 git_print_page_path($file_name, 'blob', $hash_base);
3615 print "<div class=\"page_body\">\n";
3616 print <<HTML;
3617 <table class="blame">
3618 <tr>
3619 <th>Commit</th>
3620 <th>Age</th>
3621 <th>Author</th>
3622 <th>Line</th>
3623 <th>Data</th>
3624 </tr>
3625 HTML
3626 my @line_class = (qw(light dark));
3627 my $line_class_len = scalar (@line_class);
3628 my $line_class_num = $#line_class;
3629 while (my $line = <$fd>) {
3630 my $long_rev;
3631 my $short_rev;
3632 my $author;
3633 my $time;
3634 my $lineno;
3635 my $data;
3636 my $age;
3637 my $age_str;
3638 my $age_class;
3639
3640 chomp $line;
3641 $line_class_num = ($line_class_num + 1) % $line_class_len;
3642
3643 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3644 $long_rev = $1;
3645 $author = $2;
3646 $time = $3;
3647 $lineno = $4;
3648 $data = $5;
3649 } else {
3650 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3651 next;
3652 }
3653 $short_rev = substr ($long_rev, 0, 8);
3654 $age = time () - $time;
3655 $age_str = age_string ($age);
3656 $age_str =~ s/ /&nbsp;/g;
3657 $age_class = age_class($age);
3658 $author = esc_html ($author);
3659 $author =~ s/ /&nbsp;/g;
3660
3661 $data = untabify($data);
3662 $data = esc_html ($data);
3663
3664 print <<HTML;
3665 <tr class="$line_class[$line_class_num]">
3666 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3667 <td class="$age_class">$age_str</td>
3668 <td>$author</td>
3669 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3670 <td class="pre">$data</td>
3671 </tr>
3672 HTML
3673 } # while (my $line = <$fd>)
3674 print "</table>\n\n";
3675 close $fd
3676 or print "Reading blob failed.\n";
3677 print "</div>";
3678 git_footer_html();
3679 }
3680
3681 sub git_tags {
3682 my $head = git_get_head_hash($project);
3683 git_header_html();
3684 git_print_page_nav('','', $head,undef,$head);
3685 git_print_header_div('summary', $project);
3686
3687 my @tagslist = git_get_tags_list();
3688 if (@tagslist) {
3689 git_tags_body(\@tagslist);
3690 }
3691 git_footer_html();
3692 }
3693
3694 sub git_heads {
3695 my $head = git_get_head_hash($project);
3696 git_header_html();
3697 git_print_page_nav('','', $head,undef,$head);
3698 git_print_header_div('summary', $project);
3699
3700 my @headslist = git_get_heads_list();
3701 if (@headslist) {
3702 git_heads_body(\@headslist, $head);
3703 }
3704 git_footer_html();
3705 }
3706
3707 sub git_blob_plain {
3708 my $expires;
3709
3710 if (!defined $hash) {
3711 if (defined $file_name) {
3712 my $base = $hash_base || git_get_head_hash($project);
3713 $hash = git_get_hash_by_path($base, $file_name, "blob")
3714 or die_error(undef, "Error lookup file");
3715 } else {
3716 die_error(undef, "No file name defined");
3717 }
3718 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3719 # blobs defined by non-textual hash id's can be cached
3720 $expires = "+1d";
3721 }
3722
3723 my $type = shift;
3724 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3725 or die_error(undef, "Couldn't cat $file_name, $hash");
3726
3727 $type ||= blob_mimetype($fd, $file_name);
3728
3729 # save as filename, even when no $file_name is given
3730 my $save_as = "$hash";
3731 if (defined $file_name) {
3732 $save_as = $file_name;
3733 } elsif ($type =~ m/^text\//) {
3734 $save_as .= '.txt';
3735 }
3736
3737 print $cgi->header(
3738 -type => "$type",
3739 -expires=>$expires,
3740 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3741 undef $/;
3742 binmode STDOUT, ':raw';
3743 print <$fd>;
3744 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3745 $/ = "\n";
3746 close $fd;
3747 }
3748
3749 sub git_blob {
3750 my $expires;
3751
3752 if (!defined $hash) {
3753 if (defined $file_name) {
3754 my $base = $hash_base || git_get_head_hash($project);
3755 $hash = git_get_hash_by_path($base, $file_name, "blob")
3756 or die_error(undef, "Error lookup file");
3757 } else {
3758 die_error(undef, "No file name defined");
3759 }
3760 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3761 # blobs defined by non-textual hash id's can be cached
3762 $expires = "+1d";
3763 }
3764
3765 my ($have_blame) = gitweb_check_feature('blame');
3766 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3767 or die_error(undef, "Couldn't cat $file_name, $hash");
3768 my $mimetype = blob_mimetype($fd, $file_name);
3769 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3770 close $fd;
3771 return git_blob_plain($mimetype);
3772 }
3773 # we can have blame only for text/* mimetype
3774 $have_blame &&= ($mimetype =~ m!^text/!);
3775
3776 git_header_html(undef, $expires);
3777 my $formats_nav = '';
3778 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3779 if (defined $file_name) {
3780 if ($have_blame) {
3781 $formats_nav .=
3782 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3783 hash=>$hash, file_name=>$file_name)},
3784 "blame") .
3785 " | ";
3786 }
3787 $formats_nav .=
3788 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3789 hash=>$hash, file_name=>$file_name)},
3790 "history") .
3791 " | " .
3792 $cgi->a({-href => href(action=>"blob_plain",
3793 hash=>$hash, file_name=>$file_name)},
3794 "raw") .
3795 " | " .
3796 $cgi->a({-href => href(action=>"blob",
3797 hash_base=>"HEAD", file_name=>$file_name)},
3798 "HEAD");
3799 } else {
3800 $formats_nav .=
3801 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3802 }
3803 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3804 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3805 } else {
3806 print "<div class=\"page_nav\">\n" .
3807 "<br/><br/></div>\n" .
3808 "<div class=\"title\">$hash</div>\n";
3809 }
3810 git_print_page_path($file_name, "blob", $hash_base);
3811 print "<div class=\"page_body\">\n";
3812 if ($mimetype =~ m!^text/!) {
3813 my $nr;
3814 while (my $line = <$fd>) {
3815 chomp $line;
3816 $nr++;
3817 $line = untabify($line);
3818 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3819 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3820 }
3821 } elsif ($mimetype =~ m!^image/!) {
3822 print qq!<img type="$mimetype"!;
3823 if ($file_name) {
3824 print qq! alt="$file_name" title="$file_name"!;
3825 }
3826 print qq! src="! .
3827 href(action=>"blob_plain", hash=>$hash,
3828 hash_base=>$hash_base, file_name=>$file_name) .
3829 qq!" />\n!;
3830 }
3831 close $fd
3832 or print "Reading blob failed.\n";
3833 print "</div>";
3834 git_footer_html();
3835 }
3836
3837 sub git_tree {
3838 my $have_snapshot = gitweb_have_snapshot();
3839
3840 if (!defined $hash_base) {
3841 $hash_base = "HEAD";
3842 }
3843 if (!defined $hash) {
3844 if (defined $file_name) {
3845 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3846 } else {
3847 $hash = $hash_base;
3848 }
3849 }
3850 $/ = "\0";
3851 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3852 or die_error(undef, "Open git-ls-tree failed");
3853 my @entries = map { chomp; $_ } <$fd>;
3854 close $fd or die_error(undef, "Reading tree failed");
3855 $/ = "\n";
3856
3857 my $refs = git_get_references();
3858 my $ref = format_ref_marker($refs, $hash_base);
3859 git_header_html();
3860 my $basedir = '';
3861 my ($have_blame) = gitweb_check_feature('blame');
3862 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3863 my @views_nav = ();
3864 if (defined $file_name) {
3865 push @views_nav,
3866 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3867 hash=>$hash, file_name=>$file_name)},
3868 "history"),
3869 $cgi->a({-href => href(action=>"tree",
3870 hash_base=>"HEAD", file_name=>$file_name)},
3871 "HEAD"),
3872 }
3873 if ($have_snapshot) {
3874 # FIXME: Should be available when we have no hash base as well.
3875 push @views_nav,
3876 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3877 "snapshot");
3878 }
3879 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3880 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3881 } else {
3882 undef $hash_base;
3883 print "<div class=\"page_nav\">\n";
3884 print "<br/><br/></div>\n";
3885 print "<div class=\"title\">$hash</div>\n";
3886 }
3887 if (defined $file_name) {
3888 $basedir = $file_name;
3889 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3890 $basedir .= '/';
3891 }
3892 }
3893 git_print_page_path($file_name, 'tree', $hash_base);
3894 print "<div class=\"page_body\">\n";
3895 print "<table cellspacing=\"0\">\n";
3896 my $alternate = 1;
3897 # '..' (top directory) link if possible
3898 if (defined $hash_base &&
3899 defined $file_name && $file_name =~ m![^/]+$!) {
3900 if ($alternate) {
3901 print "<tr class=\"dark\">\n";
3902 } else {
3903 print "<tr class=\"light\">\n";
3904 }
3905 $alternate ^= 1;
3906
3907 my $up = $file_name;
3908 $up =~ s!/?[^/]+$!!;
3909 undef $up unless $up;
3910 # based on git_print_tree_entry
3911 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3912 print '<td class="list">';
3913 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3914 file_name=>$up)},
3915 "..");
3916 print "</td>\n";
3917 print "<td class=\"link\"></td>\n";
3918
3919 print "</tr>\n";
3920 }
3921 foreach my $line (@entries) {
3922 my %t = parse_ls_tree_line($line, -z => 1);
3923
3924 if ($alternate) {
3925 print "<tr class=\"dark\">\n";
3926 } else {
3927 print "<tr class=\"light\">\n";
3928 }
3929 $alternate ^= 1;
3930
3931 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3932
3933 print "</tr>\n";
3934 }
3935 print "</table>\n" .
3936 "</div>";
3937 git_footer_html();
3938 }
3939
3940 sub git_snapshot {
3941 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3942 my $have_snapshot = (defined $ctype && defined $suffix);
3943 if (!$have_snapshot) {
3944 die_error('403 Permission denied', "Permission denied");
3945 }
3946
3947 if (!defined $hash) {
3948 $hash = git_get_head_hash($project);
3949 }
3950
3951 my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3952
3953 print $cgi->header(
3954 -type => "application/$ctype",
3955 -content_disposition => 'inline; filename="' . "$filename" . '"',
3956 -status => '200 OK');
3957
3958 my $git = git_cmd_str();
3959 my $name = $project;
3960 $name =~ s/\047/\047\\\047\047/g;
3961 open my $fd, "-|",
3962 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3963 or die_error(undef, "Execute git-tar-tree failed");
3964 binmode STDOUT, ':raw';
3965 print <$fd>;
3966 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3967 close $fd;
3968
3969 }
3970
3971 sub git_log {
3972 my $head = git_get_head_hash($project);
3973 if (!defined $hash) {
3974 $hash = $head;
3975 }
3976 if (!defined $page) {
3977 $page = 0;
3978 }
3979 my $refs = git_get_references();
3980
3981 my @commitlist = parse_commits($hash, 101, (100 * $page));
3982
3983 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3984
3985 git_header_html();
3986 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3987
3988 if (!@commitlist) {
3989 my %co = parse_commit($hash);
3990
3991 git_print_header_div('summary', $project);
3992 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3993 }
3994 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3995 for (my $i = 0; $i <= $to; $i++) {
3996 my %co = %{$commitlist[$i]};
3997 next if !%co;
3998 my $commit = $co{'id'};
3999 my $ref = format_ref_marker($refs, $commit);
4000 my %ad = parse_date($co{'author_epoch'});
4001 git_print_header_div('commit',
4002 "<span class=\"age\">$co{'age_string'}</span>" .
4003 esc_html($co{'title'}) . $ref,
4004 $commit);
4005 print "<div class=\"title_text\">\n" .
4006 "<div class=\"log_link\">\n" .
4007 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4008 " | " .
4009 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4010 " | " .
4011 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4012 "<br/>\n" .
4013 "</div>\n" .
4014 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4015 "</div>\n";
4016
4017 print "<div class=\"log_body\">\n";
4018 git_print_log($co{'comment'}, -final_empty_line=> 1);
4019 print "</div>\n";
4020 }
4021 if ($#commitlist >= 100) {
4022 print "<div class=\"page_nav\">\n";
4023 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4024 -accesskey => "n", -title => "Alt-n"}, "next");
4025 print "</div>\n";
4026 }
4027 git_footer_html();
4028 }
4029
4030 sub git_commit {
4031 $hash ||= $hash_base || "HEAD";
4032 my %co = parse_commit($hash);
4033 if (!%co) {
4034 die_error(undef, "Unknown commit object");
4035 }
4036 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4037 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4038
4039 my $parent = $co{'parent'};
4040 my $parents = $co{'parents'}; # listref
4041
4042 # we need to prepare $formats_nav before any parameter munging
4043 my $formats_nav;
4044 if (!defined $parent) {
4045 # --root commitdiff
4046 $formats_nav .= '(initial)';
4047 } elsif (@$parents == 1) {
4048 # single parent commit
4049 $formats_nav .=
4050 '(parent: ' .
4051 $cgi->a({-href => href(action=>"commit",
4052 hash=>$parent)},
4053 esc_html(substr($parent, 0, 7))) .
4054 ')';
4055 } else {
4056 # merge commit
4057 $formats_nav .=
4058 '(merge: ' .
4059 join(' ', map {
4060 $cgi->a({-href => href(action=>"commit",
4061 hash=>$_)},
4062 esc_html(substr($_, 0, 7)));
4063 } @$parents ) .
4064 ')';
4065 }
4066
4067 if (!defined $parent) {
4068 $parent = "--root";
4069 }
4070 my @difftree;
4071 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4072 @diff_opts,
4073 (@$parents <= 1 ? $parent : '-c'),
4074 $hash, "--"
4075 or die_error(undef, "Open git-diff-tree failed");
4076 @difftree = map { chomp; $_ } <$fd>;
4077 close $fd or die_error(undef, "Reading git-diff-tree failed");
4078
4079 # non-textual hash id's can be cached
4080 my $expires;
4081 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4082 $expires = "+1d";
4083 }
4084 my $refs = git_get_references();
4085 my $ref = format_ref_marker($refs, $co{'id'});
4086
4087 my $have_snapshot = gitweb_have_snapshot();
4088
4089 git_header_html(undef, $expires);
4090 git_print_page_nav('commit', '',
4091 $hash, $co{'tree'}, $hash,
4092 $formats_nav);
4093
4094 if (defined $co{'parent'}) {
4095 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4096 } else {
4097 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4098 }
4099 print "<div class=\"title_text\">\n" .
4100 "<table cellspacing=\"0\">\n";
4101 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4102 "<tr>" .
4103 "<td></td><td> $ad{'rfc2822'}";
4104 if ($ad{'hour_local'} < 6) {
4105 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4106 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4107 } else {
4108 printf(" (%02d:%02d %s)",
4109 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4110 }
4111 print "</td>" .
4112 "</tr>\n";
4113 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4114 print "<tr><td></td><td> $cd{'rfc2822'}" .
4115 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4116 "</td></tr>\n";
4117 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4118 print "<tr>" .
4119 "<td>tree</td>" .
4120 "<td class=\"sha1\">" .
4121 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4122 class => "list"}, $co{'tree'}) .
4123 "</td>" .
4124 "<td class=\"link\">" .
4125 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4126 "tree");
4127 if ($have_snapshot) {
4128 print " | " .
4129 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4130 }
4131 print "</td>" .
4132 "</tr>\n";
4133
4134 foreach my $par (@$parents) {
4135 print "<tr>" .
4136 "<td>parent</td>" .
4137 "<td class=\"sha1\">" .
4138 $cgi->a({-href => href(action=>"commit", hash=>$par),
4139 class => "list"}, $par) .
4140 "</td>" .
4141 "<td class=\"link\">" .
4142 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4143 " | " .
4144 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4145 "</td>" .
4146 "</tr>\n";
4147 }
4148 print "</table>".
4149 "</div>\n";
4150
4151 print "<div class=\"page_body\">\n";
4152 git_print_log($co{'comment'});
4153 print "</div>\n";
4154
4155 git_difftree_body(\@difftree, $hash, @$parents);
4156
4157 git_footer_html();
4158 }
4159
4160 sub git_object {
4161 # object is defined by:
4162 # - hash or hash_base alone
4163 # - hash_base and file_name
4164 my $type;
4165
4166 # - hash or hash_base alone
4167 if ($hash || ($hash_base && !defined $file_name)) {
4168 my $object_id = $hash || $hash_base;
4169
4170 my $git_command = git_cmd_str();
4171 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4172 or die_error('404 Not Found', "Object does not exist");
4173 $type = <$fd>;
4174 chomp $type;
4175 close $fd
4176 or die_error('404 Not Found', "Object does not exist");
4177
4178 # - hash_base and file_name
4179 } elsif ($hash_base && defined $file_name) {
4180 $file_name =~ s,/+$,,;
4181
4182 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4183 or die_error('404 Not Found', "Base object does not exist");
4184
4185 # here errors should not hapen
4186 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4187 or die_error(undef, "Open git-ls-tree failed");
4188 my $line = <$fd>;
4189 close $fd;
4190
4191 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4192 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4193 die_error('404 Not Found', "File or directory for given base does not exist");
4194 }
4195 $type = $2;
4196 $hash = $3;
4197 } else {
4198 die_error('404 Not Found', "Not enough information to find object");
4199 }
4200
4201 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4202 hash=>$hash, hash_base=>$hash_base,
4203 file_name=>$file_name),
4204 -status => '302 Found');
4205 }
4206
4207 sub git_blobdiff {
4208 my $format = shift || 'html';
4209
4210 my $fd;
4211 my @difftree;
4212 my %diffinfo;
4213 my $expires;
4214
4215 # preparing $fd and %diffinfo for git_patchset_body
4216 # new style URI
4217 if (defined $hash_base && defined $hash_parent_base) {
4218 if (defined $file_name) {
4219 # read raw output
4220 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4221 $hash_parent_base, $hash_base,
4222 "--", (defined $file_parent ? $file_parent : ()), $file_name
4223 or die_error(undef, "Open git-diff-tree failed");
4224 @difftree = map { chomp; $_ } <$fd>;
4225 close $fd
4226 or die_error(undef, "Reading git-diff-tree failed");
4227 @difftree
4228 or die_error('404 Not Found', "Blob diff not found");
4229
4230 } elsif (defined $hash &&
4231 $hash =~ /[0-9a-fA-F]{40}/) {
4232 # try to find filename from $hash
4233
4234 # read filtered raw output
4235 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4236 $hash_parent_base, $hash_base, "--"
4237 or die_error(undef, "Open git-diff-tree failed");
4238 @difftree =
4239 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4240 # $hash == to_id
4241 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4242 map { chomp; $_ } <$fd>;
4243 close $fd
4244 or die_error(undef, "Reading git-diff-tree failed");
4245 @difftree
4246 or die_error('404 Not Found', "Blob diff not found");
4247
4248 } else {
4249 die_error('404 Not Found', "Missing one of the blob diff parameters");
4250 }
4251
4252 if (@difftree > 1) {
4253 die_error('404 Not Found', "Ambiguous blob diff specification");
4254 }
4255
4256 %diffinfo = parse_difftree_raw_line($difftree[0]);
4257 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4258 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4259
4260 $hash_parent ||= $diffinfo{'from_id'};
4261 $hash ||= $diffinfo{'to_id'};
4262
4263 # non-textual hash id's can be cached
4264 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4265 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4266 $expires = '+1d';
4267 }
4268
4269 # open patch output
4270 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4271 '-p', ($format eq 'html' ? "--full-index" : ()),
4272 $hash_parent_base, $hash_base,
4273 "--", (defined $file_parent ? $file_parent : ()), $file_name
4274 or die_error(undef, "Open git-diff-tree failed");
4275 }
4276
4277 # old/legacy style URI
4278 if (!%diffinfo && # if new style URI failed
4279 defined $hash && defined $hash_parent) {
4280 # fake git-diff-tree raw output
4281 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4282 $diffinfo{'from_id'} = $hash_parent;
4283 $diffinfo{'to_id'} = $hash;
4284 if (defined $file_name) {
4285 if (defined $file_parent) {
4286 $diffinfo{'status'} = '2';
4287 $diffinfo{'from_file'} = $file_parent;
4288 $diffinfo{'to_file'} = $file_name;
4289 } else { # assume not renamed
4290 $diffinfo{'status'} = '1';
4291 $diffinfo{'from_file'} = $file_name;
4292 $diffinfo{'to_file'} = $file_name;
4293 }
4294 } else { # no filename given
4295 $diffinfo{'status'} = '2';
4296 $diffinfo{'from_file'} = $hash_parent;
4297 $diffinfo{'to_file'} = $hash;
4298 }
4299
4300 # non-textual hash id's can be cached
4301 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4302 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4303 $expires = '+1d';
4304 }
4305
4306 # open patch output
4307 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4308 '-p', ($format eq 'html' ? "--full-index" : ()),
4309 $hash_parent, $hash, "--"
4310 or die_error(undef, "Open git-diff failed");
4311 } else {
4312 die_error('404 Not Found', "Missing one of the blob diff parameters")
4313 unless %diffinfo;
4314 }
4315
4316 # header
4317 if ($format eq 'html') {
4318 my $formats_nav =
4319 $cgi->a({-href => href(action=>"blobdiff_plain",
4320 hash=>$hash, hash_parent=>$hash_parent,
4321 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4322 file_name=>$file_name, file_parent=>$file_parent)},
4323 "raw");
4324 git_header_html(undef, $expires);
4325 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4326 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4327 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4328 } else {
4329 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4330 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4331 }
4332 if (defined $file_name) {
4333 git_print_page_path($file_name, "blob", $hash_base);
4334 } else {
4335 print "<div class=\"page_path\"></div>\n";
4336 }
4337
4338 } elsif ($format eq 'plain') {
4339 print $cgi->header(
4340 -type => 'text/plain',
4341 -charset => 'utf-8',
4342 -expires => $expires,
4343 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4344
4345 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4346
4347 } else {
4348 die_error(undef, "Unknown blobdiff format");
4349 }
4350
4351 # patch
4352 if ($format eq 'html') {
4353 print "<div class=\"page_body\">\n";
4354
4355 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4356 close $fd;
4357
4358 print "</div>\n"; # class="page_body"
4359 git_footer_html();
4360
4361 } else {
4362 while (my $line = <$fd>) {
4363 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4364 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4365
4366 print $line;
4367
4368 last if $line =~ m!^\+\+\+!;
4369 }
4370 local $/ = undef;
4371 print <$fd>;
4372 close $fd;
4373 }
4374 }
4375
4376 sub git_blobdiff_plain {
4377 git_blobdiff('plain');
4378 }
4379
4380 sub git_commitdiff {
4381 my $format = shift || 'html';
4382 $hash ||= $hash_base || "HEAD";
4383 my %co = parse_commit($hash);
4384 if (!%co) {
4385 die_error(undef, "Unknown commit object");
4386 }
4387
4388 # we need to prepare $formats_nav before any parameter munging
4389 my $formats_nav;
4390 if ($format eq 'html') {
4391 $formats_nav =
4392 $cgi->a({-href => href(action=>"commitdiff_plain",
4393 hash=>$hash, hash_parent=>$hash_parent)},
4394 "raw");
4395
4396 if (defined $hash_parent) {
4397 # commitdiff with two commits given
4398 my $hash_parent_short = $hash_parent;
4399 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4400 $hash_parent_short = substr($hash_parent, 0, 7);
4401 }
4402 $formats_nav .=
4403 ' (from: ' .
4404 $cgi->a({-href => href(action=>"commitdiff",
4405 hash=>$hash_parent)},
4406 esc_html($hash_parent_short)) .
4407 ')';
4408 } elsif (!$co{'parent'}) {
4409 # --root commitdiff
4410 $formats_nav .= ' (initial)';
4411 } elsif (scalar @{$co{'parents'}} == 1) {
4412 # single parent commit
4413 $formats_nav .=
4414 ' (parent: ' .
4415 $cgi->a({-href => href(action=>"commitdiff",
4416 hash=>$co{'parent'})},
4417 esc_html(substr($co{'parent'}, 0, 7))) .
4418 ')';
4419 } else {
4420 # merge commit
4421 $formats_nav .=
4422 ' (merge: ' .
4423 join(' ', map {
4424 $cgi->a({-href => href(action=>"commitdiff",
4425 hash=>$_)},
4426 esc_html(substr($_, 0, 7)));
4427 } @{$co{'parents'}} ) .
4428 ')';
4429 }
4430 }
4431
4432 my $hash_parent_param = $hash_parent;
4433 if (!defined $hash_parent) {
4434 $hash_parent_param =
4435 @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4436 }
4437
4438 # read commitdiff
4439 my $fd;
4440 my @difftree;
4441 if ($format eq 'html') {
4442 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4443 "--no-commit-id", "--patch-with-raw", "--full-index",
4444 $hash_parent_param, $hash, "--"
4445 or die_error(undef, "Open git-diff-tree failed");
4446
4447 while (my $line = <$fd>) {
4448 chomp $line;
4449 # empty line ends raw part of diff-tree output
4450 last unless $line;
4451 push @difftree, scalar parse_difftree_raw_line($line);
4452 }
4453
4454 } elsif ($format eq 'plain') {
4455 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4456 '-p', $hash_parent_param, $hash, "--"
4457 or die_error(undef, "Open git-diff-tree failed");
4458
4459 } else {
4460 die_error(undef, "Unknown commitdiff format");
4461 }
4462
4463 # non-textual hash id's can be cached
4464 my $expires;
4465 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4466 $expires = "+1d";
4467 }
4468
4469 # write commit message
4470 if ($format eq 'html') {
4471 my $refs = git_get_references();
4472 my $ref = format_ref_marker($refs, $co{'id'});
4473
4474 git_header_html(undef, $expires);
4475 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4476 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4477 git_print_authorship(\%co);
4478 print "<div class=\"page_body\">\n";
4479 if (@{$co{'comment'}} > 1) {
4480 print "<div class=\"log\">\n";
4481 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4482 print "</div>\n"; # class="log"
4483 }
4484
4485 } elsif ($format eq 'plain') {
4486 my $refs = git_get_references("tags");
4487 my $tagname = git_get_rev_name_tags($hash);
4488 my $filename = basename($project) . "-$hash.patch";
4489
4490 print $cgi->header(
4491 -type => 'text/plain',
4492 -charset => 'utf-8',
4493 -expires => $expires,
4494 -content_disposition => 'inline; filename="' . "$filename" . '"');
4495 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4496 print <<TEXT;
4497 From: $co{'author'}
4498 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4499 Subject: $co{'title'}
4500 TEXT
4501 print "X-Git-Tag: $tagname\n" if $tagname;
4502 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4503
4504 foreach my $line (@{$co{'comment'}}) {
4505 print "$line\n";
4506 }
4507 print "---\n\n";
4508 }
4509
4510 # write patch
4511 if ($format eq 'html') {
4512 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4513 print "<br/>\n";
4514
4515 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4516 close $fd;
4517 print "</div>\n"; # class="page_body"
4518 git_footer_html();
4519
4520 } elsif ($format eq 'plain') {
4521 local $/ = undef;
4522 print <$fd>;
4523 close $fd
4524 or print "Reading git-diff-tree failed\n";
4525 }
4526 }
4527
4528 sub git_commitdiff_plain {
4529 git_commitdiff('plain');
4530 }
4531
4532 sub git_history {
4533 if (!defined $hash_base) {
4534 $hash_base = git_get_head_hash($project);
4535 }
4536 if (!defined $page) {
4537 $page = 0;
4538 }
4539 my $ftype;
4540 my %co = parse_commit($hash_base);
4541 if (!%co) {
4542 die_error(undef, "Unknown commit object");
4543 }
4544
4545 my $refs = git_get_references();
4546 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4547
4548 if (!defined $hash && defined $file_name) {
4549 $hash = git_get_hash_by_path($hash_base, $file_name);
4550 }
4551 if (defined $hash) {
4552 $ftype = git_get_type($hash);
4553 }
4554
4555 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4556
4557 my $paging_nav = '';
4558 if ($page > 0) {
4559 $paging_nav .=
4560 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4561 file_name=>$file_name)},
4562 "first");
4563 $paging_nav .= " &sdot; " .
4564 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4565 file_name=>$file_name, page=>$page-1),
4566 -accesskey => "p", -title => "Alt-p"}, "prev");
4567 } else {
4568 $paging_nav .= "first";
4569 $paging_nav .= " &sdot; prev";
4570 }
4571 if ($#commitlist >= 100) {
4572 $paging_nav .= " &sdot; " .
4573 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4574 file_name=>$file_name, page=>$page+1),
4575 -accesskey => "n", -title => "Alt-n"}, "next");
4576 } else {
4577 $paging_nav .= " &sdot; next";
4578 }
4579 my $next_link = '';
4580 if ($#commitlist >= 100) {
4581 $next_link =
4582 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4583 file_name=>$file_name, page=>$page+1),
4584 -accesskey => "n", -title => "Alt-n"}, "next");
4585 }
4586
4587 git_header_html();
4588 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4589 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4590 git_print_page_path($file_name, $ftype, $hash_base);
4591
4592 git_history_body(\@commitlist, 0, 99,
4593 $refs, $hash_base, $ftype, $next_link);
4594
4595 git_footer_html();
4596 }
4597
4598 sub git_search {
4599 my ($have_search) = gitweb_check_feature('search');
4600 if (!$have_search) {
4601 die_error('403 Permission denied', "Permission denied");
4602 }
4603 if (!defined $searchtext) {
4604 die_error(undef, "Text field empty");
4605 }
4606 if (!defined $hash) {
4607 $hash = git_get_head_hash($project);
4608 }
4609 my %co = parse_commit($hash);
4610 if (!%co) {
4611 die_error(undef, "Unknown commit object");
4612 }
4613 if (!defined $page) {
4614 $page = 0;
4615 }
4616
4617 $searchtype ||= 'commit';
4618 if ($searchtype eq 'pickaxe') {
4619 # pickaxe may take all resources of your box and run for several minutes
4620 # with every query - so decide by yourself how public you make this feature
4621 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4622 if (!$have_pickaxe) {
4623 die_error('403 Permission denied', "Permission denied");
4624 }
4625 }
4626
4627 git_header_html();
4628
4629 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4630 my $greptype;
4631 if ($searchtype eq 'commit') {
4632 $greptype = "--grep=";
4633 } elsif ($searchtype eq 'author') {
4634 $greptype = "--author=";
4635 } elsif ($searchtype eq 'committer') {
4636 $greptype = "--committer=";
4637 }
4638 $greptype .= $search_regexp;
4639 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4640
4641 my $paging_nav = '';
4642 if ($page > 0) {
4643 $paging_nav .=
4644 $cgi->a({-href => href(action=>"search", hash=>$hash,
4645 searchtext=>$searchtext, searchtype=>$searchtype)},
4646 "first");
4647 $paging_nav .= " &sdot; " .
4648 $cgi->a({-href => href(action=>"search", hash=>$hash,
4649 searchtext=>$searchtext, searchtype=>$searchtype,
4650 page=>$page-1),
4651 -accesskey => "p", -title => "Alt-p"}, "prev");
4652 } else {
4653 $paging_nav .= "first";
4654 $paging_nav .= " &sdot; prev";
4655 }
4656 if ($#commitlist >= 100) {
4657 $paging_nav .= " &sdot; " .
4658 $cgi->a({-href => href(action=>"search", hash=>$hash,
4659 searchtext=>$searchtext, searchtype=>$searchtype,
4660 page=>$page+1),
4661 -accesskey => "n", -title => "Alt-n"}, "next");
4662 } else {
4663 $paging_nav .= " &sdot; next";
4664 }
4665 my $next_link = '';
4666 if ($#commitlist >= 100) {
4667 $next_link =
4668 $cgi->a({-href => href(action=>"search", hash=>$hash,
4669 searchtext=>$searchtext, searchtype=>$searchtype,
4670 page=>$page+1),
4671 -accesskey => "n", -title => "Alt-n"}, "next");
4672 }
4673
4674 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4675 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4676 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4677 }
4678
4679 if ($searchtype eq 'pickaxe') {
4680 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4681 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4682
4683 print "<table cellspacing=\"0\">\n";
4684 my $alternate = 1;
4685 $/ = "\n";
4686 my $git_command = git_cmd_str();
4687 open my $fd, "-|", "$git_command rev-list $hash | " .
4688 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4689 undef %co;
4690 my @files;
4691 while (my $line = <$fd>) {
4692 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4693 my %set;
4694 $set{'file'} = $6;
4695 $set{'from_id'} = $3;
4696 $set{'to_id'} = $4;
4697 $set{'id'} = $set{'to_id'};
4698 if ($set{'id'} =~ m/0{40}/) {
4699 $set{'id'} = $set{'from_id'};
4700 }
4701 if ($set{'id'} =~ m/0{40}/) {
4702 next;
4703 }
4704 push @files, \%set;
4705 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4706 if (%co) {
4707 if ($alternate) {
4708 print "<tr class=\"dark\">\n";
4709 } else {
4710 print "<tr class=\"light\">\n";
4711 }
4712 $alternate ^= 1;
4713 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4714 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4715 "<td>" .
4716 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4717 -class => "list subject"},
4718 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4719 while (my $setref = shift @files) {
4720 my %set = %$setref;
4721 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4722 hash=>$set{'id'}, file_name=>$set{'file'}),
4723 -class => "list"},
4724 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4725 "<br/>\n";
4726 }
4727 print "</td>\n" .
4728 "<td class=\"link\">" .
4729 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4730 " | " .
4731 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4732 print "</td>\n" .
4733 "</tr>\n";
4734 }
4735 %co = parse_commit($1);
4736 }
4737 }
4738 close $fd;
4739
4740 print "</table>\n";
4741 }
4742 git_footer_html();
4743 }
4744
4745 sub git_search_help {
4746 git_header_html();
4747 git_print_page_nav('','', $hash,$hash,$hash);
4748 print <<EOT;
4749 <dl>
4750 <dt><b>commit</b></dt>
4751 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4752 <dt><b>author</b></dt>
4753 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4754 <dt><b>committer</b></dt>
4755 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4756 EOT
4757 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4758 if ($have_pickaxe) {
4759 print <<EOT;
4760 <dt><b>pickaxe</b></dt>
4761 <dd>All commits that caused the string to appear or disappear from any file (changes that
4762 added, removed or "modified" the string) will be listed. This search can take a while and
4763 takes a lot of strain on the server, so please use it wisely.</dd>
4764 EOT
4765 }
4766 print "</dl>\n";
4767 git_footer_html();
4768 }
4769
4770 sub git_shortlog {
4771 my $head = git_get_head_hash($project);
4772 if (!defined $hash) {
4773 $hash = $head;
4774 }
4775 if (!defined $page) {
4776 $page = 0;
4777 }
4778 my $refs = git_get_references();
4779
4780 my @commitlist = parse_commits($hash, 101, (100 * $page));
4781
4782 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4783 my $next_link = '';
4784 if ($#commitlist >= 100) {
4785 $next_link =
4786 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4787 -accesskey => "n", -title => "Alt-n"}, "next");
4788 }
4789
4790 git_header_html();
4791 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4792 git_print_header_div('summary', $project);
4793
4794 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4795
4796 git_footer_html();
4797 }
4798
4799 ## ......................................................................
4800 ## feeds (RSS, Atom; OPML)
4801
4802 sub git_feed {
4803 my $format = shift || 'atom';
4804 my ($have_blame) = gitweb_check_feature('blame');
4805
4806 # Atom: http://www.atomenabled.org/developers/syndication/
4807 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4808 if ($format ne 'rss' && $format ne 'atom') {
4809 die_error(undef, "Unknown web feed format");
4810 }
4811
4812 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4813 my $head = $hash || 'HEAD';
4814 my @commitlist = parse_commits($head, 150);
4815
4816 my %latest_commit;
4817 my %latest_date;
4818 my $content_type = "application/$format+xml";
4819 if (defined $cgi->http('HTTP_ACCEPT') &&
4820 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4821 # browser (feed reader) prefers text/xml
4822 $content_type = 'text/xml';
4823 }
4824 if (defined($commitlist[0])) {
4825 %latest_commit = %{$commitlist[0]};
4826 %latest_date = parse_date($latest_commit{'author_epoch'});
4827 print $cgi->header(
4828 -type => $content_type,
4829 -charset => 'utf-8',
4830 -last_modified => $latest_date{'rfc2822'});
4831 } else {
4832 print $cgi->header(
4833 -type => $content_type,
4834 -charset => 'utf-8');
4835 }
4836
4837 # Optimization: skip generating the body if client asks only
4838 # for Last-Modified date.
4839 return if ($cgi->request_method() eq 'HEAD');
4840
4841 # header variables
4842 my $title = "$site_name - $project/$action";
4843 my $feed_type = 'log';
4844 if (defined $hash) {
4845 $title .= " - '$hash'";
4846 $feed_type = 'branch log';
4847 if (defined $file_name) {
4848 $title .= " :: $file_name";
4849 $feed_type = 'history';
4850 }
4851 } elsif (defined $file_name) {
4852 $title .= " - $file_name";
4853 $feed_type = 'history';
4854 }
4855 $title .= " $feed_type";
4856 my $descr = git_get_project_description($project);
4857 if (defined $descr) {
4858 $descr = esc_html($descr);
4859 } else {
4860 $descr = "$project " .
4861 ($format eq 'rss' ? 'RSS' : 'Atom') .
4862 " feed";
4863 }
4864 my $owner = git_get_project_owner($project);
4865 $owner = esc_html($owner);
4866
4867 #header
4868 my $alt_url;
4869 if (defined $file_name) {
4870 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4871 } elsif (defined $hash) {
4872 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4873 } else {
4874 $alt_url = href(-full=>1, action=>"summary");
4875 }
4876 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4877 if ($format eq 'rss') {
4878 print <<XML;
4879 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4880 <channel>
4881 XML
4882 print "<title>$title</title>\n" .
4883 "<link>$alt_url</link>\n" .
4884 "<description>$descr</description>\n" .
4885 "<language>en</language>\n";
4886 } elsif ($format eq 'atom') {
4887 print <<XML;
4888 <feed xmlns="http://www.w3.org/2005/Atom">
4889 XML
4890 print "<title>$title</title>\n" .
4891 "<subtitle>$descr</subtitle>\n" .
4892 '<link rel="alternate" type="text/html" href="' .
4893 $alt_url . '" />' . "\n" .
4894 '<link rel="self" type="' . $content_type . '" href="' .
4895 $cgi->self_url() . '" />' . "\n" .
4896 "<id>" . href(-full=>1) . "</id>\n" .
4897 # use project owner for feed author
4898 "<author><name>$owner</name></author>\n";
4899 if (defined $favicon) {
4900 print "<icon>" . esc_url($favicon) . "</icon>\n";
4901 }
4902 if (defined $logo_url) {
4903 # not twice as wide as tall: 72 x 27 pixels
4904 print "<logo>" . esc_url($logo) . "</logo>\n";
4905 }
4906 if (! %latest_date) {
4907 # dummy date to keep the feed valid until commits trickle in:
4908 print "<updated>1970-01-01T00:00:00Z</updated>\n";
4909 } else {
4910 print "<updated>$latest_date{'iso-8601'}</updated>\n";
4911 }
4912 }
4913
4914 # contents
4915 for (my $i = 0; $i <= $#commitlist; $i++) {
4916 my %co = %{$commitlist[$i]};
4917 my $commit = $co{'id'};
4918 # we read 150, we always show 30 and the ones more recent than 48 hours
4919 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4920 last;
4921 }
4922 my %cd = parse_date($co{'author_epoch'});
4923
4924 # get list of changed files
4925 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4926 $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4927 or next;
4928 my @difftree = map { chomp; $_ } <$fd>;
4929 close $fd
4930 or next;
4931
4932 # print element (entry, item)
4933 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4934 if ($format eq 'rss') {
4935 print "<item>\n" .
4936 "<title>" . esc_html($co{'title'}) . "</title>\n" .
4937 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4938 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4939 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4940 "<link>$co_url</link>\n" .
4941 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4942 "<content:encoded>" .
4943 "<![CDATA[\n";
4944 } elsif ($format eq 'atom') {
4945 print "<entry>\n" .
4946 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4947 "<updated>$cd{'iso-8601'}</updated>\n" .
4948 "<author>\n" .
4949 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
4950 if ($co{'author_email'}) {
4951 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
4952 }
4953 print "</author>\n" .
4954 # use committer for contributor
4955 "<contributor>\n" .
4956 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4957 if ($co{'committer_email'}) {
4958 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4959 }
4960 print "</contributor>\n" .
4961 "<published>$cd{'iso-8601'}</published>\n" .
4962 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4963 "<id>$co_url</id>\n" .
4964 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4965 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4966 }
4967 my $comment = $co{'comment'};
4968 print "<pre>\n";
4969 foreach my $line (@$comment) {
4970 $line = esc_html($line);
4971 print "$line\n";
4972 }
4973 print "</pre><ul>\n";
4974 foreach my $difftree_line (@difftree) {
4975 my %difftree = parse_difftree_raw_line($difftree_line);
4976 next if !$difftree{'from_id'};
4977
4978 my $file = $difftree{'file'} || $difftree{'to_file'};
4979
4980 print "<li>" .
4981 "[" .
4982 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4983 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4984 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4985 file_name=>$file, file_parent=>$difftree{'from_file'}),
4986 -title => "diff"}, 'D');
4987 if ($have_blame) {
4988 print $cgi->a({-href => href(-full=>1, action=>"blame",
4989 file_name=>$file, hash_base=>$commit),
4990 -title => "blame"}, 'B');
4991 }
4992 # if this is not a feed of a file history
4993 if (!defined $file_name || $file_name ne $file) {
4994 print $cgi->a({-href => href(-full=>1, action=>"history",
4995 file_name=>$file, hash=>$commit),
4996 -title => "history"}, 'H');
4997 }
4998 $file = esc_path($file);
4999 print "] ".
5000 "$file</li>\n";
5001 }
5002 if ($format eq 'rss') {
5003 print "</ul>]]>\n" .
5004 "</content:encoded>\n" .
5005 "</item>\n";
5006 } elsif ($format eq 'atom') {
5007 print "</ul>\n</div>\n" .
5008 "</content>\n" .
5009 "</entry>\n";
5010 }
5011 }
5012
5013 # end of feed
5014 if ($format eq 'rss') {
5015 print "</channel>\n</rss>\n";
5016 } elsif ($format eq 'atom') {
5017 print "</feed>\n";
5018 }
5019 }
5020
5021 sub git_rss {
5022 git_feed('rss');
5023 }
5024
5025 sub git_atom {
5026 git_feed('atom');
5027 }
5028
5029 sub git_opml {
5030 my @list = git_get_projects_list();
5031
5032 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5033 print <<XML;
5034 <?xml version="1.0" encoding="utf-8"?>
5035 <opml version="1.0">
5036 <head>
5037 <title>$site_name OPML Export</title>
5038 </head>
5039 <body>
5040 <outline text="git RSS feeds">
5041 XML
5042
5043 foreach my $pr (@list) {
5044 my %proj = %$pr;
5045 my $head = git_get_head_hash($proj{'path'});
5046 if (!defined $head) {
5047 next;
5048 }
5049 $git_dir = "$projectroot/$proj{'path'}";
5050 my %co = parse_commit($head);
5051 if (!%co) {
5052 next;
5053 }
5054
5055 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5056 my $rss = "$my_url?p=$proj{'path'};a=rss";
5057 my $html = "$my_url?p=$proj{'path'};a=summary";
5058 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5059 }
5060 print <<XML;
5061 </outline>
5062 </body>
5063 </opml>
5064 XML
5065 }
This page took 4.818811 seconds and 5 git commands to generate.