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