]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
c3b2f360704ffbf717243854c8fed4c1861423e44d60ae9d4217ecab650febae
[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 if ($co{'title'} eq "") {
1124 $co{'title'} = $co{'title_short'} = '(no commit message)';
1125 }
1126 # remove added spaces
1127 foreach my $line (@commit_lines) {
1128 $line =~ s/^ //;
1129 }
1130 $co{'comment'} = \@commit_lines;
1131
1132 my $age = time - $co{'committer_epoch'};
1133 $co{'age'} = $age;
1134 $co{'age_string'} = age_string($age);
1135 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1136 if ($age > 60*60*24*7*2) {
1137 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1138 $co{'age_string_age'} = $co{'age_string'};
1139 } else {
1140 $co{'age_string_date'} = $co{'age_string'};
1141 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1142 }
1143 return %co;
1144 }
1145
1146 # parse ref from ref_file, given by ref_id, with given type
1147 sub parse_ref {
1148 my $ref_file = shift;
1149 my $ref_id = shift;
1150 my $type = shift || git_get_type($ref_id);
1151 my %ref_item;
1152
1153 $ref_item{'type'} = $type;
1154 $ref_item{'id'} = $ref_id;
1155 $ref_item{'epoch'} = 0;
1156 $ref_item{'age'} = "unknown";
1157 if ($type eq "tag") {
1158 my %tag = parse_tag($ref_id);
1159 $ref_item{'comment'} = $tag{'comment'};
1160 if ($tag{'type'} eq "commit") {
1161 my %co = parse_commit($tag{'object'});
1162 $ref_item{'epoch'} = $co{'committer_epoch'};
1163 $ref_item{'age'} = $co{'age_string'};
1164 } elsif (defined($tag{'epoch'})) {
1165 my $age = time - $tag{'epoch'};
1166 $ref_item{'epoch'} = $tag{'epoch'};
1167 $ref_item{'age'} = age_string($age);
1168 }
1169 $ref_item{'reftype'} = $tag{'type'};
1170 $ref_item{'name'} = $tag{'name'};
1171 $ref_item{'refid'} = $tag{'object'};
1172 } elsif ($type eq "commit"){
1173 my %co = parse_commit($ref_id);
1174 $ref_item{'reftype'} = "commit";
1175 $ref_item{'name'} = $ref_file;
1176 $ref_item{'title'} = $co{'title'};
1177 $ref_item{'refid'} = $ref_id;
1178 $ref_item{'epoch'} = $co{'committer_epoch'};
1179 $ref_item{'age'} = $co{'age_string'};
1180 } else {
1181 $ref_item{'reftype'} = $type;
1182 $ref_item{'name'} = $ref_file;
1183 $ref_item{'refid'} = $ref_id;
1184 }
1185
1186 return %ref_item;
1187 }
1188
1189 # parse line of git-diff-tree "raw" output
1190 sub parse_difftree_raw_line {
1191 my $line = shift;
1192 my %res;
1193
1194 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1195 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1196 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1197 $res{'from_mode'} = $1;
1198 $res{'to_mode'} = $2;
1199 $res{'from_id'} = $3;
1200 $res{'to_id'} = $4;
1201 $res{'status'} = $5;
1202 $res{'similarity'} = $6;
1203 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1204 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1205 } else {
1206 $res{'file'} = unquote($7);
1207 }
1208 }
1209 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1210 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1211 $res{'commit'} = $1;
1212 }
1213
1214 return wantarray ? %res : \%res;
1215 }
1216
1217 # parse line of git-ls-tree output
1218 sub parse_ls_tree_line ($;%) {
1219 my $line = shift;
1220 my %opts = @_;
1221 my %res;
1222
1223 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1224 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1225
1226 $res{'mode'} = $1;
1227 $res{'type'} = $2;
1228 $res{'hash'} = $3;
1229 if ($opts{'-z'}) {
1230 $res{'name'} = $4;
1231 } else {
1232 $res{'name'} = unquote($4);
1233 }
1234
1235 return wantarray ? %res : \%res;
1236 }
1237
1238 ## ......................................................................
1239 ## parse to array of hashes functions
1240
1241 sub git_get_refs_list {
1242 my $type = shift || "";
1243 my %refs;
1244 my @reflist;
1245
1246 my @refs;
1247 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1248 or return;
1249 while (my $line = <$fd>) {
1250 chomp $line;
1251 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1252 if (defined $refs{$1}) {
1253 push @{$refs{$1}}, $2;
1254 } else {
1255 $refs{$1} = [ $2 ];
1256 }
1257
1258 if (! $4) { # unpeeled, direct reference
1259 push @refs, { hash => $1, name => $3 }; # without type
1260 } elsif ($3 eq $refs[-1]{'name'}) {
1261 # most likely a tag is followed by its peeled
1262 # (deref) one, and when that happens we know the
1263 # previous one was of type 'tag'.
1264 $refs[-1]{'type'} = "tag";
1265 }
1266 }
1267 }
1268 close $fd;
1269
1270 foreach my $ref (@refs) {
1271 my $ref_file = $ref->{'name'};
1272 my $ref_id = $ref->{'hash'};
1273
1274 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1275 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1276
1277 push @reflist, \%ref_item;
1278 }
1279 # sort refs by age
1280 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1281 return (\@reflist, \%refs);
1282 }
1283
1284 ## ----------------------------------------------------------------------
1285 ## filesystem-related functions
1286
1287 sub get_file_owner {
1288 my $path = shift;
1289
1290 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1291 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1292 if (!defined $gcos) {
1293 return undef;
1294 }
1295 my $owner = $gcos;
1296 $owner =~ s/[,;].*$//;
1297 return to_utf8($owner);
1298 }
1299
1300 ## ......................................................................
1301 ## mimetype related functions
1302
1303 sub mimetype_guess_file {
1304 my $filename = shift;
1305 my $mimemap = shift;
1306 -r $mimemap or return undef;
1307
1308 my %mimemap;
1309 open(MIME, $mimemap) or return undef;
1310 while (<MIME>) {
1311 next if m/^#/; # skip comments
1312 my ($mime, $exts) = split(/\t+/);
1313 if (defined $exts) {
1314 my @exts = split(/\s+/, $exts);
1315 foreach my $ext (@exts) {
1316 $mimemap{$ext} = $mime;
1317 }
1318 }
1319 }
1320 close(MIME);
1321
1322 $filename =~ /\.([^.]*)$/;
1323 return $mimemap{$1};
1324 }
1325
1326 sub mimetype_guess {
1327 my $filename = shift;
1328 my $mime;
1329 $filename =~ /\./ or return undef;
1330
1331 if ($mimetypes_file) {
1332 my $file = $mimetypes_file;
1333 if ($file !~ m!^/!) { # if it is relative path
1334 # it is relative to project
1335 $file = "$projectroot/$project/$file";
1336 }
1337 $mime = mimetype_guess_file($filename, $file);
1338 }
1339 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1340 return $mime;
1341 }
1342
1343 sub blob_mimetype {
1344 my $fd = shift;
1345 my $filename = shift;
1346
1347 if ($filename) {
1348 my $mime = mimetype_guess($filename);
1349 $mime and return $mime;
1350 }
1351
1352 # just in case
1353 return $default_blob_plain_mimetype unless $fd;
1354
1355 if (-T $fd) {
1356 return 'text/plain' .
1357 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1358 } elsif (! $filename) {
1359 return 'application/octet-stream';
1360 } elsif ($filename =~ m/\.png$/i) {
1361 return 'image/png';
1362 } elsif ($filename =~ m/\.gif$/i) {
1363 return 'image/gif';
1364 } elsif ($filename =~ m/\.jpe?g$/i) {
1365 return 'image/jpeg';
1366 } else {
1367 return 'application/octet-stream';
1368 }
1369 }
1370
1371 ## ======================================================================
1372 ## functions printing HTML: header, footer, error page
1373
1374 sub git_header_html {
1375 my $status = shift || "200 OK";
1376 my $expires = shift;
1377
1378 my $title = "$site_name git";
1379 if (defined $project) {
1380 $title .= " - $project";
1381 if (defined $action) {
1382 $title .= "/$action";
1383 if (defined $file_name) {
1384 $title .= " - " . esc_html($file_name);
1385 if ($action eq "tree" && $file_name !~ m|/$|) {
1386 $title .= "/";
1387 }
1388 }
1389 }
1390 }
1391 my $content_type;
1392 # require explicit support from the UA if we are to send the page as
1393 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1394 # we have to do this because MSIE sometimes globs '*/*', pretending to
1395 # support xhtml+xml but choking when it gets what it asked for.
1396 if (defined $cgi->http('HTTP_ACCEPT') &&
1397 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1398 $cgi->Accept('application/xhtml+xml') != 0) {
1399 $content_type = 'application/xhtml+xml';
1400 } else {
1401 $content_type = 'text/html';
1402 }
1403 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1404 -status=> $status, -expires => $expires);
1405 print <<EOF;
1406 <?xml version="1.0" encoding="utf-8"?>
1407 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1408 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1409 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1410 <!-- git core binaries version $git_version -->
1411 <head>
1412 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1413 <meta name="generator" content="gitweb/$version git/$git_version"/>
1414 <meta name="robots" content="index, nofollow"/>
1415 <title>$title</title>
1416 EOF
1417 # print out each stylesheet that exist
1418 if (defined $stylesheet) {
1419 #provides backwards capability for those people who define style sheet in a config file
1420 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1421 } else {
1422 foreach my $stylesheet (@stylesheets) {
1423 next unless $stylesheet;
1424 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1425 }
1426 }
1427 if (defined $project) {
1428 printf('<link rel="alternate" title="%s log" '.
1429 'href="%s" type="application/rss+xml"/>'."\n",
1430 esc_param($project), href(action=>"rss"));
1431 } else {
1432 printf('<link rel="alternate" title="%s projects list" '.
1433 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1434 $site_name, href(project=>undef, action=>"project_index"));
1435 printf('<link rel="alternate" title="%s projects logs" '.
1436 'href="%s" type="text/x-opml"/>'."\n",
1437 $site_name, href(project=>undef, action=>"opml"));
1438 }
1439 if (defined $favicon) {
1440 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1441 }
1442
1443 print "</head>\n" .
1444 "<body>\n";
1445
1446 if (-f $site_header) {
1447 open (my $fd, $site_header);
1448 print <$fd>;
1449 close $fd;
1450 }
1451
1452 print "<div class=\"page_header\">\n" .
1453 "<a href=\"" . esc_html($githelp_url) .
1454 "\" title=\"" . esc_html($githelp_label) .
1455 "\">" .
1456 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1457 "</a>\n";
1458 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1459 if (defined $project) {
1460 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1461 if (defined $action) {
1462 print " / $action";
1463 }
1464 print "\n";
1465 if (!defined $searchtext) {
1466 $searchtext = "";
1467 }
1468 my $search_hash;
1469 if (defined $hash_base) {
1470 $search_hash = $hash_base;
1471 } elsif (defined $hash) {
1472 $search_hash = $hash;
1473 } else {
1474 $search_hash = "HEAD";
1475 }
1476 $cgi->param("a", "search");
1477 $cgi->param("h", $search_hash);
1478 print $cgi->startform(-method => "get", -action => $my_uri) .
1479 "<div class=\"search\">\n" .
1480 $cgi->hidden(-name => "p") . "\n" .
1481 $cgi->hidden(-name => "a") . "\n" .
1482 $cgi->hidden(-name => "h") . "\n" .
1483 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1484 "</div>" .
1485 $cgi->end_form() . "\n";
1486 }
1487 print "</div>\n";
1488 }
1489
1490 sub git_footer_html {
1491 print "<div class=\"page_footer\">\n";
1492 if (defined $project) {
1493 my $descr = git_get_project_description($project);
1494 if (defined $descr) {
1495 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1496 }
1497 print $cgi->a({-href => href(action=>"rss"),
1498 -class => "rss_logo"}, "RSS") . "\n";
1499 } else {
1500 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1501 -class => "rss_logo"}, "OPML") . " ";
1502 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1503 -class => "rss_logo"}, "TXT") . "\n";
1504 }
1505 print "</div>\n" ;
1506
1507 if (-f $site_footer) {
1508 open (my $fd, $site_footer);
1509 print <$fd>;
1510 close $fd;
1511 }
1512
1513 print "</body>\n" .
1514 "</html>";
1515 }
1516
1517 sub die_error {
1518 my $status = shift || "403 Forbidden";
1519 my $error = shift || "Malformed query, file missing or permission denied";
1520
1521 git_header_html($status);
1522 print <<EOF;
1523 <div class="page_body">
1524 <br /><br />
1525 $status - $error
1526 <br />
1527 </div>
1528 EOF
1529 git_footer_html();
1530 exit;
1531 }
1532
1533 ## ----------------------------------------------------------------------
1534 ## functions printing or outputting HTML: navigation
1535
1536 sub git_print_page_nav {
1537 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1538 $extra = '' if !defined $extra; # pager or formats
1539
1540 my @navs = qw(summary shortlog log commit commitdiff tree);
1541 if ($suppress) {
1542 @navs = grep { $_ ne $suppress } @navs;
1543 }
1544
1545 my %arg = map { $_ => {action=>$_} } @navs;
1546 if (defined $head) {
1547 for (qw(commit commitdiff)) {
1548 $arg{$_}{hash} = $head;
1549 }
1550 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1551 for (qw(shortlog log)) {
1552 $arg{$_}{hash} = $head;
1553 }
1554 }
1555 }
1556 $arg{tree}{hash} = $treehead if defined $treehead;
1557 $arg{tree}{hash_base} = $treebase if defined $treebase;
1558
1559 print "<div class=\"page_nav\">\n" .
1560 (join " | ",
1561 map { $_ eq $current ?
1562 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1563 } @navs);
1564 print "<br/>\n$extra<br/>\n" .
1565 "</div>\n";
1566 }
1567
1568 sub format_paging_nav {
1569 my ($action, $hash, $head, $page, $nrevs) = @_;
1570 my $paging_nav;
1571
1572
1573 if ($hash ne $head || $page) {
1574 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1575 } else {
1576 $paging_nav .= "HEAD";
1577 }
1578
1579 if ($page > 0) {
1580 $paging_nav .= " &sdot; " .
1581 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1582 -accesskey => "p", -title => "Alt-p"}, "prev");
1583 } else {
1584 $paging_nav .= " &sdot; prev";
1585 }
1586
1587 if ($nrevs >= (100 * ($page+1)-1)) {
1588 $paging_nav .= " &sdot; " .
1589 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1590 -accesskey => "n", -title => "Alt-n"}, "next");
1591 } else {
1592 $paging_nav .= " &sdot; next";
1593 }
1594
1595 return $paging_nav;
1596 }
1597
1598 ## ......................................................................
1599 ## functions printing or outputting HTML: div
1600
1601 sub git_print_header_div {
1602 my ($action, $title, $hash, $hash_base) = @_;
1603 my %args = ();
1604
1605 $args{action} = $action;
1606 $args{hash} = $hash if $hash;
1607 $args{hash_base} = $hash_base if $hash_base;
1608
1609 print "<div class=\"header\">\n" .
1610 $cgi->a({-href => href(%args), -class => "title"},
1611 $title ? $title : $action) .
1612 "\n</div>\n";
1613 }
1614
1615 #sub git_print_authorship (\%) {
1616 sub git_print_authorship {
1617 my $co = shift;
1618
1619 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1620 print "<div class=\"author_date\">" .
1621 esc_html($co->{'author_name'}) .
1622 " [$ad{'rfc2822'}";
1623 if ($ad{'hour_local'} < 6) {
1624 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1625 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1626 } else {
1627 printf(" (%02d:%02d %s)",
1628 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1629 }
1630 print "]</div>\n";
1631 }
1632
1633 sub git_print_page_path {
1634 my $name = shift;
1635 my $type = shift;
1636 my $hb = shift;
1637
1638 if (!defined $name) {
1639 print "<div class=\"page_path\">/</div>\n";
1640 } else {
1641 my @dirname = split '/', $name;
1642 my $basename = pop @dirname;
1643 my $fullname = '';
1644
1645 print "<div class=\"page_path\">";
1646 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1647 -title => 'tree root'}, "[$project]");
1648 print " / ";
1649 foreach my $dir (@dirname) {
1650 $fullname .= ($fullname ? '/' : '') . $dir;
1651 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1652 hash_base=>$hb),
1653 -title => $fullname}, esc_html($dir));
1654 print " / ";
1655 }
1656 if (defined $type && $type eq 'blob') {
1657 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1658 hash_base=>$hb),
1659 -title => $name}, esc_html($basename));
1660 } elsif (defined $type && $type eq 'tree') {
1661 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1662 hash_base=>$hb),
1663 -title => $name}, esc_html($basename));
1664 } else {
1665 print esc_html($basename);
1666 }
1667 print "<br/></div>\n";
1668 }
1669 }
1670
1671 # sub git_print_log (\@;%) {
1672 sub git_print_log ($;%) {
1673 my $log = shift;
1674 my %opts = @_;
1675
1676 if ($opts{'-remove_title'}) {
1677 # remove title, i.e. first line of log
1678 shift @$log;
1679 }
1680 # remove leading empty lines
1681 while (defined $log->[0] && $log->[0] eq "") {
1682 shift @$log;
1683 }
1684
1685 # print log
1686 my $signoff = 0;
1687 my $empty = 0;
1688 foreach my $line (@$log) {
1689 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1690 $signoff = 1;
1691 $empty = 0;
1692 if (! $opts{'-remove_signoff'}) {
1693 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1694 next;
1695 } else {
1696 # remove signoff lines
1697 next;
1698 }
1699 } else {
1700 $signoff = 0;
1701 }
1702
1703 # print only one empty line
1704 # do not print empty line after signoff
1705 if ($line eq "") {
1706 next if ($empty || $signoff);
1707 $empty = 1;
1708 } else {
1709 $empty = 0;
1710 }
1711
1712 print format_log_line_html($line) . "<br/>\n";
1713 }
1714
1715 if ($opts{'-final_empty_line'}) {
1716 # end with single empty line
1717 print "<br/>\n" unless $empty;
1718 }
1719 }
1720
1721 sub git_print_simplified_log {
1722 my $log = shift;
1723 my $remove_title = shift;
1724
1725 git_print_log($log,
1726 -final_empty_line=> 1,
1727 -remove_title => $remove_title);
1728 }
1729
1730 # print tree entry (row of git_tree), but without encompassing <tr> element
1731 sub git_print_tree_entry {
1732 my ($t, $basedir, $hash_base, $have_blame) = @_;
1733
1734 my %base_key = ();
1735 $base_key{hash_base} = $hash_base if defined $hash_base;
1736
1737 # The format of a table row is: mode list link. Where mode is
1738 # the mode of the entry, list is the name of the entry, an href,
1739 # and link is the action links of the entry.
1740
1741 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1742 if ($t->{'type'} eq "blob") {
1743 print "<td class=\"list\">" .
1744 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1745 file_name=>"$basedir$t->{'name'}", %base_key),
1746 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1747 print "<td class=\"link\">";
1748 if ($have_blame) {
1749 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1750 file_name=>"$basedir$t->{'name'}", %base_key)},
1751 "blame");
1752 }
1753 if (defined $hash_base) {
1754 if ($have_blame) {
1755 print " | ";
1756 }
1757 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1758 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1759 "history");
1760 }
1761 print " | " .
1762 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1763 file_name=>"$basedir$t->{'name'}")},
1764 "raw");
1765 print "</td>\n";
1766
1767 } elsif ($t->{'type'} eq "tree") {
1768 print "<td class=\"list\">";
1769 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1770 file_name=>"$basedir$t->{'name'}", %base_key)},
1771 esc_html($t->{'name'}));
1772 print "</td>\n";
1773 print "<td class=\"link\">";
1774 if (defined $hash_base) {
1775 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1776 file_name=>"$basedir$t->{'name'}")},
1777 "history");
1778 }
1779 print "</td>\n";
1780 }
1781 }
1782
1783 ## ......................................................................
1784 ## functions printing large fragments of HTML
1785
1786 sub git_difftree_body {
1787 my ($difftree, $hash, $parent) = @_;
1788
1789 print "<div class=\"list_head\">\n";
1790 if ($#{$difftree} > 10) {
1791 print(($#{$difftree} + 1) . " files changed:\n");
1792 }
1793 print "</div>\n";
1794
1795 print "<table class=\"diff_tree\">\n";
1796 my $alternate = 1;
1797 my $patchno = 0;
1798 foreach my $line (@{$difftree}) {
1799 my %diff = parse_difftree_raw_line($line);
1800
1801 if ($alternate) {
1802 print "<tr class=\"dark\">\n";
1803 } else {
1804 print "<tr class=\"light\">\n";
1805 }
1806 $alternate ^= 1;
1807
1808 my ($to_mode_oct, $to_mode_str, $to_file_type);
1809 my ($from_mode_oct, $from_mode_str, $from_file_type);
1810 if ($diff{'to_mode'} ne ('0' x 6)) {
1811 $to_mode_oct = oct $diff{'to_mode'};
1812 if (S_ISREG($to_mode_oct)) { # only for regular file
1813 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1814 }
1815 $to_file_type = file_type($diff{'to_mode'});
1816 }
1817 if ($diff{'from_mode'} ne ('0' x 6)) {
1818 $from_mode_oct = oct $diff{'from_mode'};
1819 if (S_ISREG($to_mode_oct)) { # only for regular file
1820 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1821 }
1822 $from_file_type = file_type($diff{'from_mode'});
1823 }
1824
1825 if ($diff{'status'} eq "A") { # created
1826 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1827 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1828 $mode_chng .= "]</span>";
1829 print "<td>";
1830 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1831 hash_base=>$hash, file_name=>$diff{'file'}),
1832 -class => "list"}, esc_html($diff{'file'}));
1833 print "</td>\n";
1834 print "<td>$mode_chng</td>\n";
1835 print "<td class=\"link\">";
1836 if ($action eq 'commitdiff') {
1837 # link to patch
1838 $patchno++;
1839 print $cgi->a({-href => "#patch$patchno"}, "patch");
1840 }
1841 print "</td>\n";
1842
1843 } elsif ($diff{'status'} eq "D") { # deleted
1844 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1845 print "<td>";
1846 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1847 hash_base=>$parent, file_name=>$diff{'file'}),
1848 -class => "list"}, esc_html($diff{'file'}));
1849 print "</td>\n";
1850 print "<td>$mode_chng</td>\n";
1851 print "<td class=\"link\">";
1852 if ($action eq 'commitdiff') {
1853 # link to patch
1854 $patchno++;
1855 print $cgi->a({-href => "#patch$patchno"}, "patch");
1856 print " | ";
1857 }
1858 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1859 file_name=>$diff{'file'})},
1860 "blame") . " | ";
1861 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1862 file_name=>$diff{'file'})},
1863 "history");
1864 print "</td>\n";
1865
1866 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1867 my $mode_chnge = "";
1868 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1869 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1870 if ($from_file_type != $to_file_type) {
1871 $mode_chnge .= " from $from_file_type to $to_file_type";
1872 }
1873 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1874 if ($from_mode_str && $to_mode_str) {
1875 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1876 } elsif ($to_mode_str) {
1877 $mode_chnge .= " mode: $to_mode_str";
1878 }
1879 }
1880 $mode_chnge .= "]</span>\n";
1881 }
1882 print "<td>";
1883 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1884 hash_base=>$hash, file_name=>$diff{'file'}),
1885 -class => "list"}, esc_html($diff{'file'}));
1886 print "</td>\n";
1887 print "<td>$mode_chnge</td>\n";
1888 print "<td class=\"link\">";
1889 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1890 if ($action eq 'commitdiff') {
1891 # link to patch
1892 $patchno++;
1893 print $cgi->a({-href => "#patch$patchno"}, "patch");
1894 } else {
1895 print $cgi->a({-href => href(action=>"blobdiff",
1896 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1897 hash_base=>$hash, hash_parent_base=>$parent,
1898 file_name=>$diff{'file'})},
1899 "diff");
1900 }
1901 print " | ";
1902 }
1903 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1904 file_name=>$diff{'file'})},
1905 "blame") . " | ";
1906 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1907 file_name=>$diff{'file'})},
1908 "history");
1909 print "</td>\n";
1910
1911 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1912 my %status_name = ('R' => 'moved', 'C' => 'copied');
1913 my $nstatus = $status_name{$diff{'status'}};
1914 my $mode_chng = "";
1915 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1916 # mode also for directories, so we cannot use $to_mode_str
1917 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1918 }
1919 print "<td>" .
1920 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1921 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1922 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1923 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1924 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1925 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1926 -class => "list"}, esc_html($diff{'from_file'})) .
1927 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1928 "<td class=\"link\">";
1929 if ($diff{'to_id'} ne $diff{'from_id'}) {
1930 if ($action eq 'commitdiff') {
1931 # link to patch
1932 $patchno++;
1933 print $cgi->a({-href => "#patch$patchno"}, "patch");
1934 } else {
1935 print $cgi->a({-href => href(action=>"blobdiff",
1936 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1937 hash_base=>$hash, hash_parent_base=>$parent,
1938 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1939 "diff");
1940 }
1941 print " | ";
1942 }
1943 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1944 file_name=>$diff{'from_file'})},
1945 "blame") . " | ";
1946 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1947 file_name=>$diff{'from_file'})},
1948 "history");
1949 print "</td>\n";
1950
1951 } # we should not encounter Unmerged (U) or Unknown (X) status
1952 print "</tr>\n";
1953 }
1954 print "</table>\n";
1955 }
1956
1957 sub git_patchset_body {
1958 my ($fd, $difftree, $hash, $hash_parent) = @_;
1959
1960 my $patch_idx = 0;
1961 my $in_header = 0;
1962 my $patch_found = 0;
1963 my $diffinfo;
1964
1965 print "<div class=\"patchset\">\n";
1966
1967 LINE:
1968 while (my $patch_line = <$fd>) {
1969 chomp $patch_line;
1970
1971 if ($patch_line =~ m/^diff /) { # "git diff" header
1972 # beginning of patch (in patchset)
1973 if ($patch_found) {
1974 # close previous patch
1975 print "</div>\n"; # class="patch"
1976 } else {
1977 # first patch in patchset
1978 $patch_found = 1;
1979 }
1980 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1981
1982 if (ref($difftree->[$patch_idx]) eq "HASH") {
1983 $diffinfo = $difftree->[$patch_idx];
1984 } else {
1985 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1986 }
1987 $patch_idx++;
1988
1989 # for now, no extended header, hence we skip empty patches
1990 # companion to next LINE if $in_header;
1991 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1992 $in_header = 1;
1993 next LINE;
1994 }
1995
1996 if ($diffinfo->{'status'} eq "A") { # added
1997 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1998 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1999 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2000 $diffinfo->{'to_id'}) . " (new)" .
2001 "</div>\n"; # class="diff_info"
2002
2003 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2004 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2005 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2006 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2007 $diffinfo->{'from_id'}) . " (deleted)" .
2008 "</div>\n"; # class="diff_info"
2009
2010 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2011 $diffinfo->{'status'} eq "C" || # copied
2012 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2013 print "<div class=\"diff_info\">" .
2014 file_type($diffinfo->{'from_mode'}) . ":" .
2015 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2016 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2017 $diffinfo->{'from_id'}) .
2018 " -> " .
2019 file_type($diffinfo->{'to_mode'}) . ":" .
2020 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2021 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2022 $diffinfo->{'to_id'});
2023 print "</div>\n"; # class="diff_info"
2024
2025 } else { # modified, mode changed, ...
2026 print "<div class=\"diff_info\">" .
2027 file_type($diffinfo->{'from_mode'}) . ":" .
2028 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2029 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2030 $diffinfo->{'from_id'}) .
2031 " -> " .
2032 file_type($diffinfo->{'to_mode'}) . ":" .
2033 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2034 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2035 $diffinfo->{'to_id'});
2036 print "</div>\n"; # class="diff_info"
2037 }
2038
2039 #print "<div class=\"diff extended_header\">\n";
2040 $in_header = 1;
2041 next LINE;
2042 } # start of patch in patchset
2043
2044
2045 if ($in_header && $patch_line =~ m/^---/) {
2046 #print "</div>\n"; # class="diff extended_header"
2047 $in_header = 0;
2048
2049 my $file = $diffinfo->{'from_file'};
2050 $file ||= $diffinfo->{'file'};
2051 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2052 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2053 -class => "list"}, esc_html($file));
2054 $patch_line =~ s|a/.*$|a/$file|g;
2055 print "<div class=\"diff from_file\">$patch_line</div>\n";
2056
2057 $patch_line = <$fd>;
2058 chomp $patch_line;
2059
2060 #$patch_line =~ m/^+++/;
2061 $file = $diffinfo->{'to_file'};
2062 $file ||= $diffinfo->{'file'};
2063 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2064 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2065 -class => "list"}, esc_html($file));
2066 $patch_line =~ s|b/.*|b/$file|g;
2067 print "<div class=\"diff to_file\">$patch_line</div>\n";
2068
2069 next LINE;
2070 }
2071 next LINE if $in_header;
2072
2073 print format_diff_line($patch_line);
2074 }
2075 print "</div>\n" if $patch_found; # class="patch"
2076
2077 print "</div>\n"; # class="patchset"
2078 }
2079
2080 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2081
2082 sub git_shortlog_body {
2083 # uses global variable $project
2084 my ($revlist, $from, $to, $refs, $extra) = @_;
2085
2086 $from = 0 unless defined $from;
2087 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2088
2089 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2090 my $alternate = 1;
2091 for (my $i = $from; $i <= $to; $i++) {
2092 my $commit = $revlist->[$i];
2093 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2094 my $ref = format_ref_marker($refs, $commit);
2095 my %co = parse_commit($commit);
2096 if ($alternate) {
2097 print "<tr class=\"dark\">\n";
2098 } else {
2099 print "<tr class=\"light\">\n";
2100 }
2101 $alternate ^= 1;
2102 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2103 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2104 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2105 "<td>";
2106 print format_subject_html($co{'title'}, $co{'title_short'},
2107 href(action=>"commit", hash=>$commit), $ref);
2108 print "</td>\n" .
2109 "<td class=\"link\">" .
2110 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2111 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2112 if (gitweb_have_snapshot()) {
2113 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2114 }
2115 print "</td>\n" .
2116 "</tr>\n";
2117 }
2118 if (defined $extra) {
2119 print "<tr>\n" .
2120 "<td colspan=\"4\">$extra</td>\n" .
2121 "</tr>\n";
2122 }
2123 print "</table>\n";
2124 }
2125
2126 sub git_history_body {
2127 # Warning: assumes constant type (blob or tree) during history
2128 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2129
2130 $from = 0 unless defined $from;
2131 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2132
2133 print "<table class=\"history\" cellspacing=\"0\">\n";
2134 my $alternate = 1;
2135 for (my $i = $from; $i <= $to; $i++) {
2136 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2137 next;
2138 }
2139
2140 my $commit = $1;
2141 my %co = parse_commit($commit);
2142 if (!%co) {
2143 next;
2144 }
2145
2146 my $ref = format_ref_marker($refs, $commit);
2147
2148 if ($alternate) {
2149 print "<tr class=\"dark\">\n";
2150 } else {
2151 print "<tr class=\"light\">\n";
2152 }
2153 $alternate ^= 1;
2154 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2155 # shortlog uses chop_str($co{'author_name'}, 10)
2156 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2157 "<td>";
2158 # originally git_history used chop_str($co{'title'}, 50)
2159 print format_subject_html($co{'title'}, $co{'title_short'},
2160 href(action=>"commit", hash=>$commit), $ref);
2161 print "</td>\n" .
2162 "<td class=\"link\">" .
2163 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2164 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2165
2166 if ($ftype eq 'blob') {
2167 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2168 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2169 if (defined $blob_current && defined $blob_parent &&
2170 $blob_current ne $blob_parent) {
2171 print " | " .
2172 $cgi->a({-href => href(action=>"blobdiff",
2173 hash=>$blob_current, hash_parent=>$blob_parent,
2174 hash_base=>$hash_base, hash_parent_base=>$commit,
2175 file_name=>$file_name)},
2176 "diff to current");
2177 }
2178 }
2179 print "</td>\n" .
2180 "</tr>\n";
2181 }
2182 if (defined $extra) {
2183 print "<tr>\n" .
2184 "<td colspan=\"4\">$extra</td>\n" .
2185 "</tr>\n";
2186 }
2187 print "</table>\n";
2188 }
2189
2190 sub git_tags_body {
2191 # uses global variable $project
2192 my ($taglist, $from, $to, $extra) = @_;
2193 $from = 0 unless defined $from;
2194 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2195
2196 print "<table class=\"tags\" cellspacing=\"0\">\n";
2197 my $alternate = 1;
2198 for (my $i = $from; $i <= $to; $i++) {
2199 my $entry = $taglist->[$i];
2200 my %tag = %$entry;
2201 my $comment_lines = $tag{'comment'};
2202 my $comment = shift @$comment_lines;
2203 my $comment_short;
2204 if (defined $comment) {
2205 $comment_short = chop_str($comment, 30, 5);
2206 }
2207 if ($alternate) {
2208 print "<tr class=\"dark\">\n";
2209 } else {
2210 print "<tr class=\"light\">\n";
2211 }
2212 $alternate ^= 1;
2213 print "<td><i>$tag{'age'}</i></td>\n" .
2214 "<td>" .
2215 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2216 -class => "list name"}, esc_html($tag{'name'})) .
2217 "</td>\n" .
2218 "<td>";
2219 if (defined $comment) {
2220 print format_subject_html($comment, $comment_short,
2221 href(action=>"tag", hash=>$tag{'id'}));
2222 }
2223 print "</td>\n" .
2224 "<td class=\"selflink\">";
2225 if ($tag{'type'} eq "tag") {
2226 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2227 } else {
2228 print "&nbsp;";
2229 }
2230 print "</td>\n" .
2231 "<td class=\"link\">" . " | " .
2232 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2233 if ($tag{'reftype'} eq "commit") {
2234 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2235 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2236 } elsif ($tag{'reftype'} eq "blob") {
2237 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2238 }
2239 print "</td>\n" .
2240 "</tr>";
2241 }
2242 if (defined $extra) {
2243 print "<tr>\n" .
2244 "<td colspan=\"5\">$extra</td>\n" .
2245 "</tr>\n";
2246 }
2247 print "</table>\n";
2248 }
2249
2250 sub git_heads_body {
2251 # uses global variable $project
2252 my ($headlist, $head, $from, $to, $extra) = @_;
2253 $from = 0 unless defined $from;
2254 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2255
2256 print "<table class=\"heads\" cellspacing=\"0\">\n";
2257 my $alternate = 1;
2258 for (my $i = $from; $i <= $to; $i++) {
2259 my $entry = $headlist->[$i];
2260 my %tag = %$entry;
2261 my $curr = $tag{'id'} eq $head;
2262 if ($alternate) {
2263 print "<tr class=\"dark\">\n";
2264 } else {
2265 print "<tr class=\"light\">\n";
2266 }
2267 $alternate ^= 1;
2268 print "<td><i>$tag{'age'}</i></td>\n" .
2269 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2270 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2271 -class => "list name"},esc_html($tag{'name'})) .
2272 "</td>\n" .
2273 "<td class=\"link\">" .
2274 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2275 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2276 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2277 "</td>\n" .
2278 "</tr>";
2279 }
2280 if (defined $extra) {
2281 print "<tr>\n" .
2282 "<td colspan=\"3\">$extra</td>\n" .
2283 "</tr>\n";
2284 }
2285 print "</table>\n";
2286 }
2287
2288 ## ======================================================================
2289 ## ======================================================================
2290 ## actions
2291
2292 sub git_project_list {
2293 my $order = $cgi->param('o');
2294 if (defined $order && $order !~ m/project|descr|owner|age/) {
2295 die_error(undef, "Unknown order parameter");
2296 }
2297
2298 my @list = git_get_projects_list();
2299 my @projects;
2300 if (!@list) {
2301 die_error(undef, "No projects found");
2302 }
2303 foreach my $pr (@list) {
2304 my $head = git_get_head_hash($pr->{'path'});
2305 if (!defined $head) {
2306 next;
2307 }
2308 $git_dir = "$projectroot/$pr->{'path'}";
2309 my %co = parse_commit($head);
2310 if (!%co) {
2311 next;
2312 }
2313 $pr->{'commit'} = \%co;
2314 if (!defined $pr->{'descr'}) {
2315 my $descr = git_get_project_description($pr->{'path'}) || "";
2316 $pr->{'descr'} = chop_str($descr, 25, 5);
2317 }
2318 if (!defined $pr->{'owner'}) {
2319 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2320 }
2321 push @projects, $pr;
2322 }
2323
2324 git_header_html();
2325 if (-f $home_text) {
2326 print "<div class=\"index_include\">\n";
2327 open (my $fd, $home_text);
2328 print <$fd>;
2329 close $fd;
2330 print "</div>\n";
2331 }
2332 print "<table class=\"project_list\">\n" .
2333 "<tr>\n";
2334 $order ||= "project";
2335 if ($order eq "project") {
2336 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2337 print "<th>Project</th>\n";
2338 } else {
2339 print "<th>" .
2340 $cgi->a({-href => href(project=>undef, order=>'project'),
2341 -class => "header"}, "Project") .
2342 "</th>\n";
2343 }
2344 if ($order eq "descr") {
2345 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2346 print "<th>Description</th>\n";
2347 } else {
2348 print "<th>" .
2349 $cgi->a({-href => href(project=>undef, order=>'descr'),
2350 -class => "header"}, "Description") .
2351 "</th>\n";
2352 }
2353 if ($order eq "owner") {
2354 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2355 print "<th>Owner</th>\n";
2356 } else {
2357 print "<th>" .
2358 $cgi->a({-href => href(project=>undef, order=>'owner'),
2359 -class => "header"}, "Owner") .
2360 "</th>\n";
2361 }
2362 if ($order eq "age") {
2363 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2364 print "<th>Last Change</th>\n";
2365 } else {
2366 print "<th>" .
2367 $cgi->a({-href => href(project=>undef, order=>'age'),
2368 -class => "header"}, "Last Change") .
2369 "</th>\n";
2370 }
2371 print "<th></th>\n" .
2372 "</tr>\n";
2373 my $alternate = 1;
2374 foreach my $pr (@projects) {
2375 if ($alternate) {
2376 print "<tr class=\"dark\">\n";
2377 } else {
2378 print "<tr class=\"light\">\n";
2379 }
2380 $alternate ^= 1;
2381 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2382 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2383 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2384 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2385 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2386 $pr->{'commit'}{'age_string'} . "</td>\n" .
2387 "<td class=\"link\">" .
2388 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2389 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2390 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2391 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2392 "</td>\n" .
2393 "</tr>\n";
2394 }
2395 print "</table>\n";
2396 git_footer_html();
2397 }
2398
2399 sub git_project_index {
2400 my @projects = git_get_projects_list();
2401
2402 print $cgi->header(
2403 -type => 'text/plain',
2404 -charset => 'utf-8',
2405 -content_disposition => 'inline; filename="index.aux"');
2406
2407 foreach my $pr (@projects) {
2408 if (!exists $pr->{'owner'}) {
2409 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2410 }
2411
2412 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2413 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2414 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2415 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2416 $path =~ s/ /\+/g;
2417 $owner =~ s/ /\+/g;
2418
2419 print "$path $owner\n";
2420 }
2421 }
2422
2423 sub git_summary {
2424 my $descr = git_get_project_description($project) || "none";
2425 my $head = git_get_head_hash($project);
2426 my %co = parse_commit($head);
2427 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2428
2429 my $owner = git_get_project_owner($project);
2430
2431 my ($reflist, $refs) = git_get_refs_list();
2432
2433 my @taglist;
2434 my @headlist;
2435 foreach my $ref (@$reflist) {
2436 if ($ref->{'name'} =~ s!^heads/!!) {
2437 push @headlist, $ref;
2438 } else {
2439 $ref->{'name'} =~ s!^tags/!!;
2440 push @taglist, $ref;
2441 }
2442 }
2443
2444 git_header_html();
2445 git_print_page_nav('summary','', $head);
2446
2447 print "<div class=\"title\">&nbsp;</div>\n";
2448 print "<table cellspacing=\"0\">\n" .
2449 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2450 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2451 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2452 # use per project git URL list in $projectroot/$project/cloneurl
2453 # or make project git URL from git base URL and project name
2454 my $url_tag = "URL";
2455 my @url_list = git_get_project_url_list($project);
2456 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2457 foreach my $git_url (@url_list) {
2458 next unless $git_url;
2459 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2460 $url_tag = "";
2461 }
2462 print "</table>\n";
2463
2464 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2465 git_get_head_hash($project)
2466 or die_error(undef, "Open git-rev-list failed");
2467 my @revlist = map { chomp; $_ } <$fd>;
2468 close $fd;
2469 git_print_header_div('shortlog');
2470 git_shortlog_body(\@revlist, 0, 15, $refs,
2471 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2472
2473 if (@taglist) {
2474 git_print_header_div('tags');
2475 git_tags_body(\@taglist, 0, 15,
2476 $cgi->a({-href => href(action=>"tags")}, "..."));
2477 }
2478
2479 if (@headlist) {
2480 git_print_header_div('heads');
2481 git_heads_body(\@headlist, $head, 0, 15,
2482 $cgi->a({-href => href(action=>"heads")}, "..."));
2483 }
2484
2485 git_footer_html();
2486 }
2487
2488 sub git_tag {
2489 my $head = git_get_head_hash($project);
2490 git_header_html();
2491 git_print_page_nav('','', $head,undef,$head);
2492 my %tag = parse_tag($hash);
2493 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2494 print "<div class=\"title_text\">\n" .
2495 "<table cellspacing=\"0\">\n" .
2496 "<tr>\n" .
2497 "<td>object</td>\n" .
2498 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2499 $tag{'object'}) . "</td>\n" .
2500 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2501 $tag{'type'}) . "</td>\n" .
2502 "</tr>\n";
2503 if (defined($tag{'author'})) {
2504 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2505 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2506 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2507 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2508 "</td></tr>\n";
2509 }
2510 print "</table>\n\n" .
2511 "</div>\n";
2512 print "<div class=\"page_body\">";
2513 my $comment = $tag{'comment'};
2514 foreach my $line (@$comment) {
2515 print esc_html($line) . "<br/>\n";
2516 }
2517 print "</div>\n";
2518 git_footer_html();
2519 }
2520
2521 sub git_blame2 {
2522 my $fd;
2523 my $ftype;
2524
2525 my ($have_blame) = gitweb_check_feature('blame');
2526 if (!$have_blame) {
2527 die_error('403 Permission denied', "Permission denied");
2528 }
2529 die_error('404 Not Found', "File name not defined") if (!$file_name);
2530 $hash_base ||= git_get_head_hash($project);
2531 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2532 my %co = parse_commit($hash_base)
2533 or die_error(undef, "Reading commit failed");
2534 if (!defined $hash) {
2535 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2536 or die_error(undef, "Error looking up file");
2537 }
2538 $ftype = git_get_type($hash);
2539 if ($ftype !~ "blob") {
2540 die_error("400 Bad Request", "Object is not a blob");
2541 }
2542 open ($fd, "-|", git_cmd(), "blame", '--porcelain', '--',
2543 $file_name, $hash_base)
2544 or die_error(undef, "Open git-blame failed");
2545 git_header_html();
2546 my $formats_nav =
2547 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2548 "blob") .
2549 " | " .
2550 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2551 "history") .
2552 " | " .
2553 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2554 "HEAD");
2555 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2556 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2557 git_print_page_path($file_name, $ftype, $hash_base);
2558 my @rev_color = (qw(light2 dark2));
2559 my $num_colors = scalar(@rev_color);
2560 my $current_color = 0;
2561 my $last_rev;
2562 print <<HTML;
2563 <div class="page_body">
2564 <table class="blame">
2565 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2566 HTML
2567 my %metainfo = ();
2568 while (1) {
2569 $_ = <$fd>;
2570 last unless defined $_;
2571 my ($full_rev, $lineno, $orig_lineno, $group_size) =
2572 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2573 if (!exists $metainfo{$full_rev}) {
2574 $metainfo{$full_rev} = {};
2575 }
2576 my $meta = $metainfo{$full_rev};
2577 while (<$fd>) {
2578 last if (s/^\t//);
2579 if (/^(\S+) (.*)$/) {
2580 $meta->{$1} = $2;
2581 }
2582 }
2583 my $data = $_;
2584 my $rev = substr($full_rev, 0, 8);
2585 my $author = $meta->{'author'};
2586 my %date = parse_date($meta->{'author-time'},
2587 $meta->{'author-tz'});
2588 my $date = $date{'iso-tz'};
2589 if ($group_size) {
2590 $current_color = ++$current_color % $num_colors;
2591 }
2592 print "<tr class=\"$rev_color[$current_color]\">\n";
2593 if ($group_size) {
2594 print "<td class=\"sha1\"";
2595 print " title=\"$author, $date\"";
2596 print " rowspan=\"$group_size\"" if ($group_size > 1);
2597 print ">";
2598 print $cgi->a({-href => href(action=>"commit",
2599 hash=>$full_rev,
2600 file_name=>$file_name)},
2601 esc_html($rev));
2602 print "</td>\n";
2603 }
2604 my $blamed = href(action => 'blame',
2605 file_name => $meta->{'filename'},
2606 hash_base => $full_rev);
2607 print "<td class=\"linenr\">";
2608 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2609 -id => "l$lineno",
2610 -class => "linenr" },
2611 esc_html($lineno));
2612 print "</td>";
2613 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2614 print "</tr>\n";
2615 }
2616 print "</table>\n";
2617 print "</div>";
2618 close $fd
2619 or print "Reading blob failed\n";
2620 git_footer_html();
2621 }
2622
2623 sub git_blame {
2624 my $fd;
2625
2626 my ($have_blame) = gitweb_check_feature('blame');
2627 if (!$have_blame) {
2628 die_error('403 Permission denied', "Permission denied");
2629 }
2630 die_error('404 Not Found', "File name not defined") if (!$file_name);
2631 $hash_base ||= git_get_head_hash($project);
2632 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2633 my %co = parse_commit($hash_base)
2634 or die_error(undef, "Reading commit failed");
2635 if (!defined $hash) {
2636 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2637 or die_error(undef, "Error lookup file");
2638 }
2639 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2640 or die_error(undef, "Open git-annotate failed");
2641 git_header_html();
2642 my $formats_nav =
2643 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2644 "blob") .
2645 " | " .
2646 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2647 "history") .
2648 " | " .
2649 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2650 "HEAD");
2651 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2652 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2653 git_print_page_path($file_name, 'blob', $hash_base);
2654 print "<div class=\"page_body\">\n";
2655 print <<HTML;
2656 <table class="blame">
2657 <tr>
2658 <th>Commit</th>
2659 <th>Age</th>
2660 <th>Author</th>
2661 <th>Line</th>
2662 <th>Data</th>
2663 </tr>
2664 HTML
2665 my @line_class = (qw(light dark));
2666 my $line_class_len = scalar (@line_class);
2667 my $line_class_num = $#line_class;
2668 while (my $line = <$fd>) {
2669 my $long_rev;
2670 my $short_rev;
2671 my $author;
2672 my $time;
2673 my $lineno;
2674 my $data;
2675 my $age;
2676 my $age_str;
2677 my $age_class;
2678
2679 chomp $line;
2680 $line_class_num = ($line_class_num + 1) % $line_class_len;
2681
2682 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2683 $long_rev = $1;
2684 $author = $2;
2685 $time = $3;
2686 $lineno = $4;
2687 $data = $5;
2688 } else {
2689 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2690 next;
2691 }
2692 $short_rev = substr ($long_rev, 0, 8);
2693 $age = time () - $time;
2694 $age_str = age_string ($age);
2695 $age_str =~ s/ /&nbsp;/g;
2696 $age_class = age_class($age);
2697 $author = esc_html ($author);
2698 $author =~ s/ /&nbsp;/g;
2699
2700 $data = untabify($data);
2701 $data = esc_html ($data);
2702
2703 print <<HTML;
2704 <tr class="$line_class[$line_class_num]">
2705 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2706 <td class="$age_class">$age_str</td>
2707 <td>$author</td>
2708 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2709 <td class="pre">$data</td>
2710 </tr>
2711 HTML
2712 } # while (my $line = <$fd>)
2713 print "</table>\n\n";
2714 close $fd
2715 or print "Reading blob failed.\n";
2716 print "</div>";
2717 git_footer_html();
2718 }
2719
2720 sub git_tags {
2721 my $head = git_get_head_hash($project);
2722 git_header_html();
2723 git_print_page_nav('','', $head,undef,$head);
2724 git_print_header_div('summary', $project);
2725
2726 my ($taglist) = git_get_refs_list("tags");
2727 if (@$taglist) {
2728 git_tags_body($taglist);
2729 }
2730 git_footer_html();
2731 }
2732
2733 sub git_heads {
2734 my $head = git_get_head_hash($project);
2735 git_header_html();
2736 git_print_page_nav('','', $head,undef,$head);
2737 git_print_header_div('summary', $project);
2738
2739 my ($headlist) = git_get_refs_list("heads");
2740 if (@$headlist) {
2741 git_heads_body($headlist, $head);
2742 }
2743 git_footer_html();
2744 }
2745
2746 sub git_blob_plain {
2747 my $expires;
2748
2749 if (!defined $hash) {
2750 if (defined $file_name) {
2751 my $base = $hash_base || git_get_head_hash($project);
2752 $hash = git_get_hash_by_path($base, $file_name, "blob")
2753 or die_error(undef, "Error lookup file");
2754 } else {
2755 die_error(undef, "No file name defined");
2756 }
2757 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2758 # blobs defined by non-textual hash id's can be cached
2759 $expires = "+1d";
2760 }
2761
2762 my $type = shift;
2763 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2764 or die_error(undef, "Couldn't cat $file_name, $hash");
2765
2766 $type ||= blob_mimetype($fd, $file_name);
2767
2768 # save as filename, even when no $file_name is given
2769 my $save_as = "$hash";
2770 if (defined $file_name) {
2771 $save_as = $file_name;
2772 } elsif ($type =~ m/^text\//) {
2773 $save_as .= '.txt';
2774 }
2775
2776 print $cgi->header(
2777 -type => "$type",
2778 -expires=>$expires,
2779 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2780 undef $/;
2781 binmode STDOUT, ':raw';
2782 print <$fd>;
2783 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2784 $/ = "\n";
2785 close $fd;
2786 }
2787
2788 sub git_blob {
2789 my $expires;
2790
2791 if (!defined $hash) {
2792 if (defined $file_name) {
2793 my $base = $hash_base || git_get_head_hash($project);
2794 $hash = git_get_hash_by_path($base, $file_name, "blob")
2795 or die_error(undef, "Error lookup file");
2796 } else {
2797 die_error(undef, "No file name defined");
2798 }
2799 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2800 # blobs defined by non-textual hash id's can be cached
2801 $expires = "+1d";
2802 }
2803
2804 my ($have_blame) = gitweb_check_feature('blame');
2805 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2806 or die_error(undef, "Couldn't cat $file_name, $hash");
2807 my $mimetype = blob_mimetype($fd, $file_name);
2808 if ($mimetype !~ m/^text\//) {
2809 close $fd;
2810 return git_blob_plain($mimetype);
2811 }
2812 git_header_html(undef, $expires);
2813 my $formats_nav = '';
2814 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2815 if (defined $file_name) {
2816 if ($have_blame) {
2817 $formats_nav .=
2818 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2819 hash=>$hash, file_name=>$file_name)},
2820 "blame") .
2821 " | ";
2822 }
2823 $formats_nav .=
2824 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2825 hash=>$hash, file_name=>$file_name)},
2826 "history") .
2827 " | " .
2828 $cgi->a({-href => href(action=>"blob_plain",
2829 hash=>$hash, file_name=>$file_name)},
2830 "raw") .
2831 " | " .
2832 $cgi->a({-href => href(action=>"blob",
2833 hash_base=>"HEAD", file_name=>$file_name)},
2834 "HEAD");
2835 } else {
2836 $formats_nav .=
2837 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2838 }
2839 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2840 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2841 } else {
2842 print "<div class=\"page_nav\">\n" .
2843 "<br/><br/></div>\n" .
2844 "<div class=\"title\">$hash</div>\n";
2845 }
2846 git_print_page_path($file_name, "blob", $hash_base);
2847 print "<div class=\"page_body\">\n";
2848 my $nr;
2849 while (my $line = <$fd>) {
2850 chomp $line;
2851 $nr++;
2852 $line = untabify($line);
2853 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2854 $nr, $nr, $nr, esc_html($line);
2855 }
2856 close $fd
2857 or print "Reading blob failed.\n";
2858 print "</div>";
2859 git_footer_html();
2860 }
2861
2862 sub git_tree {
2863 my $have_snapshot = gitweb_have_snapshot();
2864
2865 if (!defined $hash_base) {
2866 $hash_base = "HEAD";
2867 }
2868 if (!defined $hash) {
2869 if (defined $file_name) {
2870 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2871 } else {
2872 $hash = $hash_base;
2873 }
2874 }
2875 $/ = "\0";
2876 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2877 or die_error(undef, "Open git-ls-tree failed");
2878 my @entries = map { chomp; $_ } <$fd>;
2879 close $fd or die_error(undef, "Reading tree failed");
2880 $/ = "\n";
2881
2882 my $refs = git_get_references();
2883 my $ref = format_ref_marker($refs, $hash_base);
2884 git_header_html();
2885 my $base = "";
2886 my ($have_blame) = gitweb_check_feature('blame');
2887 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2888 my @views_nav = ();
2889 if (defined $file_name) {
2890 push @views_nav,
2891 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2892 hash=>$hash, file_name=>$file_name)},
2893 "history"),
2894 $cgi->a({-href => href(action=>"tree",
2895 hash_base=>"HEAD", file_name=>$file_name)},
2896 "HEAD"),
2897 }
2898 if ($have_snapshot) {
2899 # FIXME: Should be available when we have no hash base as well.
2900 push @views_nav,
2901 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2902 "snapshot");
2903 }
2904 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2905 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2906 } else {
2907 undef $hash_base;
2908 print "<div class=\"page_nav\">\n";
2909 print "<br/><br/></div>\n";
2910 print "<div class=\"title\">$hash</div>\n";
2911 }
2912 if (defined $file_name) {
2913 $base = esc_html("$file_name/");
2914 }
2915 git_print_page_path($file_name, 'tree', $hash_base);
2916 print "<div class=\"page_body\">\n";
2917 print "<table cellspacing=\"0\">\n";
2918 my $alternate = 1;
2919 foreach my $line (@entries) {
2920 my %t = parse_ls_tree_line($line, -z => 1);
2921
2922 if ($alternate) {
2923 print "<tr class=\"dark\">\n";
2924 } else {
2925 print "<tr class=\"light\">\n";
2926 }
2927 $alternate ^= 1;
2928
2929 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2930
2931 print "</tr>\n";
2932 }
2933 print "</table>\n" .
2934 "</div>";
2935 git_footer_html();
2936 }
2937
2938 sub git_snapshot {
2939 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2940 my $have_snapshot = (defined $ctype && defined $suffix);
2941 if (!$have_snapshot) {
2942 die_error('403 Permission denied', "Permission denied");
2943 }
2944
2945 if (!defined $hash) {
2946 $hash = git_get_head_hash($project);
2947 }
2948
2949 my $filename = basename($project) . "-$hash.tar.$suffix";
2950
2951 print $cgi->header(
2952 -type => 'application/x-tar',
2953 -content_encoding => $ctype,
2954 -content_disposition => 'inline; filename="' . "$filename" . '"',
2955 -status => '200 OK');
2956
2957 my $git = git_cmd_str();
2958 my $name = $project;
2959 $name =~ s/\047/\047\\\047\047/g;
2960 open my $fd, "-|",
2961 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
2962 or die_error(undef, "Execute git-tar-tree failed.");
2963 binmode STDOUT, ':raw';
2964 print <$fd>;
2965 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2966 close $fd;
2967
2968 }
2969
2970 sub git_log {
2971 my $head = git_get_head_hash($project);
2972 if (!defined $hash) {
2973 $hash = $head;
2974 }
2975 if (!defined $page) {
2976 $page = 0;
2977 }
2978 my $refs = git_get_references();
2979
2980 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2981 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2982 or die_error(undef, "Open git-rev-list failed");
2983 my @revlist = map { chomp; $_ } <$fd>;
2984 close $fd;
2985
2986 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2987
2988 git_header_html();
2989 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2990
2991 if (!@revlist) {
2992 my %co = parse_commit($hash);
2993
2994 git_print_header_div('summary', $project);
2995 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2996 }
2997 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2998 my $commit = $revlist[$i];
2999 my $ref = format_ref_marker($refs, $commit);
3000 my %co = parse_commit($commit);
3001 next if !%co;
3002 my %ad = parse_date($co{'author_epoch'});
3003 git_print_header_div('commit',
3004 "<span class=\"age\">$co{'age_string'}</span>" .
3005 esc_html($co{'title'}) . $ref,
3006 $commit);
3007 print "<div class=\"title_text\">\n" .
3008 "<div class=\"log_link\">\n" .
3009 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3010 " | " .
3011 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3012 " | " .
3013 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3014 "<br/>\n" .
3015 "</div>\n" .
3016 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3017 "</div>\n";
3018
3019 print "<div class=\"log_body\">\n";
3020 git_print_simplified_log($co{'comment'});
3021 print "</div>\n";
3022 }
3023 git_footer_html();
3024 }
3025
3026 sub git_commit {
3027 my %co = parse_commit($hash);
3028 if (!%co) {
3029 die_error(undef, "Unknown commit object");
3030 }
3031 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3032 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3033
3034 my $parent = $co{'parent'};
3035 if (!defined $parent) {
3036 $parent = "--root";
3037 }
3038 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3039 or die_error(undef, "Open git-diff-tree failed");
3040 my @difftree = map { chomp; $_ } <$fd>;
3041 close $fd or die_error(undef, "Reading git-diff-tree failed");
3042
3043 # non-textual hash id's can be cached
3044 my $expires;
3045 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3046 $expires = "+1d";
3047 }
3048 my $refs = git_get_references();
3049 my $ref = format_ref_marker($refs, $co{'id'});
3050
3051 my $have_snapshot = gitweb_have_snapshot();
3052
3053 my @views_nav = ();
3054 if (defined $file_name && defined $co{'parent'}) {
3055 push @views_nav,
3056 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3057 "blame");
3058 }
3059 git_header_html(undef, $expires);
3060 git_print_page_nav('commit', '',
3061 $hash, $co{'tree'}, $hash,
3062 join (' | ', @views_nav));
3063
3064 if (defined $co{'parent'}) {
3065 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3066 } else {
3067 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3068 }
3069 print "<div class=\"title_text\">\n" .
3070 "<table cellspacing=\"0\">\n";
3071 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3072 "<tr>" .
3073 "<td></td><td> $ad{'rfc2822'}";
3074 if ($ad{'hour_local'} < 6) {
3075 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3076 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3077 } else {
3078 printf(" (%02d:%02d %s)",
3079 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3080 }
3081 print "</td>" .
3082 "</tr>\n";
3083 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3084 print "<tr><td></td><td> $cd{'rfc2822'}" .
3085 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3086 "</td></tr>\n";
3087 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3088 print "<tr>" .
3089 "<td>tree</td>" .
3090 "<td class=\"sha1\">" .
3091 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3092 class => "list"}, $co{'tree'}) .
3093 "</td>" .
3094 "<td class=\"link\">" .
3095 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3096 "tree");
3097 if ($have_snapshot) {
3098 print " | " .
3099 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3100 }
3101 print "</td>" .
3102 "</tr>\n";
3103 my $parents = $co{'parents'};
3104 foreach my $par (@$parents) {
3105 print "<tr>" .
3106 "<td>parent</td>" .
3107 "<td class=\"sha1\">" .
3108 $cgi->a({-href => href(action=>"commit", hash=>$par),
3109 class => "list"}, $par) .
3110 "</td>" .
3111 "<td class=\"link\">" .
3112 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3113 " | " .
3114 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3115 "</td>" .
3116 "</tr>\n";
3117 }
3118 print "</table>".
3119 "</div>\n";
3120
3121 print "<div class=\"page_body\">\n";
3122 git_print_log($co{'comment'});
3123 print "</div>\n";
3124
3125 git_difftree_body(\@difftree, $hash, $parent);
3126
3127 git_footer_html();
3128 }
3129
3130 sub git_blobdiff {
3131 my $format = shift || 'html';
3132
3133 my $fd;
3134 my @difftree;
3135 my %diffinfo;
3136 my $expires;
3137
3138 # preparing $fd and %diffinfo for git_patchset_body
3139 # new style URI
3140 if (defined $hash_base && defined $hash_parent_base) {
3141 if (defined $file_name) {
3142 # read raw output
3143 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3144 "--", $file_name
3145 or die_error(undef, "Open git-diff-tree failed");
3146 @difftree = map { chomp; $_ } <$fd>;
3147 close $fd
3148 or die_error(undef, "Reading git-diff-tree failed");
3149 @difftree
3150 or die_error('404 Not Found', "Blob diff not found");
3151
3152 } elsif (defined $hash &&
3153 $hash =~ /[0-9a-fA-F]{40}/) {
3154 # try to find filename from $hash
3155
3156 # read filtered raw output
3157 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3158 or die_error(undef, "Open git-diff-tree failed");
3159 @difftree =
3160 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3161 # $hash == to_id
3162 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3163 map { chomp; $_ } <$fd>;
3164 close $fd
3165 or die_error(undef, "Reading git-diff-tree failed");
3166 @difftree
3167 or die_error('404 Not Found', "Blob diff not found");
3168
3169 } else {
3170 die_error('404 Not Found', "Missing one of the blob diff parameters");
3171 }
3172
3173 if (@difftree > 1) {
3174 die_error('404 Not Found', "Ambiguous blob diff specification");
3175 }
3176
3177 %diffinfo = parse_difftree_raw_line($difftree[0]);
3178 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3179 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3180
3181 $hash_parent ||= $diffinfo{'from_id'};
3182 $hash ||= $diffinfo{'to_id'};
3183
3184 # non-textual hash id's can be cached
3185 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3186 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3187 $expires = '+1d';
3188 }
3189
3190 # open patch output
3191 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3192 '-p', $hash_parent_base, $hash_base,
3193 "--", $file_name
3194 or die_error(undef, "Open git-diff-tree failed");
3195 }
3196
3197 # old/legacy style URI
3198 if (!%diffinfo && # if new style URI failed
3199 defined $hash && defined $hash_parent) {
3200 # fake git-diff-tree raw output
3201 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3202 $diffinfo{'from_id'} = $hash_parent;
3203 $diffinfo{'to_id'} = $hash;
3204 if (defined $file_name) {
3205 if (defined $file_parent) {
3206 $diffinfo{'status'} = '2';
3207 $diffinfo{'from_file'} = $file_parent;
3208 $diffinfo{'to_file'} = $file_name;
3209 } else { # assume not renamed
3210 $diffinfo{'status'} = '1';
3211 $diffinfo{'from_file'} = $file_name;
3212 $diffinfo{'to_file'} = $file_name;
3213 }
3214 } else { # no filename given
3215 $diffinfo{'status'} = '2';
3216 $diffinfo{'from_file'} = $hash_parent;
3217 $diffinfo{'to_file'} = $hash;
3218 }
3219
3220 # non-textual hash id's can be cached
3221 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3222 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3223 $expires = '+1d';
3224 }
3225
3226 # open patch output
3227 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3228 or die_error(undef, "Open git-diff failed");
3229 } else {
3230 die_error('404 Not Found', "Missing one of the blob diff parameters")
3231 unless %diffinfo;
3232 }
3233
3234 # header
3235 if ($format eq 'html') {
3236 my $formats_nav =
3237 $cgi->a({-href => href(action=>"blobdiff_plain",
3238 hash=>$hash, hash_parent=>$hash_parent,
3239 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3240 file_name=>$file_name, file_parent=>$file_parent)},
3241 "raw");
3242 git_header_html(undef, $expires);
3243 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3244 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3245 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3246 } else {
3247 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3248 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3249 }
3250 if (defined $file_name) {
3251 git_print_page_path($file_name, "blob", $hash_base);
3252 } else {
3253 print "<div class=\"page_path\"></div>\n";
3254 }
3255
3256 } elsif ($format eq 'plain') {
3257 print $cgi->header(
3258 -type => 'text/plain',
3259 -charset => 'utf-8',
3260 -expires => $expires,
3261 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3262
3263 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3264
3265 } else {
3266 die_error(undef, "Unknown blobdiff format");
3267 }
3268
3269 # patch
3270 if ($format eq 'html') {
3271 print "<div class=\"page_body\">\n";
3272
3273 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3274 close $fd;
3275
3276 print "</div>\n"; # class="page_body"
3277 git_footer_html();
3278
3279 } else {
3280 while (my $line = <$fd>) {
3281 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3282 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3283
3284 print $line;
3285
3286 last if $line =~ m!^\+\+\+!;
3287 }
3288 local $/ = undef;
3289 print <$fd>;
3290 close $fd;
3291 }
3292 }
3293
3294 sub git_blobdiff_plain {
3295 git_blobdiff('plain');
3296 }
3297
3298 sub git_commitdiff {
3299 my $format = shift || 'html';
3300 my %co = parse_commit($hash);
3301 if (!%co) {
3302 die_error(undef, "Unknown commit object");
3303 }
3304 if (!defined $hash_parent) {
3305 $hash_parent = $co{'parent'} || '--root';
3306 }
3307
3308 # read commitdiff
3309 my $fd;
3310 my @difftree;
3311 if ($format eq 'html') {
3312 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3313 "--patch-with-raw", "--full-index", $hash_parent, $hash
3314 or die_error(undef, "Open git-diff-tree failed");
3315
3316 while (chomp(my $line = <$fd>)) {
3317 # empty line ends raw part of diff-tree output
3318 last unless $line;
3319 push @difftree, $line;
3320 }
3321
3322 } elsif ($format eq 'plain') {
3323 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3324 '-p', $hash_parent, $hash
3325 or die_error(undef, "Open git-diff-tree failed");
3326
3327 } else {
3328 die_error(undef, "Unknown commitdiff format");
3329 }
3330
3331 # non-textual hash id's can be cached
3332 my $expires;
3333 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3334 $expires = "+1d";
3335 }
3336
3337 # write commit message
3338 if ($format eq 'html') {
3339 my $refs = git_get_references();
3340 my $ref = format_ref_marker($refs, $co{'id'});
3341 my $formats_nav =
3342 $cgi->a({-href => href(action=>"commitdiff_plain",
3343 hash=>$hash, hash_parent=>$hash_parent)},
3344 "raw");
3345
3346 git_header_html(undef, $expires);
3347 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3348 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3349 git_print_authorship(\%co);
3350 print "<div class=\"page_body\">\n";
3351 print "<div class=\"log\">\n";
3352 git_print_simplified_log($co{'comment'}, 1); # skip title
3353 print "</div>\n"; # class="log"
3354
3355 } elsif ($format eq 'plain') {
3356 my $refs = git_get_references("tags");
3357 my $tagname = git_get_rev_name_tags($hash);
3358 my $filename = basename($project) . "-$hash.patch";
3359
3360 print $cgi->header(
3361 -type => 'text/plain',
3362 -charset => 'utf-8',
3363 -expires => $expires,
3364 -content_disposition => 'inline; filename="' . "$filename" . '"');
3365 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3366 print <<TEXT;
3367 From: $co{'author'}
3368 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3369 Subject: $co{'title'}
3370 TEXT
3371 print "X-Git-Tag: $tagname\n" if $tagname;
3372 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3373
3374 foreach my $line (@{$co{'comment'}}) {
3375 print "$line\n";
3376 }
3377 print "---\n\n";
3378 }
3379
3380 # write patch
3381 if ($format eq 'html') {
3382 git_difftree_body(\@difftree, $hash, $hash_parent);
3383 print "<br/>\n";
3384
3385 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3386 close $fd;
3387 print "</div>\n"; # class="page_body"
3388 git_footer_html();
3389
3390 } elsif ($format eq 'plain') {
3391 local $/ = undef;
3392 print <$fd>;
3393 close $fd
3394 or print "Reading git-diff-tree failed\n";
3395 }
3396 }
3397
3398 sub git_commitdiff_plain {
3399 git_commitdiff('plain');
3400 }
3401
3402 sub git_history {
3403 if (!defined $hash_base) {
3404 $hash_base = git_get_head_hash($project);
3405 }
3406 if (!defined $page) {
3407 $page = 0;
3408 }
3409 my $ftype;
3410 my %co = parse_commit($hash_base);
3411 if (!%co) {
3412 die_error(undef, "Unknown commit object");
3413 }
3414
3415 my $refs = git_get_references();
3416 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3417
3418 if (!defined $hash && defined $file_name) {
3419 $hash = git_get_hash_by_path($hash_base, $file_name);
3420 }
3421 if (defined $hash) {
3422 $ftype = git_get_type($hash);
3423 }
3424
3425 open my $fd, "-|",
3426 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3427 or die_error(undef, "Open git-rev-list-failed");
3428 my @revlist = map { chomp; $_ } <$fd>;
3429 close $fd
3430 or die_error(undef, "Reading git-rev-list failed");
3431
3432 my $paging_nav = '';
3433 if ($page > 0) {
3434 $paging_nav .=
3435 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3436 file_name=>$file_name)},
3437 "first");
3438 $paging_nav .= " &sdot; " .
3439 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3440 file_name=>$file_name, page=>$page-1),
3441 -accesskey => "p", -title => "Alt-p"}, "prev");
3442 } else {
3443 $paging_nav .= "first";
3444 $paging_nav .= " &sdot; prev";
3445 }
3446 if ($#revlist >= (100 * ($page+1)-1)) {
3447 $paging_nav .= " &sdot; " .
3448 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3449 file_name=>$file_name, page=>$page+1),
3450 -accesskey => "n", -title => "Alt-n"}, "next");
3451 } else {
3452 $paging_nav .= " &sdot; next";
3453 }
3454 my $next_link = '';
3455 if ($#revlist >= (100 * ($page+1)-1)) {
3456 $next_link =
3457 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3458 file_name=>$file_name, page=>$page+1),
3459 -title => "Alt-n"}, "next");
3460 }
3461
3462 git_header_html();
3463 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3464 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3465 git_print_page_path($file_name, $ftype, $hash_base);
3466
3467 git_history_body(\@revlist, ($page * 100), $#revlist,
3468 $refs, $hash_base, $ftype, $next_link);
3469
3470 git_footer_html();
3471 }
3472
3473 sub git_search {
3474 if (!defined $searchtext) {
3475 die_error(undef, "Text field empty");
3476 }
3477 if (!defined $hash) {
3478 $hash = git_get_head_hash($project);
3479 }
3480 my %co = parse_commit($hash);
3481 if (!%co) {
3482 die_error(undef, "Unknown commit object");
3483 }
3484
3485 my $commit_search = 1;
3486 my $author_search = 0;
3487 my $committer_search = 0;
3488 my $pickaxe_search = 0;
3489 if ($searchtext =~ s/^author\\://i) {
3490 $author_search = 1;
3491 } elsif ($searchtext =~ s/^committer\\://i) {
3492 $committer_search = 1;
3493 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3494 $commit_search = 0;
3495 $pickaxe_search = 1;
3496
3497 # pickaxe may take all resources of your box and run for several minutes
3498 # with every query - so decide by yourself how public you make this feature
3499 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3500 if (!$have_pickaxe) {
3501 die_error('403 Permission denied', "Permission denied");
3502 }
3503 }
3504 git_header_html();
3505 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3506 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3507
3508 print "<table cellspacing=\"0\">\n";
3509 my $alternate = 1;
3510 if ($commit_search) {
3511 $/ = "\0";
3512 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3513 while (my $commit_text = <$fd>) {
3514 if (!grep m/$searchtext/i, $commit_text) {
3515 next;
3516 }
3517 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3518 next;
3519 }
3520 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3521 next;
3522 }
3523 my @commit_lines = split "\n", $commit_text;
3524 my %co = parse_commit(undef, \@commit_lines);
3525 if (!%co) {
3526 next;
3527 }
3528 if ($alternate) {
3529 print "<tr class=\"dark\">\n";
3530 } else {
3531 print "<tr class=\"light\">\n";
3532 }
3533 $alternate ^= 1;
3534 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3535 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3536 "<td>" .
3537 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3538 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3539 my $comment = $co{'comment'};
3540 foreach my $line (@$comment) {
3541 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3542 my $lead = esc_html($1) || "";
3543 $lead = chop_str($lead, 30, 10);
3544 my $match = esc_html($2) || "";
3545 my $trail = esc_html($3) || "";
3546 $trail = chop_str($trail, 30, 10);
3547 my $text = "$lead<span class=\"match\">$match</span>$trail";
3548 print chop_str($text, 80, 5) . "<br/>\n";
3549 }
3550 }
3551 print "</td>\n" .
3552 "<td class=\"link\">" .
3553 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3554 " | " .
3555 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3556 print "</td>\n" .
3557 "</tr>\n";
3558 }
3559 close $fd;
3560 }
3561
3562 if ($pickaxe_search) {
3563 $/ = "\n";
3564 my $git_command = git_cmd_str();
3565 open my $fd, "-|", "$git_command rev-list $hash | " .
3566 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3567 undef %co;
3568 my @files;
3569 while (my $line = <$fd>) {
3570 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3571 my %set;
3572 $set{'file'} = $6;
3573 $set{'from_id'} = $3;
3574 $set{'to_id'} = $4;
3575 $set{'id'} = $set{'to_id'};
3576 if ($set{'id'} =~ m/0{40}/) {
3577 $set{'id'} = $set{'from_id'};
3578 }
3579 if ($set{'id'} =~ m/0{40}/) {
3580 next;
3581 }
3582 push @files, \%set;
3583 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3584 if (%co) {
3585 if ($alternate) {
3586 print "<tr class=\"dark\">\n";
3587 } else {
3588 print "<tr class=\"light\">\n";
3589 }
3590 $alternate ^= 1;
3591 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3592 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3593 "<td>" .
3594 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3595 -class => "list subject"},
3596 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3597 while (my $setref = shift @files) {
3598 my %set = %$setref;
3599 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3600 hash=>$set{'id'}, file_name=>$set{'file'}),
3601 -class => "list"},
3602 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3603 "<br/>\n";
3604 }
3605 print "</td>\n" .
3606 "<td class=\"link\">" .
3607 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3608 " | " .
3609 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3610 print "</td>\n" .
3611 "</tr>\n";
3612 }
3613 %co = parse_commit($1);
3614 }
3615 }
3616 close $fd;
3617 }
3618 print "</table>\n";
3619 git_footer_html();
3620 }
3621
3622 sub git_shortlog {
3623 my $head = git_get_head_hash($project);
3624 if (!defined $hash) {
3625 $hash = $head;
3626 }
3627 if (!defined $page) {
3628 $page = 0;
3629 }
3630 my $refs = git_get_references();
3631
3632 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3633 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3634 or die_error(undef, "Open git-rev-list failed");
3635 my @revlist = map { chomp; $_ } <$fd>;
3636 close $fd;
3637
3638 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3639 my $next_link = '';
3640 if ($#revlist >= (100 * ($page+1)-1)) {
3641 $next_link =
3642 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3643 -title => "Alt-n"}, "next");
3644 }
3645
3646
3647 git_header_html();
3648 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3649 git_print_header_div('summary', $project);
3650
3651 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3652
3653 git_footer_html();
3654 }
3655
3656 ## ......................................................................
3657 ## feeds (RSS, OPML)
3658
3659 sub git_rss {
3660 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3661 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3662 or die_error(undef, "Open git-rev-list failed");
3663 my @revlist = map { chomp; $_ } <$fd>;
3664 close $fd or die_error(undef, "Reading git-rev-list failed");
3665 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3666 print <<XML;
3667 <?xml version="1.0" encoding="utf-8"?>
3668 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3669 <channel>
3670 <title>$project $my_uri $my_url</title>
3671 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3672 <description>$project log</description>
3673 <language>en</language>
3674 XML
3675
3676 for (my $i = 0; $i <= $#revlist; $i++) {
3677 my $commit = $revlist[$i];
3678 my %co = parse_commit($commit);
3679 # we read 150, we always show 30 and the ones more recent than 48 hours
3680 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3681 last;
3682 }
3683 my %cd = parse_date($co{'committer_epoch'});
3684 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3685 $co{'parent'}, $co{'id'}
3686 or next;
3687 my @difftree = map { chomp; $_ } <$fd>;
3688 close $fd
3689 or next;
3690 print "<item>\n" .
3691 "<title>" .
3692 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3693 "</title>\n" .
3694 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3695 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3696 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3697 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3698 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3699 "<content:encoded>" .
3700 "<![CDATA[\n";
3701 my $comment = $co{'comment'};
3702 foreach my $line (@$comment) {
3703 $line = to_utf8($line);
3704 print "$line<br/>\n";
3705 }
3706 print "<br/>\n";
3707 foreach my $line (@difftree) {
3708 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3709 next;
3710 }
3711 my $file = esc_html(unquote($7));
3712 $file = to_utf8($file);
3713 print "$file<br/>\n";
3714 }
3715 print "]]>\n" .
3716 "</content:encoded>\n" .
3717 "</item>\n";
3718 }
3719 print "</channel></rss>";
3720 }
3721
3722 sub git_opml {
3723 my @list = git_get_projects_list();
3724
3725 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3726 print <<XML;
3727 <?xml version="1.0" encoding="utf-8"?>
3728 <opml version="1.0">
3729 <head>
3730 <title>$site_name Git OPML Export</title>
3731 </head>
3732 <body>
3733 <outline text="git RSS feeds">
3734 XML
3735
3736 foreach my $pr (@list) {
3737 my %proj = %$pr;
3738 my $head = git_get_head_hash($proj{'path'});
3739 if (!defined $head) {
3740 next;
3741 }
3742 $git_dir = "$projectroot/$proj{'path'}";
3743 my %co = parse_commit($head);
3744 if (!%co) {
3745 next;
3746 }
3747
3748 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3749 my $rss = "$my_url?p=$proj{'path'};a=rss";
3750 my $html = "$my_url?p=$proj{'path'};a=summary";
3751 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3752 }
3753 print <<XML;
3754 </outline>
3755 </body>
3756 </opml>
3757 XML
3758 }
This page took 4.530169 seconds and 3 git commands to generate.