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